Send HID report with PyUSB

后端 未结 2 1999
醉话见心
醉话见心 2021-01-06 12:25

UPDATE


I managed to send the data properly. For anyone who ran into the same problem, I used the following code:

data=[0x00, 0x04, 0x04,          


        
相关标签:
2条回答
  • 2021-01-06 12:47

    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.

    0 讨论(0)
  • 2021-01-06 12:47

    This is all you need to do HID with just 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, # "Vendor" Descriptor Type + 0 Descriptor Index
              0,     # USB interface № 0
              report # the HID payload as a byte array -- e.g. from struct.pack()
          )
    
      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, # "Vendor" Descriptor Type + 0 Descriptor Index
              0,     # USB interface № 0
              64     # max reply size
      )
    

    There isn't any need to jump onto the library-wrappers-around-libraries bandwagon. Are you an engineer or what? Just read the documentation. The protocol is not going to change anytime soon.

    Finally, yeah. All the four 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.

    0 讨论(0)
提交回复
热议问题