我有一个应用程序,需要保持一个长期(无限期)连接到usb设备,并打印一个潜在的无限次数。因此,它需要在相同的设备接口上做出大量声明。
最终,我的应用程序中断了,因为在第65次声明中,我被抛出了一个LIBUSB_ERROR_ACCESS错误。用node-usb可以修复这个问题吗?可能与
环境与硬件
windows 11 home insider preview
nodejs v11.0.0
node-usb v1.5.0
node-escpos v2码
function testClaim() {
const d = new USB();
const p = new Printer(d, { encoding: 'Shift-JIS' });
const loop = (curr = 0) => {
console.log('LOOP', curr)
if (curr === 50) {
setTimeout(() => {
d.open(() => d.reset(() => {
console.log('RESET', curr)
// should not require the user to do anything, needs to be able to print indefinitely
loop(0)
}))
}, 1000);
return;
}
d.open(() => {
p.close(() => {
loop(curr + 1)
})
})
}
loop();
}
testClaim();发布于 2021-12-09 10:10:39
我通过使用主线程来管理两个子线程来解决这个问题。
MAX_COUNT=6
print 1 [thread]
print 2 [thread]
print 3 [thread, thread] # MAX_COUNT / 2, add the head
print 4 [thread, thread]
print 5 [thread, thread]
print 6 [thread] # MAX_COUNT, the head becomes the tail, delete the previous tail
always print to the tail thread, it should always exist
this lets you spin up the thread asynchronously分叉线程内部的打印似乎不会转移到其他线程,因此libusb或node-usb中的某些内容似乎正在改变线程的内存,我不确定。
https://stackoverflow.com/questions/70273144
复制相似问题