I've just started with javascript and im still trying to get my head around how it works.
Can you explain why this works
serialPort.on("open", function () {
console.log('open');
serialPort.on('data', function(data) {
console.log('data received: ' + data);
//socket.emit('test', { arduinotest: 'Arduino Connected'});
});
});
and this doesn't, i get a "data is not defined error"
serialPort.on("open", function () {
console.log('open');
serialPort.on('data', received(data));
});
function received(data) {
console.log('data received: ' + data);
//socket.emit('test', { arduinotest: 'Arduino Connected'});
}
How should I be separating my functions out as i don't like having lots of nested functions?
Here is my whole code
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server)
, SerialPort = require('serialport').SerialPort;
//SERIAL
var portName = '/dev/ttyACM0';
var serialPort = new SerialPort(portName, {
baudRate: 115200,
dataBits: 8,
stopBits: 1,
parity: 'none',
});
serialPort.on("open", function () {
console.log('open');
serialPort.on('data', function(data) {
console.log('data received: ' + data);
//socket.emit('test', { arduinotest: 'Arduino Connected'});
});
});
//SERVER
app.get('/', function (req, res){
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket){
socket.emit('test', { servertest: 'Server Connected' });
socket.on('value', function (data){
switch(data.key)
{
case "Right":
if(data.state == "True")
{
serialPort.write("d180\n");
console.log("test RT");
}
else if(data.state == "False")
{
serialPort.write("d90\n");
console.log("test RF");
}
break;
case "Left":
if(data.state == "True")
{
serialPort.write("d0\n");
console.log("test LT");
}
else if(data.state == "False")
{
serialPort.write("d90\n");
console.log("test LF");
}
break;
case "Up":
if(data.state == "True")
{
serialPort.write("s0\n");
console.log("test UT");
}
else if(data.state == "False")
{
serialPort.write("s90\n");
console.log("test UF");
}
break;
case "Down":
if(data.state == "True")
{
serialPort.write("s180\n");
console.log("test DT");
}
else if(data.state == "False")
{
serialPort.write("s90\n");
console.log("test DF");
}
break;
}
});
});
server.listen(9081, '192.168.1.158');
How should i be accessing my socket object from within my serial port code (commented out line)?
Thanks for helping with my understanding
Joe
In the second example you've written serialPort.on('data', received(data)); which will invoke the received function immediately, and pass the result to the second argument of the on listener.
Instead, you simply want to do serialPort.on('data', received); which passes the received function itself, meaning the listener can invoke it when ready.
To extend Graham's good answer. It is possible to send a callback that "takes parameters" by simply making a function that return a function. Here is how:
serialPort.on('data', received(data));
received = function(data){
return function(){
console.log(data);
}
}
For your other questions:
How should I be separating my functions out as i don't like having lots of nested functions? It is normal to have a lot of callbacks when writing a node application, this is how it was designed. Just separate you logic into main actions. For example, lets say that you want to save something in the database:
function createBobAndSave(){
var bob - new Bob();
bob.save(function(err,savedBob){
console.log(savedBob.id)
});
}
How should i be accessing my socket object from within my serial port code (commented out line)? You don't since socket in not within the client's socket scope. You can still use the sockets object to send data to all socket.io clients.