1
|
import usb.core
|
2
|
import usb.util
|
3
|
|
4
|
|
5
|
def read_connected_devices():
|
6
|
detected_devices = []
|
7
|
|
8
|
busses = usb.busses()
|
9
|
|
10
|
for bus in busses:
|
11
|
devices = bus.devices
|
12
|
for dev in devices:
|
13
|
serial_number = None
|
14
|
device_info = usb.core.find(idProduct=dev.idProduct)
|
15
|
try:
|
16
|
serial_number = usb.util.get_string(device_info, device_info.iSerialNumber)
|
17
|
except:
|
18
|
# Failed to retrieve information from device
|
19
|
pass
|
20
|
|
21
|
if serial_number is not None:
|
22
|
detected_devices.append({
|
23
|
"vendor_id": dev.idVendor,
|
24
|
"product_id": dev.idProduct,
|
25
|
"serial_number": serial_number
|
26
|
})
|
27
|
|
28
|
return detected_devices
|