Finding USB Device by ID

  • Share this:

Code introduction


This function is used to find a USB device based on the given device ID and return the device and its configuration interface. If the device is not found, an exception is raised.


Technology Stack : PyUSB

Code Type : Function

Code Difficulty : Intermediate


                
                    
import usb.core
import usb.util

def find_usb_device(device_id):
    # Find a USB device using its vid and pid
    device = usb.core.find(idVendor=device_id[0], idProduct=device_id[1])
    if device is None:
        raise ValueError("Device not found")
    # Set the active configuration. With no arguments, the first configuration will be the default
    device.set_configuration()
    # Get an endpoint instance
    cfg = device.get_active_configuration()
    intf = cfg[(0,0)]
    return device, intf                
              
Tags: