Send HID report with PyUSB

后端 未结 2 2025
醉话见心
醉话见心 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

    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.

提交回复
热议问题