hid_test.py 937 B

12345678910111213141516171819202122
  1. # Install python3 HID package https://pypi.org/project/hid/
  2. import hid
  3. # default is TinyUSB (0xcafe), Adafruit (0x239a), RaspberryPi (0x2e8a), Espressif (0x303a) VID
  4. USB_VID = (0xcafe, 0x239a, 0x2e8a, 0x303a)
  5. print("VID list: " + ", ".join('%02x' % v for v in USB_VID))
  6. for vid in USB_VID:
  7. for dict in hid.enumerate(vid):
  8. print(dict)
  9. dev = hid.Device(dict['vendor_id'], dict['product_id'])
  10. if dev:
  11. while True:
  12. # Get input from console and encode to UTF8 for array of chars.
  13. # hid generic inout is single report therefore by HIDAPI requirement
  14. # it must be preceeded with 0x00 as dummy reportID
  15. str_out = b'\x00'
  16. str_out += input("Send text to HID Device : ").encode('utf-8')
  17. dev.write(str_out)
  18. str_in = dev.read(64)
  19. print("Received from HID Device:", str_in, '\n')