不能占用 usb 接口超过 64 次

cannot claim usb interface more than 64 times

我有一个应用程序需要与 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();

我通过使用一个主线程来管理两个子线程来解决这个问题

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

分叉线程内的打印似乎不会转移到其他线程,所以 libusbnode-usb 中的某些内容似乎正在改变线程的内存,我不确定。