USB Device Identification Function

  • Share this:

Code introduction


This function searches for a USB device connected to the computer by specified device ID from all devices connected to the computer.


Technology Stack : PyUSB

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import usb.core
import usb.util

def find_usb_device(device_id):
    # Find all devices connected to the computer
    devices = usb.core.find(find_all=True)
    
    # Filter devices by specified device ID
    target_devices = [dev for dev in devices if dev.idVendor == device_id[0] and dev.idProduct == device_id[1]]
    
    # Return the first matching device or None if not found
    return target_devices[0] if target_devices else None                
              
Tags: