具有多个设备的 windows 上的节点串口挂起
node-serialport on windows with multiple devices hangs
我一直在尝试使用 node-serialport 库来访问连接到 USB 集线器的设备以及 send/receive 数据到这些设备。该代码在 linux 上运行良好,但在 windows(windows 8.1 和 windows 7) 上我得到了一些奇怪的行为。它似乎不适用于 2 个以上的设备,它只是在写入端口时挂起。 write 方法的回调永远不会被调用。我不确定如何去调试这个问题。我不是windows人,如果有人能给我一些指导就太好了。
下面是我目前用来测试的代码。
/*
Sample code to debug node-serialport library on windows
*/
//var SerialPort = require("./build/Debug/serialport");
var s = require("./serialport-logger");
var parsers = require('./parsers');
var ee = require('events');
s.list(function(err, ports) {
console.log("Number of ports available: " + ports.length);
ports.forEach(function(port) {
var cName = port.comName,
sp;
//console.log(cName);
sp = new s.SerialPort(cName, {
parser: s.parsers.readline("\r\n")
}, false);
// sp.once('data', function(data) {
// if (data) {
// console.log("Retrieved data " + data);
// //console.log(data);
// }
// });
//console.log("Is port open " + sp.isOpen());
if(!sp.isOpen()) {
sp.open(function(err) {
if(err) {
console.log("Port cannot be opened manually");
} else {
console.log("Port is open " + cName);
sp.write("LED=2\r\n", function(err) {
if (err) {
console.log("Cannot write to port");
console.error(err);
} else {
console.log("Written to port " + cName);
}
});
}
});
}
//sp.close();
});
});
我相信你已经注意到我不需要串行端口库,而是使用串行端口记录器库,这只是一种使用串行端口插件的方法,它是在调试开关打开的情况下编译的 windows框。
TLDR;对我来说,它通过增加 libuv 的线程池大小来工作。
$ UV_THREADPOOL_SIZE=20 && node server.js
一段时间以来,我对每个命令的 opening/closing 端口都很好,但我现在正在处理的功能请求需要保持端口打开并重新使用与 运行 命令的连接。所以我必须找到这个问题的答案。
我可以通过打开连接并保持连接来支持的设备数量是 3。问题恰好是默认线程池大小为 4。我已经有另一个后台工作者占用 1 个线程,所以我只有 3 个线程离开。 EIO_WatchPort function in node-serialport runs as a background worker which results in blocking a thread. So when I use more than 3 devices the "open" method call is waiting in the queue to be pushed to the background worker but since they are all busy it blocks node. Then any subsequent requests cannot be handled by node. Finally increasing the thread pool size did the trick, it's working fine now. It might help someone. Also this 线程绝对帮助了我。
正如 opensourcegeek 指出的那样,您需要做的就是将 UV_THREADPOOL_SIZE 变量设置为高于默认 4 个线程。
当我尝试在 USB 端口上查询更多 tan 3 RS-485 设备时,我的 node.js 和 modbus-rtu 或 modbus-serial 库项目出现问题。 3 台设备,没问题,第 4 台或更多,永久超时。这些设备每 600 毫秒响应一次,但当池繁忙时,它们永远不会收到响应。
So on Windows 只需在您的 node.js 环境命令行中输入:
设置 UV_THREADPOOL_SIZE=8
或任何你喜欢的直到 128。我查询了 6 个 USB 端口,所以我使用了 8 个。
我一直在尝试使用 node-serialport 库来访问连接到 USB 集线器的设备以及 send/receive 数据到这些设备。该代码在 linux 上运行良好,但在 windows(windows 8.1 和 windows 7) 上我得到了一些奇怪的行为。它似乎不适用于 2 个以上的设备,它只是在写入端口时挂起。 write 方法的回调永远不会被调用。我不确定如何去调试这个问题。我不是windows人,如果有人能给我一些指导就太好了。
下面是我目前用来测试的代码。
/*
Sample code to debug node-serialport library on windows
*/
//var SerialPort = require("./build/Debug/serialport");
var s = require("./serialport-logger");
var parsers = require('./parsers');
var ee = require('events');
s.list(function(err, ports) {
console.log("Number of ports available: " + ports.length);
ports.forEach(function(port) {
var cName = port.comName,
sp;
//console.log(cName);
sp = new s.SerialPort(cName, {
parser: s.parsers.readline("\r\n")
}, false);
// sp.once('data', function(data) {
// if (data) {
// console.log("Retrieved data " + data);
// //console.log(data);
// }
// });
//console.log("Is port open " + sp.isOpen());
if(!sp.isOpen()) {
sp.open(function(err) {
if(err) {
console.log("Port cannot be opened manually");
} else {
console.log("Port is open " + cName);
sp.write("LED=2\r\n", function(err) {
if (err) {
console.log("Cannot write to port");
console.error(err);
} else {
console.log("Written to port " + cName);
}
});
}
});
}
//sp.close();
});
});
我相信你已经注意到我不需要串行端口库,而是使用串行端口记录器库,这只是一种使用串行端口插件的方法,它是在调试开关打开的情况下编译的 windows框。
TLDR;对我来说,它通过增加 libuv 的线程池大小来工作。
$ UV_THREADPOOL_SIZE=20 && node server.js
一段时间以来,我对每个命令的 opening/closing 端口都很好,但我现在正在处理的功能请求需要保持端口打开并重新使用与 运行 命令的连接。所以我必须找到这个问题的答案。
我可以通过打开连接并保持连接来支持的设备数量是 3。问题恰好是默认线程池大小为 4。我已经有另一个后台工作者占用 1 个线程,所以我只有 3 个线程离开。 EIO_WatchPort function in node-serialport runs as a background worker which results in blocking a thread. So when I use more than 3 devices the "open" method call is waiting in the queue to be pushed to the background worker but since they are all busy it blocks node. Then any subsequent requests cannot be handled by node. Finally increasing the thread pool size did the trick, it's working fine now. It might help someone. Also this 线程绝对帮助了我。
正如 opensourcegeek 指出的那样,您需要做的就是将 UV_THREADPOOL_SIZE 变量设置为高于默认 4 个线程。
当我尝试在 USB 端口上查询更多 tan 3 RS-485 设备时,我的 node.js 和 modbus-rtu 或 modbus-serial 库项目出现问题。 3 台设备,没问题,第 4 台或更多,永久超时。这些设备每 600 毫秒响应一次,但当池繁忙时,它们永远不会收到响应。
So on Windows 只需在您的 node.js 环境命令行中输入: 设置 UV_THREADPOOL_SIZE=8 或任何你喜欢的直到 128。我查询了 6 个 USB 端口,所以我使用了 8 个。