Skip to content
This repository was archived by the owner on Jan 17, 2023. It is now read-only.

Commit 6daec79

Browse files
authored
Merge pull request #362 from NickSamNam/master
win32 serial enumeration fix
2 parents 7007fe2 + 77e2e69 commit 6daec79

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

nordicsemi/lister/windows/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class Device:
5757
"""DEVPKEY_Device_xxx constants"""
5858
ContainerId = DevicePropertyKey('{8c7ed206-3f8a-4827-b3ab-ae9e1faefc6c}', 2,
5959
'DEVPKEY_Device_ContainerId')
60+
DeviceAddress = DevicePropertyKey('{a45c254e-df1c-4efd-8020-67d146a850e0}', 30,
61+
'DEVPKEY_Device_Address')
6062

6163
# noinspection SpellCheckingInspection
6264
DIGCF_DEFAULT = DiGetClassDevsFlags.Default

nordicsemi/lister/windows/lister_win32.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ def get_serial_serial_no(vendor_id, product_id, h_dev_info, device_info_data):
9292

9393
wanted_GUID = GUID(ctypesInternalGUID(instance_id_buffer))
9494

95+
device_address = ctypes.c_int32()
96+
res = setup_api.SetupDiGetDevicePropertyW(h_dev_info, ctypes.byref(device_info_data),
97+
ctypes.byref(DEVPKEY.Device.DeviceAddress),
98+
ctypes.byref(prop_type), ctypes.byref(device_address),
99+
ctypes.sizeof(ctypes.c_int32), ctypes.byref(required_size), 0)
100+
95101
hkey_path = "SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_{}&PID_{}".format(vendor_id, product_id)
96102
try:
97103
vendor_product_hkey = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, hkey_path)
@@ -120,9 +126,15 @@ def get_serial_serial_no(vendor_id, product_id, h_dev_info, device_info_data):
120126
winreg.CloseKey(device_hkey)
121127
continue
122128

129+
try:
130+
queried_address = winreg.QueryValueEx(device_hkey, "Address")[0]
131+
except EnvironmentError as err:
132+
winreg.CloseKey(device_hkey)
133+
continue
134+
123135
winreg.CloseKey(device_hkey)
124136

125-
if queried_container_id.lower() == str(wanted_GUID).lower():
137+
if queried_container_id.lower() == str(wanted_GUID).lower() and queried_address == device_address.value:
126138
winreg.CloseKey(vendor_product_hkey)
127139
return serial_number
128140

0 commit comments

Comments
 (0)