从串口阅读。

时间:2021-08-27 15:09:24

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?

基本上,我想从串口读取数据(在本例中,是通过USB)。我已经查看了node-serialport模块,但是它在串行端口的第一个结果之后一直拖着。我期望它在收到数据时就会吐出数据。就好像一个缓冲区被填满了,需要以某种方式刷新?

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

我稍微修改了这里的演示代码——https://github.com/voodotikigod/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.

我用的是Arduino所以是9600波特拉特。

Any help would be awesome, cheers,

任何帮助都会很棒,干杯,

James

詹姆斯

2 个解决方案

#1


7  

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

我还遇到了串口读取的问题。这是由于节点中的错误。js v4.7(参见本期)

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

但是,在切换到旧版本的Node之后,它就可以工作了。js(v4.0)。

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

它可能也适用于v4.6版本,但我还没有验证。

#2


23  

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.

node-serialport的作者。我已经跟踪了这个问题,这是由于在node.js中与IOWatcher的编译问题。我已经修改了从串口读取的策略,现在它应该在所有情况下都能正常工作。请确保您正在使用node-serialport 0.2.6和greater。

Now go out and build JS controlled robots!!!

现在出去制造JS控制机器人!!!

#1


7  

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

我还遇到了串口读取的问题。这是由于节点中的错误。js v4.7(参见本期)

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

但是,在切换到旧版本的Node之后,它就可以工作了。js(v4.0)。

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

它可能也适用于v4.6版本,但我还没有验证。

#2


23  

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.

node-serialport的作者。我已经跟踪了这个问题,这是由于在node.js中与IOWatcher的编译问题。我已经修改了从串口读取的策略,现在它应该在所有情况下都能正常工作。请确保您正在使用node-serialport 0.2.6和greater。

Now go out and build JS controlled robots!!!

现在出去制造JS控制机器人!!!