How to retrieve USB device information in Python?
To obtain information about a USB device, you can use the pyusb library in Python. pyusb is a Python library for accessing USB devices. Here is an example code demonstrating how to get information about USB devices connected to the computer:
import usb.core
# 查找USB设备
dev = usb.core.find(find_all=True)
# 遍历所有USB设备
for device in dev:
print("Device ID: %s" % device.idVendor)
print("Product ID: %s" % device.idProduct)
print("Device Class: %s" % device.bDeviceClass)
print("Manufacturer: %s" % usb.util.get_string(device, device.iManufacturer))
print("Product: %s" % usb.util.get_string(device, device.iProduct))
print("Serial Number: %s" % usb.util.get_string(device, device.iSerialNumber))
print("")
This code uses the usb.core.find() method to search for all USB devices. Then, it uses the usb.util.get_string() method to retrieve information such as manufacturer, product name, and serial number of the device. Finally, it prints out this information.
Please note that before using this code, you need to first install the pyusb library by running the following command:
pip install pyusb
Furthermore, before running this code, it is also important to make sure you have sufficient permissions to access the USB device.