Finding First USB Device by VID and PID

  • Share this:

Code introduction


This function is used to find the first USB device that matches the given vendor ID (vid) and product ID (pid). It first retrieves a list of all connected USB devices, then iterates through these devices to find a matching device.


Technology Stack : PyUSB

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import usb.core
import usb.util

def find_first_device_byvidpid(vid, pid):
    # This function finds the first USB device that matches the given vendor ID (vid) and product ID (pid)
    devices = usb.core.find(find_all=True)
    for device in devices:
        if device.idVendor == vid and device.idProduct == pid:
            return device
    return None

# JSON explanation                
              
Tags: