具有多次写入的节点串行端口问题
Node-SerialPort Issue w/ Multiple Writes
当我传递一个包含 1 个元素的数组时一切正常,当我传递一个包含两个元素时(对于我们的用例最大)我收到以下错误:
There's no write queue for that file descriptor (after write)!
这是我的代码:
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;
});
});
}
第一次写入效果很好,第二次失败了。我猜有一个明显的解决方案,但我没有找到它。任何见解将不胜感激。
我最后做了两件事来解决这个问题。首先我升级到 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();
}
}
我现在可以连续写入而不会导致任何错误,并且端口不会以奇怪的锁定状态结束。
当我传递一个包含 1 个元素的数组时一切正常,当我传递一个包含两个元素时(对于我们的用例最大)我收到以下错误:
There's no write queue for that file descriptor (after write)!
这是我的代码:
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;
});
});
}
第一次写入效果很好,第二次失败了。我猜有一个明显的解决方案,但我没有找到它。任何见解将不胜感激。
我最后做了两件事来解决这个问题。首先我升级到 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();
}
}
我现在可以连续写入而不会导致任何错误,并且端口不会以奇怪的锁定状态结束。