首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能要求usb接口超过64次。

不能要求usb接口超过64次。
EN

Stack Overflow用户
提问于 2021-12-08 10:05:20
回答 1查看 199关注 0票数 0

我有一个应用程序,需要保持一个长期(无限期)连接到usb设备,并打印一个潜在的无限次数。因此,它需要在相同的设备接口上做出大量声明。

最终,我的应用程序中断了,因为在第65次声明中,我被抛出了一个LIBUSB_ERROR_ACCESS错误。用node-usb可以修复这个问题吗?可能与

环境与硬件

代码语言:javascript
复制
windows 11 home insider preview
nodejs v11.0.0
node-usb v1.5.0
node-escpos v2

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-09 10:10:39

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

代码语言:javascript
复制
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中的某些内容似乎正在改变线程的内存,我不确定。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70273144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档