USB Device Search by VID and PID

  • Share this:

Code introduction


This function searches for a specific USB device connected to the computer by its Vendor ID and Product ID.


Technology Stack : Python, PyUSB

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
import usb.core
import usb.util

def find_usb_device_by_vid_pid(vid, pid):
    # Find all devices connected to the computer
    devices = usb.core.find(find_all=True)
    
    # Filter devices by vendor ID and product ID
    filtered_devices = [device for device in devices if device.idVendor == vid and device.idProduct == pid]
    
    # Return the first matching device or None if no device is found
    return filtered_devices[0] if filtered_devices else None                
              
Tags: