Node.js – nodejs – reading from the serial port

node.jsserial-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

Best Answer

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!!!