How to get more information about a USB device in Windows?

123 Views Asked by At

I have a USB device (BBC micro:bit v2), which is presented by Chrome with description "BBC micro:bit CSMIS-DAP" when connected to in a webapp via Web Serial API, even in Windows.

The problem is that in Windows I'm not able to retrieve this description with programmatic means (e.g. pyserial library, which works nicely in macOS) and I also don't see anything about "micro:bit" when I look the details of the corresponding COM-port under Device Manager or when I execute Get-CimInstance -Class Win32_SerialPort? in PowerShell. The Description is simply reported as "USB Serial Device", Manufacturer as "Microsoft".

My ultimate goal is to retrieve this description (correct product / vendor information) in a Python program, but I'd be thankful for any hints about where to look for.

How does Chrome get to now the proper description of the device?

1

There are 1 best solutions below

2
Tino D On

The easiest way would be to use the pywinusb module, as it is quite nice and concise:

import pywinusb.hid as hid

devices = hid.find_all_hid_devices() # find all devices
for device in devices: # loop through all HID devices, I have two ports
    print(f"Device Path: {device.device_path}") # path of device
    print(f"Vendor ID: {device.vendor_id}") # vendor ID
    print(f"Product ID: {device.product_id}") # product ID
    print(f"Description: {device.product_name}") # product name
    print("______________________________________") # for niceness

These are the results of my two ports:

Device Path: \\?\hid#elan06c6&col02#5&6596592&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
Vendor ID: 1267
Product ID: 12691
Description: HIDI2C Device
______________________________________
Device Path: \\?\hid#elan06c6&col04#5&6596592&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}
Vendor ID: 1267
Product ID: 12691
Description: HIDI2C Device
______________________________________

V2.0: I spent some time this morning learning about the difference between what I just posted and a possible correct answer to this question. I got to this change by using win32com.client:

import win32com.client

wmi = win32com.client.GetObject ("winmgmts:")
for usb in wmi.InstancesOf ("Win32_USBHub"): # get devices that are instances of win32_usbhub
    print(usb.DeviceID) # print it out
    print("________________________________________________________________")

Results:

USB4\ROOT_DEVICE_ROUTER&VID_8086&PID_A73E\4&24EA304D&0&0
________________________________________________________________
USB\VID_5986&PID_215D\01.00.00
________________________________________________________________
USB\ROOT_HUB30\4&BF357F&0&0
________________________________________________________________
USB\ROOT_HUB30\4&3D9EDED&0&0
________________________________________________________________

V3.0: so my final answer would be like this:

import win32com.client # the win32com client
import re # regex
wmi = win32com.client.GetObject ("winmgmts:") # get Windows Management Instrumentation Service
for usb in wmi.InstancesOf ("Win32_USBHub"): # get devices that are instances of win32_usbhub
    DeviceID = usb.DeviceID # store device id in variable
    DeviceDescription = usb.Description # store description in variable
    print("Raw results: "+DeviceID) # print out raw results
    if ("VID" in DeviceID) and ("PID" in DeviceID): # check if VID AND PID are mentioned
        pattern = r'VID_(\w{4}).*PID_(\w{4})' # define the pattern of 4 characters after VID and PID
        match = re.search(pattern, DeviceID) # search for hte pattern in the raw DeviceID
        vid = match.group(1) # VID is the first group
        pid = match.group(2) # PID is the second group
        print(DeviceDescription) # print out the description
        print("VID:", vid) # print out the vid
        print("PID:", pid) # print out the pid
    else: # if no VID AND PID are found
        print("No ID detected...") # print out that no VID AND PID were found
    print("________________________________________________________________") # for niceness

I checked with my device manager and I could find everything there as well.

The results are like this:

Raw results: USB4\ROOT_DEVICE_ROUTER&VID_8086&PID_A73E\4&24EA304D&0&0
USB4-Stammrouter
VID: 8086
PID: A73E
________________________________________________________________
Raw results: USB\VID_5986&PID_215D\01.00.00
USB-Verbundgerät
VID: 5986
PID: 215D
________________________________________________________________
Raw results: USB\ROOT_HUB30\4&BF357F&0&0
No ID detected...
________________________________________________________________
Raw results: USB\ROOT_HUB30\4&3D9EDED&0&0
No ID detected...
________________________________________________________________

Hope this helps you further