13

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

6
  • You forgot a var infront of serial! Have you tried adding one of the parsers? Commented May 17, 2011 at 21:02
  • 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. Commented May 17, 2011 at 21:16
  • @James there's also a buffer size option in the constructor. Try pumping that way up! (it defaults to 255). Commented May 17, 2011 at 21:29
  • @Raynos, tried that too :(, thanks though! Commented May 17, 2011 at 21:34
  • @James compile in --debug then try debugging that module or raise a github bug Commented May 17, 2011 at 21:39

2 Answers 2

26

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

Sign up to request clarification or add additional context in comments.

1 Comment

Also, you don't need a repl - the serial port will keep the event loop open until you close it.
9

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.

1 Comment

Ok, I fixed this by updating my FTDI USB Serial drivers to 2.2.16 and updating node to 0.5.0-pre :)

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.