Node.js Serialport同步写入读取
有没有人有任何示例代码以阻塞/同步方式使用node.js serialport模块?
我试图做的是发送一个命令给微控制器,并在发送下一个命令之前等待响应。
我有发送/接收工作,但数据只是与听众进来
serial.on( "data", function( data) { console.log(data); });
有没有办法等待返回的数据后做一个
serial.write("Send Command");
我应该设置一个全球标志或什么?
对于node.js的asynchronous编程风格,我还是一个新手
谢谢
没有这样的select,实际上没有必要。 这样做的一个方法是保持一个命令队列。 像这样的东西:
function Device (serial) { this._serial = serial; this._queue = queue; this._busy = false; this._current = null; var device = this; serial.on('data', function (data) { if (!device._current) return; device._current[1](null, data); device.processQueue(); }); } Device.prototype.send = function (data, callback) { this._queue.push([data, callback]); if (this._busy) return; this._busy = true; this.processQueue(); }; Device.prototype.processQueue = function () { var next = this._queue.shift(); if (!next) { this._busy = false; return; } this._current = next; this._serial.write(next[0]); };