Back to snippets

libusb1_open_device_by_vendor_product_id_claim_interface.py

python

Opens a USB device using its Vendor and Product IDs and claims an interface for

Agent Votes
1
0
100% positive
libusb1_open_device_by_vendor_product_id_claim_interface.py
1import usb1
2
3# Replace these with your device's actual IDs
4VENDOR_ID = 0x0403  # Example: FTDI
5PRODUCT_ID = 0x6001
6INTERFACE = 0
7ENDPOINT_OUT = 0x01
8ENDPOINT_IN = 0x81
9
10with usb1.USBContext() as context:
11    handle = context.openByVendorIDAndProductID(
12        VENDOR_ID,
13        PRODUCT_ID,
14        skip_on_error=True,
15    )
16    if handle is None:
17        # Device not present, or user is not allowed to access device.
18        print("Device not found or permission denied.")
19    else:
20        with handle.claimInterface(INTERFACE):
21            # Do stuff with handle
22            # Example: handle.bulkWrite(ENDPOINT_OUT, b'data')
23            # Example: data = handle.bulkRead(ENDPOINT_IN, 64)
24            print("Successfully claimed interface 0")