node.js - Arduino Serial and Socket -


i m trying send serial data arduino using node.js , socket.io , code.

and html page have 1 button. work node , html side .but not send serial data.

var express = require('express'); var app = express(); var server = require('http').createserver(app); var io = require('socket.io')(server);  var port = process.env.port || 3000;    server.listen(port, function () { //  console.log('server listening @ port %d', port); });   // routing   app.use(express.static(__dirname + '/public'));  var serialport = require("serialport").serialport var serialport = new serialport("/dev/ttyacm3", { baudrate:9600 }, false); // openimmediately flag [default true]    io.on('connection', function (socket) {      socket.on('my other event', function (data) {         console.log(data);          serialport.open(function () {             console.log('open');             serialport.on('data', function (data) {                 console.log('data received: ' + data);             });          serialport.write(data, function (err, results) {             console.log('err ' + err);             console.log('results ' + results);         });     });    });   });   app.get('/', function (req, res) {  res.sendfile(__dirname + '/index.html'); }); 

sending serial messages arduino not easy passing in string. unfortunately have send string character character arduino receive , concatenate string. after sent last character need send 1 final new line character (/n) signal arduino stop concatenating , evaluate message.

this need in node.js server:

// socket.io message browser socket.on('serialevent', function (data) {      // message received string     console.log(data);      // sending string character character     for(var i=0; i<data.length; i++){         myport.write(new buffer(data[i], 'ascii'), function(err, results) {             // console.log('error: ' + err);             // console.log('results ' + results);         });     }      // sending terminate character     myport.write(new buffer('\n', 'ascii'), function(err, results) {         // console.log('err ' + err);         // console.log('results ' + results);     }); }); 

and arduino code receives this:

string indata = "";  void loop(){     while (serial.available() > 0) {         char received = serial.read();         indata.concat(received);          // process message when new line character received         if (received == '\n') {             // message ready in indate         }     } } 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -