查找 USB 连接或未连接的 USB 集线器(10 个插槽)的特定插槽编号.我想获得 USB 连接或未连接的特定插槽 [英] Find specific slot no of the USB Hub(10 slots) where USB is connected or not. I want to get the specific slot no where USB is connected or not

查看:45
本文介绍了查找 USB 连接或未连接的 USB 集线器(10 个插槽)的特定插槽编号.我想获得 USB 连接或未连接的特定插槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 10 个 USB 插槽组成的 USB 集线器连接到我的 USB 端口.我想将 USB 设备连接到特定端口.
示例:两个 USB 连接在插槽 3 和插槽 7.因此,我想要一个列表,其中显示插槽 3 和插槽 7 有 USB,其余插槽为空.

I have a USB Hub of 10 USB slots connected to my USB Port. I want to get the USB device connected to the specific port.
Example: Two USB's are connected at Slot 3 and Slot 7. So, I want a list which will show Slot 3 and Slot 7 have USB and rest slot are empty.

我尝试过使用 WMI 查询 Win32_USBHub.但在这里我只得到 6 个设备 ID 而不是 10 个.我使用通用 VID 作为设备 ID 来区分端口.

I have tried using WMI Query Win32_USBHub. But here I am only getting 6 device IDs and not 10. I am differentiating the ports using common VID for the Device ID.

但即使在将 USB 连接到特定端口后仍然如此.我想获取它们连接到 USBHub 的相应插槽.

But still even after getting the USB's connected to the specific port. I want to get their corresponding slot in which they are connected to USBHub.

我无法识别连接 USB 的插槽以及该插槽是空的.

I am not able to identify the slot in which the USB is connected and where the slot is empty.

ManagementObjectCollection collection;
var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub");
collection = searcher.Get();

推荐答案

我会尝试从 USB 设备中提取位置信息(与设备管理器中的相同)...我不会在 C#WMI 中编码,但您应该能够使用 setupapi.h 获取此类信息,它是一部分winapi(我认为)我在 C++/VCL 中这样做:

I would try to extract location information from the USB device (the same as in device manager)... I do not code in C# nor WMI but you should be able to obtain this kind of info with setupapi.h which is part of winapi (I think) I do it like this in C++/VCL:

#include <setupapi.h>
bool USBinfo()
    {
    int i,n;
    AnsiString s,txt="";
    DWORD dwSize,dwPropertyRegDataType;
    HDEVINFO hDevInfo;
    SP_DEVINFO_DATA DeviceInfoData;
    TCHAR szDesc[1024];

    // List all connected USB devices
    hDevInfo = SetupDiGetClassDevs(NULL, TEXT("USB"), NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);
    if (hDevInfo == INVALID_HANDLE_VALUE) return false;
    for (i=0;;i++)
        {
        DeviceInfoData.cbSize = sizeof(DeviceInfoData);
        if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData)) break;
        SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC,&dwPropertyRegDataType, (BYTE*)szDesc,sizeof(szDesc),&dwSize);
        s=szDesc; n=48; while (s.Length()<n) s+=" "; if (s.Length()>n) s=s.SubString(1,n); txt+=s+" "; // this just set constant string size to allign the columns to n chars
        SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID,&dwPropertyRegDataType, (BYTE*)szDesc,sizeof(szDesc),&dwSize);
        s=szDesc; if (s=="USB\\VID_????&PID_????REV_????")
            {
            // here you can do custom stuff for specific VID,PID just change the ???? in above line to your specific VID,PID,REV
            }
        s=szDesc; n=64; while (s.Length()<n) s+=" "; if (s.Length()>n) s=s.SubString(1,n); txt+=s+" ";
        SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_LOCATION_INFORMATION,&dwPropertyRegDataType, (BYTE*)szDesc,sizeof(szDesc),&dwSize);
        s=szDesc; n=64; while (s.Length()<n) s+=" "; if (s.Length()>n) s=s.SubString(1,n); txt+=s+" ";
        txt+="\r\n";
        }
    Main->mm_log->Lines->Add(txt); // this just output txt string to memo
    return true;
    }

这是我机器上的输出:

USB Root Hub                                     USB\ROOT_HUB&VID1022&PID7807&REV0011                             USB\ROOT_HUB&VID1022&PID7807&REV0011                             
USB Root Hub                                     USB\ROOT_HUB&VID1022&PID7807&REV0011                             USB\ROOT_HUB&VID1022&PID7807&REV0011                             
USB Root Hub                                     USB\ROOT_HUB&VID1022&PID7809&REV0011                             USB\ROOT_HUB&VID1022&PID7809&REV0011                             
USB Root Hub                                     USB\ROOT_HUB20&VID1022&PID7808&REV0011                           USB\ROOT_HUB20&VID1022&PID7808&REV0011                           
USB Root Hub                                     USB\ROOT_HUB20&VID1022&PID7808&REV0011                           USB\ROOT_HUB20&VID1022&PID7808&REV0011                           
USB Composite Device                             USB\VID_048D&PID_9006&REV_0200                                   Port_#0001.Hub_#0004                                             
IT9135 BDA Device                                USB\VID_048D&PID_9006&REV_0200&MI_00                             0000.0013.0002.001.000.000.000.000.000                           
USB Input Device                                 USB\VID_048D&PID_9006&REV_0200&MI_01                             0000.0013.0002.001.000.000.000.000.000                           
Canon LiDE 30                                    USB\VID_04A9&PID_220E&REV_0100                                   Port_#0005.Hub_#0001                                             
American Power Conversion USB UPS                USB\VID_051D&PID_0002&REV_0106                                   Port_#0001.Hub_#0001                                             
USB Input Device                                 USB\Vid_093A&Pid_2510&Rev_0100                                   USB Optical Mouse                                                
USB Input Device                                 USB\VID_413C&PID_2107&REV_0115                                   Port_#0002.Hub_#0001                                             

如您所见,最后一列(第 3 列)包含您想要的信息.查看 setupapi.h 中所有你可以使用的 SPDRP_ 定义...... VCL 中唯一使用的是 AnsiString 因此将其更改为您可以使用的任何字符串类型.

As you can see the last column (3th) holds the info you want. Look inside setupapi.h for all the SPDRP_ defines you can use ... The only thing used from VCL is AnsiString so change it to any string type you have at your disposal.

这不仅限于USB.如果您想要所有设备,请将 TEXT("USB") 搜索参数更改为 NULL

This is not restricted to USB. If you want all the devices then change TEXT("USB") search parameter to NULL

hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);

这篇关于查找 USB 连接或未连接的 USB 集线器(10 个插槽)的特定插槽编号.我想获得 USB 连接或未连接的特定插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆