Easy USB access for Python with backend-neutral, cross-platform support
—
Find and enumerate USB devices using flexible filtering criteria. PyUSB provides powerful device discovery capabilities with support for custom matching logic and backend selection.
Locate USB devices based on descriptor properties with support for single or multiple device retrieval.
def find(find_all=False, backend=None, custom_match=None, **args):
"""
Find USB devices matching specified criteria.
Parameters:
- find_all: bool, if True return all matches, otherwise return first match
- backend: backend instance to use for device discovery
- custom_match: callable taking device as argument, return True for matches
- **args: device descriptor fields to match (idVendor, idProduct, bDeviceClass, etc.)
Returns:
Device object (find_all=False) or list of Device objects (find_all=True)
None if no devices found and find_all=False
Common descriptor fields for **args:
- idVendor: int, vendor ID
- idProduct: int, product ID
- bDeviceClass: int, device class
- bDeviceSubClass: int, device sub-class
- bDeviceProtocol: int, device protocol
- bcdUSB: int, USB version
"""Display comprehensive information about available USB devices.
def show_devices(verbose=False, **kwargs):
"""
Display information about available USB devices.
Parameters:
- verbose: bool, show detailed device information
- **kwargs: same filtering arguments as find()
"""import usb.core
# Find device by vendor and product ID
device = usb.core.find(idVendor=0x1234, idProduct=0x5678)
if device is None:
print("Device not found")
else:
print(f"Found device: {device}")
print(f"Vendor ID: 0x{device.idVendor:04x}")
print(f"Product ID: 0x{device.idProduct:04x}")import usb.core
# Find all devices from a specific vendor
devices = usb.core.find(find_all=True, idVendor=0x1234)
print(f"Found {len(devices)} devices from vendor 0x1234:")
for i, device in enumerate(devices):
print(f" Device {i}: Product ID 0x{device.idProduct:04x}")import usb.core
# Find all HID devices (Human Interface Device class)
hid_devices = usb.core.find(find_all=True, bDeviceClass=3)
print(f"Found {len(hid_devices)} HID devices:")
for device in hid_devices:
print(f" Vendor: 0x{device.idVendor:04x}, Product: 0x{device.idProduct:04x}")import usb.core
def is_high_speed_device(device):
"""Custom matcher for high-speed USB devices."""
return device.bcdUSB >= 0x0200 # USB 2.0 or higher
# Find all high-speed devices
high_speed_devices = usb.core.find(find_all=True, custom_match=is_high_speed_device)
print(f"Found {len(high_speed_devices)} high-speed devices")import usb.core
def find_storage_devices():
"""Find USB mass storage devices."""
return usb.core.find(
find_all=True,
bDeviceClass=8, # Mass Storage class
custom_match=lambda d: d.bDeviceSubClass == 6 # SCSI transparent subclass
)
storage_devices = find_storage_devices()
print(f"Found {len(storage_devices)} mass storage devices")import usb.core
# Show all available devices
print("All USB devices:")
usb.core.show_devices()
# Show detailed information
print("\nDetailed device information:")
usb.core.show_devices(verbose=True)
# Show devices from specific vendor
print("\nDevices from vendor 0x1234:")
usb.core.show_devices(idVendor=0x1234)import usb.core
import usb.backend.libusb1
# Use specific backend for device discovery
backend = usb.backend.libusb1.get_backend()
device = usb.core.find(idVendor=0x1234, backend=backend)
if device:
print(f"Found device using libusb1 backend: {device}")import usb.core
# Get all USB devices and examine each one
all_devices = usb.core.find(find_all=True)
for device in all_devices:
try:
# Access device properties
vendor_id = device.idVendor
product_id = device.idProduct
print(f"Device: {vendor_id:04x}:{product_id:04x}")
# Try to get string descriptors
try:
import usb.util
manufacturer = usb.util.get_string(device, device.iManufacturer)
product = usb.util.get_string(device, device.iProduct)
print(f" Manufacturer: {manufacturer}")
print(f" Product: {product}")
except:
print(" String descriptors not accessible")
except usb.core.USBError as e:
print(f"Error accessing device: {e}")import usb.core
import time
def wait_for_device(vendor_id, product_id, timeout=30):
"""Wait for a specific device to become available."""
start_time = time.time()
while time.time() - start_time < timeout:
device = usb.core.find(idVendor=vendor_id, idProduct=product_id)
if device is not None:
return device
time.sleep(1)
return None
# Wait for device to be connected
print("Waiting for device 0x1234:0x5678...")
device = wait_for_device(0x1234, 0x5678)
if device:
print("Device found!")
else:
print("Device not found within timeout period")Install with Tessl CLI
npx tessl i tessl/pypi-pyusb