test.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import usb.core
  2. import usb.util
  3. import sys
  4. import threading
  5. import time
  6. VID = '128d'
  7. PID = '0013'
  8. dev = usb.core.find(idVendor=VID, idProduct=PID)
  9. cfg = dev.get_active_configuration()
  10. intf = cfg[(0, 0)]
  11. print(intf)
  12. write_ep = usb.util.find_descriptor(
  13. intf,
  14. custom_match= \
  15. lambda e: \
  16. usb.util.endpoint_direction(e.bEndpointAddress) == \
  17. usb.util.ENDPOINT_OUT
  18. )
  19. read_ep = usb.util.find_descriptor(
  20. intf,
  21. custom_match= \
  22. lambda e: \
  23. usb.util.endpoint_direction(e.bEndpointAddress) == \
  24. usb.util.ENDPOINT_IN
  25. )
  26. def write_endpoint_thread():
  27. while True:
  28. try:
  29. write_ep.write("0123456789")
  30. except:
  31. print("write error")
  32. def read_endpoint_thread():
  33. while True:
  34. try:
  35. data = read_ep.read(64)
  36. size = len(data)
  37. if size > 0:
  38. print("recv %d bytes:" % size, end='')
  39. for i in range(0, size):
  40. print("%c" % data[i], end='')
  41. print('')
  42. except:
  43. print("no data")
  44. def main():
  45. r = threading.Thread(target=read_endpoint_thread)
  46. w = threading.Thread(target=write_endpoint_thread)
  47. r.start()
  48. w.start()
  49. r.join()
  50. w.join()
  51. dev.reset()
  52. if __name__ == '__main__':
  53. main()