13
votes

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

2
You forgot a var infront of serial! Have you tried adding one of the parsers?Raynos
Thanks, I've just added that. I've tried the other parser (for new lines) and that doesn't seem to do the trick either.James
@James there's also a buffer size option in the constructor. Try pumping that way up! (it defaults to 255).Raynos
@Raynos, tried that too :(, thanks though!James
@James compile in --debug then try debugging that module or raise a github bugRaynos

2 Answers

26
votes

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

9
votes

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.