Finding and Describing USB Devices

  • Share this:

Code introduction


This function is used to find and describe a specific USB device connected to the computer. It accepts the device vendor ID and product ID as parameters, then searches all connected devices, filters out the matching devices, and prints out the device details.


Technology Stack : PyUSB

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import usb.core
import usb.util

def find_and_describe_usb_device(vendor_id, product_id):
    # Find all devices connected to the computer
    devices = usb.core.find(find_all=True)
    # Filter devices by vendor and product ID
    target_devices = [device for device in devices if device.idVendor == vendor_id and device.idProduct == product_id]
    # Describe the found devices
    for device in target_devices:
        print(f"Device: {device}")
        print(f"  Vendor ID: {device.idVendor}")
        print(f"  Product ID: {device.idProduct}")
        print(f"  Serial Number: {device.serial}")
        print(f"  Manufacturer: {usb.util.get_string(device, device.iManufacturer)}")
        print(f"  Product: {usb.util.get_string(device, device.iProduct)}")
        print(f"  Interface: {device.active_configuration[(device.active_interface.bInterfaceNumber, 0)].bInterfaceClass}")                
              
Tags: