pyusb无法访问OUT端点

1 投票
1 回答
3791 浏览
提问于 2025-04-20 18:40

我正在尝试将数字锁定键(num lock)发送到我的自定义硬件,这个硬件充当一个HID键盘。我连接了一个LED灯,当USB接收到数字锁定键时,LED会亮起来。这个功能在外部键盘按下数字锁定键时工作得很好。但是,我通过pyusb手动发送数字锁定键(0x01)时却遇到了问题。

以下是负责发送数字锁定键的代码部分:

  dev = usb.core.find(idVendor=0xXXXX, idProduct=0xXXXX)

  try:
    dev.set_configuration()
  except usb.core.USBError as e:
    print e
  #endpoint = dev[0][(0,0)][0]

  # get an endpoint instance
  cfg = dev.get_active_configuration()
  intf = cfg[(0,0)]

  print intf

  ep = usb.util.find_descriptor(
      intf,
      # match the first OUT endpoint
      custom_match = \
      lambda e: \
          usb.util.endpoint_direction(e.bEndpointAddress) == \
          usb.util.ENDPOINT_OUT)

  assert ep is not None

  # write the data
  ep.write('\x01')

我的输出是:

    INTERFACE 0: Human Interface Device ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x1
     bInterfaceClass    :    0x3 Human Interface Device
     bInterfaceSubClass :    0x0
     bInterfaceProtocol :    0x1
     iInterface         :    0x0
      ENDPOINT 0x81: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x81 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :   0x18
Traceback (most recent call last):
  File "./main.py", line 43, in <module>
    assert ep is not None
AssertionError

因为从外部键盘发送这个信号是可以的,所以我猜应该没有权限方面的问题,或者说操作系统可以访问这个功能,但外部程序却无法访问。我现在在使用Mac。有没有人能帮我解决这个问题呢?

谢谢。

1 个回答

3

你的硬件没有完全支持USB HID键盘协议——它没有一个OUT端点,所以当你的脚本找不到这个端点时就会停止运行。

如果你使用的是Mac电脑,可以试着用苹果的“USB Prober”开发工具来检查这个设备,这个工具是“Xcode硬件工具”包的一部分。(你可以从http://developer.apple.com下载。)

撰写回答