summaryrefslogtreecommitdiff
path: root/examples/usb/usb_simple_host_pyusb.py
blob: d8ac2dd9c3e342048a28de2ff577792491aada67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
#
# Host side of the `usb_simple_device.py` example.  This must be run using standard
# Python on a PC.  See further instructions in `usb_simple_device.py`.

import sys
import usb.core
import usb.util

# VID and PID of the custom USB device.
VID = 0xF055
PID = 0x9999

# USB endpoints used by the device.
EP_OUT = 0x01
EP_IN = 0x81


def main():
    # Search for the custom USB device by VID/PID.
    dev = usb.core.find(idVendor=VID, idProduct=PID)

    if dev is None:
        print("No USB device found")
        sys.exit(1)

    # Claim the USB device.
    usb.util.claim_interface(dev, 0)

    # Read the device's strings.
    for i in range(0x11, 0x17):
        print(f"str{i}:", usb.util.get_string(dev, i))

    # Test writing to the device.
    ret = dev.write(EP_OUT, b"01234567", timeout=1000)
    print(ret)

    # Test reading from the device.
    print(dev.read(EP_IN, 64))

    # Release the USB device.
    usb.util.release_interface(dev, 0)
    usb.util.dispose_resources(dev)


if __name__ == "__main__":
    main()