libusb1

3.3.1 · active · verified Tue Apr 14

libusb1 is a pure-Python wrapper for the `libusb-1.0` C library, providing low-level access to USB devices. It exposes both synchronous and asynchronous APIs, allowing access to all USB transfer types (control, bulk, interrupt, isochronous). Unlike PyUSB, which aims for a common subset across various USB libraries, libusb1 focuses on making the entire `libusb-1.0` API available. The project is actively maintained, with regular releases addressing bug fixes and improvements, such as the recent 3.3.1 release.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a USB context, list all connected USB devices, and then find and claim an interface on a specific device by its Vendor ID and Product ID. Remember to replace `VENDOR_ID` and `PRODUCT_ID` with the actual values for your target device, and adjust the interface number and endpoint operations as required. Root privileges may be necessary on some systems to access USB devices.

import usb1
import os

# Replace with your device's Vendor ID and Product ID
# You can often find these using 'lsusb' on Linux or device manager on Windows.
VENDOR_ID = os.environ.get('USB_DEVICE_VENDOR_ID', '0x1234') # Example vendor ID
PRODUCT_ID = os.environ.get('USB_DEVICE_PRODUCT_ID', '0x5678') # Example product ID

def list_all_devices():
    with usb1.USBContext() as context:
        print("Listing all USB devices:")
        for device in context.get  DeviceIterator():
            print(f"  Bus {device.getBusNumber():03d} Device {device.getDeviceAddress():03d}: ID {device.getVendorID():04x}:{device.getProductID():04x} {device.getManufacturerString() or 'N/A'} {device.getProductString() or 'N/A'}")

def find_and_access_device(vendor_id, product_id):
    print(f"\nAttempting to find device with ID {int(vendor_id, 16):04x}:{int(product_id, 16):04x}")
    try:
        with usb1.USBContext() as context:
            handle = context.openByVendorIDAndProductID(
                int(vendor_id, 16),
                int(product_id, 16),
                skip_on_error=True,
            )
            if handle is None:
                print("Device not found or access denied.")
                return

            with handle.claimInterface(0): # Claim interface 0 (adjust as needed)
                print(f"Successfully found and claimed interface 0 of device {handle.getVendorID():04x}:{handle.getProductID():04x}")
                # Example: Read from an endpoint (replace ENDPOINT_ADDRESS and BUFFER_SIZE)
                # data = handle.bulkRead(0x81, 64) 
                # print(f"Read: {data.hex()}")
                # Example: Write to an endpoint
                # handle.bulkWrite(0x01, b'Hello USB')

    except usb1.USBError as e:
        print(f"USB Error: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    list_all_devices()
    find_and_access_device(VENDOR_ID, PRODUCT_ID)

view raw JSON →