0

I am making an application that needs data read from the serial port, added to the BACK of a queue and then emitted over socket.io from the FRONT of the queue.

My sample code (untested so far) is this:

//includes
var http = require('http'),
io = require('socket.io'),
serialport = require('serialport'),
listish = require('listish');

//initialize serial
var portname = '/devttyACM0';
var sp = new serialport();
sp.open(portname, {
    baudrate: 9600,
    databits: 8,
    parity: 'none',
    stopbits: 1,
    flowcontrol: false
});

//initialize socket.io
io.sockets.on('connection', function(socket){
    socket.on('msg', function(msg){
        console.log(msg);
    });
    socket.on('disconnect', function(){
        console.log('disconnected');
    });
}).listen(8888);

//initialize queue
var queue = new listish();

//enqueue all data received on the serial port
sp.on('data', function(data){
    queue.enqueue(data.tostring());
})

//emit all data from the FRONT of the queue
io.sockets.emit('msg', queue.dequeue());

The part where it enqueues the data is largely OK, because it is asynchronous. When data is received on the serial port, THEN it is processed (enqueued).

What I want is the 'emit' to happen independently of everything else. That is, as long as there is data inside the queue, this has to run.

9
  • Should io.sockets.emit('msg', queue.dequeue()); be executed every n bytes from the serial port? Or on a timer? Or once if you get an end event on the sp readable stream? Commented Dec 16, 2013 at 14:52
  • @Plato I would like this to be executed again and again, independently of everything else in the code. Is it possible to use a "while(true)" loop and still keep all the parts in the same module? Commented Dec 16, 2013 at 15:44
  • I don't think you understand async event based programming correctly. The queue is useless here, unless you want a end event like Plato said. You just pipe whatever you get off the serial directly into sockets. Commented Dec 16, 2013 at 16:06
  • It is not possible to keep running it, that would 'block' the execution and prevent the rest of the program to run. Please explain, under what circumstances should the socket send the data from the serial port? Your options are pretty much, 'when the serial port emits a data event', 'on some data events but not others, based on this function', 'when the serial port emits an end event', 'every n ms', or 'when I manually fire it from some other function'. Commented Dec 16, 2013 at 16:18
  • 1
    As @TC1 suggests your best bet is likely to have the client send e.g. a requestingData event to the server; then the server would do io.sockets.on('requestingData', handleDataRequest). In the handleDataRequest function you would take some or all of your queue, package it in a socket.io message, and send it back to the client that requested it. Commented Dec 16, 2013 at 21:28

1 Answer 1

1

here's a draft, haven't tested it, let me know if it works!

//includes
var http = require('http'),
io = require('socket.io'),
serialport = require('serialport'),
listish = require('listish');

//initialize serial
var portname = '/devttyACM0';
var sp = new serialport();
sp.open(portname, {
    baudrate: 9600,
    databits: 8,
    parity: 'none',
    stopbits: 1,
    flowcontrol: false
});

//initialize socket.io
io.sockets.on('connection', function(socket){
    socket.on('msg', function(msg){
        console.log(msg);
    });
    socket.on('disconnect', function(){
        console.log('disconnected');
    });
    socket.on('requestingData', function(){
      var msg = queue.dequeue();
      if(msg === "" || msg === [] || msg === null){
        return false; // don't send 'msg' with no msg
      };
      // emit all data from the FRONT of the queue to all clients
      io.sockets.emit('msg', msg);

      // Or to only send msg to the socket that requested the data this time:
      // socket.emit('msg', queue.dequeue());
    });
}).listen(8888);

//initialize queue
var queue = new listish();

//enqueue all data received on the serial port
sp.on('data', function(data){
    queue.enqueue(data.tostring());
})

// ***client.js***
socket = io.connect('http://localhost:8888');
socket.on('connect', function(){
  alert('Connected! Check F12 console');
});
socket.on('msg', function(data){
  $('<div></div>').text(data)
  .appendTo($(document));
});

window.setInterval(
function(){
  socket.emit('requestingData');
}
, 200); // in ms
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.