Getting friendly device names in python

后端 未结 6 1089
夕颜
夕颜 2021-01-04 10:29

I have an 2-port signal relay connected to my computer via a USB serial interface. Using the pyserial module I can control these relays with ease. However, this is based on

6条回答
  •  清酒与你
    2021-01-04 10:58

    I know this is an older post, but I was struggling with it today. Ultimately I used the wmi library for python as I'm on a Windows machine (sorry, I know my answer only applies to Windows, but maybe it'll help someone).

    Install the package using pip first:

    pip install wmi
    

    then

    import wmi
    c = wmi.WMI()
    wql = "Select * From Win32_USBControllerDevice"
    for item in c.query(wql):
        print item.Dependent.Caption
    

    Should result with something like:

    USB Root Hub
    USB Root Hub
    Prolific USB-to-Serial Comm Port (COM9) USB Root Hub
    USB Root Hub
    USB Composite Device
    USB Video Device USB Audio Device
    USB Root Hub
    ...snip...

    In this case, you'd have to string parse the Caption to find the COM port. You can also take a look at just the item. Dependent object to see other attributes of the USB device beside Caption that you may find relevant:

    instance of Win32_PnPEntity
    {
        Caption = "USB Root Hub";
        ClassGuid = "{36fc9e60-c465-11cf-8056-444553540000}";
        ConfigManagerErrorCode = 0;
        ConfigManagerUserConfig = FALSE;
        CreationClassName = "Win32_PnPEntity";
        Description = "USB Root Hub";
        DeviceID = "USB\\ROOT_HUB\\4&32F13EF0&1";
        HardwareID = {"USB\\ROOT_HUB&VID8086&PID3A36&REV0000",     
                    "USB\\ROOT_HUB&VID8086&PID3A36", "USB\\ROOT_HUB"};
        Manufacturer = "(Standard USB Host Controller)";
        Name = "USB Root Hub";
        PNPDeviceID = "USB\\ROOT_HUB\\4&32F13EF0&1";
        Service = "usbhub";
        Status = "OK";
        SystemCreationClassName = "Win32_ComputerSystem";
        SystemName = "001fbc0934d1";
    };
    

提交回复
热议问题