|
10 | 10 |
|
11 | 11 | import usb.core |
12 | 12 | import usb.util |
| 13 | +import time |
13 | 14 |
|
14 | | -# find our device |
15 | | -dev = usb.core.find(idVendor=0x0000, idProduct=0x0001) |
16 | | - |
17 | | -# was it found? |
18 | | -if dev is None: |
19 | | - raise ValueError('Device not found') |
20 | | - |
21 | | -# get an endpoint instance |
22 | | -cfg = dev.get_active_configuration() |
23 | | -intf = cfg[(0, 0)] |
24 | | - |
25 | | -outep = usb.util.find_descriptor( |
26 | | - intf, |
27 | | - # match the first OUT endpoint |
28 | | - custom_match= \ |
29 | | - lambda e: \ |
30 | | - usb.util.endpoint_direction(e.bEndpointAddress) == \ |
31 | | - usb.util.ENDPOINT_OUT) |
32 | | - |
33 | | -inep = usb.util.find_descriptor( |
34 | | - intf, |
35 | | - # match the first IN endpoint |
36 | | - custom_match= \ |
37 | | - lambda e: \ |
38 | | - usb.util.endpoint_direction(e.bEndpointAddress) == \ |
39 | | - usb.util.ENDPOINT_IN) |
40 | | - |
41 | | -assert inep is not None |
42 | | -assert outep is not None |
43 | | - |
44 | | -test_string = "Hello World!" |
45 | | -outep.write(test_string) |
46 | | -from_device = inep.read(len(test_string)) |
47 | | - |
48 | | -print("Device Says: {}".format(''.join([chr(x) for x in from_device]))) |
| 15 | +try: |
| 16 | + # find our device |
| 17 | + # use list_devices.py for idVendor and idProduct values |
| 18 | + dev = usb.core.find(idVendor=0x0000, idProduct=0x0001) |
| 19 | + |
| 20 | + # was it found? |
| 21 | + if dev is None: |
| 22 | + raise ValueError("Device not found. Verify that device is attached, then check vendor and product settings.") |
| 23 | + |
| 24 | + print("Device found!") |
| 25 | + |
| 26 | + # Detach kernel driver BEFORE setting configuration |
| 27 | + detached_interfaces = [] |
| 28 | + for interface in [0, 1]: |
| 29 | + if dev.is_kernel_driver_active(interface): |
| 30 | + try: |
| 31 | + dev.detach_kernel_driver(interface) |
| 32 | + detached_interfaces.append(interface) |
| 33 | + print(f"Kernel driver detached from interface {interface}") |
| 34 | + except: |
| 35 | + pass |
| 36 | + |
| 37 | + # Set configuration |
| 38 | + try: |
| 39 | + dev.set_configuration() |
| 40 | + print("Configuration set") |
| 41 | + except usb.core.USBError as e: |
| 42 | + print(f"Configuration already set or error: {e}") |
| 43 | + |
| 44 | + # get an endpoint instance |
| 45 | + cfg = dev.get_active_configuration() |
| 46 | + intf = cfg[(1, 0)] # Interface 1, Alt 0 |
| 47 | + |
| 48 | + outep = usb.util.find_descriptor( |
| 49 | + intf, |
| 50 | + custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT) |
| 51 | + |
| 52 | + inep = usb.util.find_descriptor( |
| 53 | + intf, |
| 54 | + custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN) |
| 55 | + |
| 56 | + if inep is None: |
| 57 | + raise ValueError("IN endpoint not found") |
| 58 | + if outep is None: |
| 59 | + raise ValueError("OUT endpoint not found") |
| 60 | + |
| 61 | + print(f"OUT endpoint: 0x{outep.bEndpointAddress:02x}") |
| 62 | + print(f"IN endpoint: 0x{inep.bEndpointAddress:02x}") |
| 63 | + |
| 64 | + # Clear any stale data from the IN endpoint |
| 65 | + try: |
| 66 | + while True: |
| 67 | + inep.read(64, timeout=100) # Short timeout to flush buffer |
| 68 | + except usb.core.USBTimeoutError: |
| 69 | + pass # Good, buffer is empty now |
| 70 | + |
| 71 | + test_string = "Hello World!" |
| 72 | + print(f"\nSending: {test_string}") |
| 73 | + |
| 74 | + bytes_written = outep.write(test_string, timeout=1000) |
| 75 | + print(f"Wrote {bytes_written} bytes") |
| 76 | + |
| 77 | + time.sleep(0.1) # Give device a moment to process |
| 78 | + |
| 79 | + from_device = inep.read(64, timeout=2000) |
| 80 | + print(f"Received {len(from_device)} bytes") |
| 81 | + print("Device Says: {}".format(''.join([chr(x) for x in from_device]))) |
| 82 | +except usb.core.USBTimeoutError: |
| 83 | + print("\nTimeout! The device isn't responding.") |
| 84 | +except usb.core.USBError as e: |
| 85 | + print(f"USB Error: {e}") |
| 86 | +finally: |
| 87 | + # Proper cleanup |
| 88 | + print("\nCleaning up...") |
| 89 | + try: |
| 90 | + # Release interfaces |
| 91 | + usb.util.release_interface(dev, 1) |
| 92 | + usb.util.release_interface(dev, 0) |
| 93 | + |
| 94 | + # Reattach kernel driver if we detached it |
| 95 | + for interface in detached_interfaces: |
| 96 | + try: |
| 97 | + dev.attach_kernel_driver(interface) |
| 98 | + print(f"Kernel driver reattached to interface {interface}") |
| 99 | + except: |
| 100 | + print(f"Could not reattach kernel driver to interface {interface}") |
| 101 | + |
| 102 | + # Dispose of the device object |
| 103 | + usb.util.dispose_resources(dev) |
| 104 | + except: |
| 105 | + print("Attempts to cleanup by releasing interfaces failed.") |
| 106 | + |
| 107 | + print("Done!") |
0 commit comments