OUT Endpoint not accessible by pyusb

2019-05-31 17:09发布

问题:

I am trying to send num lock to my custom hardware acting as a HID Keyboard. I have tied up an LED to glow if the num lock key is received on the USB. It works fine for numlock keypress from external keyboard. But I am unable to send the num lock key manually through pyusb (0x01)

This is the part of the code responsible for sending it:

  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')

My output is :

    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

Since it is doable from an external keyboard I guess there are no issues with permission or maybe it is accessible by the OS but not by an external process. I am on Mac. Can someone help me out here.

Thanks.

回答1:

Your hardware hasn't fully implemented the USB HID keyboard protocol — it doesn't have an OUT endpoint, so your script is bailing out when it fails to find one.

If you're on a Mac, you may want to inspect the device using Apple's "USB Prober" developer utility, which is part of the "Hardware Tools for Xcode" package. (You can download it from http://developer.apple.com.)