Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs - reading from the serial port

I've been looking around for an answer on this...

Basically, I want to read data from the serial port (in this case, over USB). I've looked into the node-serialport module but it keeps stalling after the first result form the serial port. I expected it to just spit out the data when it received it. It's as if a buffer is filling up and needs to be flushed somehow?

I've slightly modified the code from the demos I found here - https://github.com/voodootikigod/node-serialport/tree/master/tests

Here's my code:

    var sys = require("sys"),
    repl = require("repl"),
    serialPort = require("serialport").SerialPort;

    // Create new serialport pointer
    var serial = new serialPort("/dev/tty.usbmodem1d11" , { baudrate : 9600 });

    // Add data read event listener
    serial.on( "data", function( chunk ) {
        sys.puts(chunk);
    });


    serial.on( "error", function( msg ) {
        sys.puts("error: " + msg );
    });

    repl.start( "=>" );

I'm using an Arduino hence the 9600 baudrate.

Any help would be awesome, cheers,

James

like image 315
James Avatar asked May 17 '11 20:05

James


2 Answers

Author of node-serialport. I have tracked down the issue and it is due to a compilation issue with IOWatcher in node.js. I have revised the strategy for reading from the serial port and it now should function as designed in all cases. Please ensure you are using node-serialport 0.2.6 and greater.

Now go out and build JS controlled robots!!!

like image 177
voodootikigod Avatar answered Nov 06 '22 10:11

voodootikigod


I also experienced problems with the serial port read. This is due to a bug in node.js v4.7 (see this issue)

However it worked after switching to an older version of Node.js (v4.0).

It might work with versions up to v4.6 also, but I haven't verified that yet.

like image 9
Stefan Avatar answered Nov 06 '22 11:11

Stefan