具有多个写入的节点 – 串行端口问题

当我传递一个元素的数组时,一切都很好,当我传递一个二(max用于我们的用例)我得到以下错误:

该文件描述符没有写入队列(写入后)!

这是我的代码:

exports.triggerPhysical = function(state, alerts) { console.dir("IN PHYSICAL"); console.dir(alerts); SerialPort.list(function(err, ports) { var port = {}; for(var i = 0; i < ports.length; i++) { try { if(typeof ports[i].manufacturer != 'undefined' && ports[i].manufacturer.includes("Numato")) { port = ports[i]; } } catch(err) { console.dir(err); } } var numato = new SerialPort(port.comName, {baudrate : 19200}, function(err) { if(err) { return console.dir(err); } console.dir('calling write'); for(var j = 0; j < alerts.length; j++) { numato.write('relay ' + state + ' ' + alerts[j].index + '\r', function(err) { if(err) { console.dir('error writing'); console.dir(err); } console.dir('serial message written'); }); } numato.close(); return true; }); }); } 

第一个写作很好,第二个写失败。 我猜测有一个明显的解决scheme,但我没有find它。 任何有识之士将不胜感激。

我最终做了两件事来解决这个问题。 首先我升级到node-serialport库的版本5.x。

其次,我将我的代码更改为以下内容:

 exports.triggerPhysical = function(state, alerts) { var port = new SerialPort('/dev/ttyACM0'); port.on("open", function() { alerts.forEach(function(alert, idx) { str = 'relay ' + state + ' ' + alert.index + '\r'; port.write(str, function(err, results) { if(err) { console.dir("err writing"); console.dir(err); } else { console.dir(results); } }); }); }) port.drain(writeDone); function writeDone() { console.dir("In writeDone"); port.close(); } } 

我现在可以做连续写入而不会造成任何错误,并且端口不会以奇怪的locking状态结束。