nodejs - 从串口读取
我一直在寻找这个问题的答案......
基本上,我想从串行端口读取数据(在这种情况下,通过USB)。我研究了节点串行端口模块,但在从串行端口发出第一个结果后,它一直停滞不前。我预计它在收到数据时会吐出数据。就好像缓冲区已满并且需要以某种方式刷新?
我稍微修改了我在这里找到的演示中的代码 - https://github .com/voodootikigod/node-serialport/tree/master/tests
这是我的代码:
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( "=>" );
我使用的是 Arduino,因此波特率是 9600。
任何帮助都会很棒,干杯,
詹姆斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
节点串行端口的作者。我已经找到了这个问题,这是由于 Node.js 中 IOWatcher 的编译问题造成的。我修改了从串行端口读取的策略,现在它应该在所有情况下都能按设计运行。请确保您使用的是node-serialport 0.2.6 及更高版本。
现在出去建造 JS 控制的机器人吧!
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!!!
我也遇到了串口读取的问题。
这是由于 node.js v4.7 中的错误造成的(请参阅此问题 )
但是在切换到旧版本的 Node.js (v4.0) 后它就可以工作了。
它也可能适用于 v4.6 之前的版本,但我尚未验证这一点。
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.