【问题标题】:cannot claim usb interface more than 64 times不能超过 64 次认领 USB 接口
【发布时间】:2021-12-08 10:05:20
【问题描述】:

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

【问题讨论】:

    标签: node.js windows usb libusb


    【解决方案1】:

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

    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 中的某些内容似乎正在改变线程的内存,我不确定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-10
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 2015-10-27
      • 2012-05-13
      相关资源
      最近更新 更多