1
|
import logging
|
2
|
|
3
|
import usb.core
|
4
|
import usb.util
|
5
|
|
6
|
|
7
|
_invalid_devices = []
|
8
|
|
9
|
|
10
|
def read_connected_devices():
|
11
|
logging.debug("reading all currently connected devices")
|
12
|
detected_devices = []
|
13
|
|
14
|
busses = usb.busses()
|
15
|
|
16
|
for bus in busses:
|
17
|
devices = bus.devices
|
18
|
for dev in devices:
|
19
|
device = {
|
20
|
"vendor_id": dev.idVendor,
|
21
|
"product_id": dev.idProduct
|
22
|
}
|
23
|
serial_number = None
|
24
|
device_info = usb.core.find(idProduct=dev.idProduct)
|
25
|
try:
|
26
|
serial_number = usb.util.get_string(device_info, device_info.iSerialNumber)
|
27
|
except ValueError:
|
28
|
if device not in _invalid_devices:
|
29
|
logging.warning(f"Could not retrieve serial number from device {device}")
|
30
|
_invalid_devices.append(device)
|
31
|
|
32
|
if serial_number is not None:
|
33
|
if device in _invalid_devices:
|
34
|
_invalid_devices.remove(device)
|
35
|
|
36
|
device["serial_number"] = serial_number
|
37
|
detected_devices.append(device)
|
38
|
|
39
|
return detected_devices
|