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

Commit

Permalink
Merge pull request #362 from NickSamNam/master
Browse files Browse the repository at this point in the history
win32 serial enumeration fix
  • Loading branch information
jornbh authored Jul 6, 2022
2 parents 7007fe2 + 77e2e69 commit 6daec79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions nordicsemi/lister/windows/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Device:
"""DEVPKEY_Device_xxx constants"""
ContainerId = DevicePropertyKey('{8c7ed206-3f8a-4827-b3ab-ae9e1faefc6c}', 2,
'DEVPKEY_Device_ContainerId')
DeviceAddress = DevicePropertyKey('{a45c254e-df1c-4efd-8020-67d146a850e0}', 30,
'DEVPKEY_Device_Address')

# noinspection SpellCheckingInspection
DIGCF_DEFAULT = DiGetClassDevsFlags.Default
Expand Down
14 changes: 13 additions & 1 deletion nordicsemi/lister/windows/lister_win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def get_serial_serial_no(vendor_id, product_id, h_dev_info, device_info_data):

wanted_GUID = GUID(ctypesInternalGUID(instance_id_buffer))

device_address = ctypes.c_int32()
res = setup_api.SetupDiGetDevicePropertyW(h_dev_info, ctypes.byref(device_info_data),
ctypes.byref(DEVPKEY.Device.DeviceAddress),
ctypes.byref(prop_type), ctypes.byref(device_address),
ctypes.sizeof(ctypes.c_int32), ctypes.byref(required_size), 0)

hkey_path = "SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_{}&PID_{}".format(vendor_id, product_id)
try:
vendor_product_hkey = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, hkey_path)
Expand Down Expand Up @@ -120,9 +126,15 @@ def get_serial_serial_no(vendor_id, product_id, h_dev_info, device_info_data):
winreg.CloseKey(device_hkey)
continue

try:
queried_address = winreg.QueryValueEx(device_hkey, "Address")[0]
except EnvironmentError as err:
winreg.CloseKey(device_hkey)
continue

winreg.CloseKey(device_hkey)

if queried_container_id.lower() == str(wanted_GUID).lower():
if queried_container_id.lower() == str(wanted_GUID).lower() and queried_address == device_address.value:
winreg.CloseKey(vendor_product_hkey)
return serial_number

Expand Down

0 comments on commit 6daec79

Please sign in to comment.