PyUSB send HID report

a 夏天 提交于 2019-12-01 00:58:08

Don't use PyUSB (unless you need other protocols too). Managing HID isn't difficult, but there is a much easier solution.

HIDAPI is a C-library which manages the protocol, and there is a Python wrapper available too.

Also, it hides the necessity to take control back from the operating system, which recognizes the HID protocol on connection, and install its own driver.

This is all you need to do HID with PyUSB:

  def hid_set_report(dev, report):
      """ Implements HID SetReport via USB control transfer """
      dev.ctrl_transfer(
          0x21, # REQUEST_TYPE_CLASS | RECIPIENT_INTERFACE | ENDPOINT_OUT
          9, # SET_REPORT
          0x200, 0x00,
          report)

  def hid_get_report(dev):
      """ Implements HID GetReport via USB control transfer """
      return dev.ctrl_transfer(
          0xA1, # REQUEST_TYPE_CLASS | RECIPIENT_INTERFACE | ENDPOINT_IN
          1, # GET_REPORT
          0x200, 0x00,
          64)

No need to jump onto the library-wrappers-around-libraries bandwagon. Are you an engineer or what? Just read the doc. The protocol is not going to change anytime soon.

Finally, yeah. All the 4 libusbhid's I've seen are written in disastrously horrible C and depend on yet even more libraries. For what is essentially 10 lines of code. Make your own decision.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!