nodejs - 从串口读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6036427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nodejs - reading from the serial port
提问by James
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/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.
我使用的是 Arduino,因此波特率为 9600。
Any help would be awesome, cheers,
任何帮助都会很棒,干杯,
James
詹姆士
采纳答案by Stefan
I also experienced problems with the serial port read. This is due to a bug in node.js v4.7 (see this issue)
我也遇到了串口读取的问题。这是由于 node.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 的版本,但我还没有验证过。
回答by voodootikigod
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.js 中 IOWatcher 的编译问题。我已经修改了从串行端口读取的策略,现在它应该在所有情况下都按设计运行。请确保您使用的是 node-serialport 0.2.6 及更高版本。
Now go out and build JS controlled robots!!!
现在出去搭建JS控制的机器人!!!

