繁体   English   中英

查找 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

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

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

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

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

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

我会尝试从USB设备中提取location information (与设备管理器中的相同)...我不使用C#WMI 进行编码,但您应该能够使用setupapi.h获取此类信息,它是 winapi 的一部分(我认为)我在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因此将其更改为您可以使用的任何字符串类型。

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

hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);
static int GetPhysicalPort()
    {
        try
        {
            devices = new List<USBDeviceInfo>();
            
            ManagementObjectCollection collection;
            using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPSignedDriver WHERE DeviceId LIKE 'USB\\VID%' AND Description = 'USB Mass Storage Device' "))
            {
                collection = searcher.Get();
                searcher.Dispose();
            }
                

            foreach (var device in collection)
            {
                devices.Add(new USBDeviceInfo(
                (string)device.GetPropertyValue("DeviceId"),
                (string)device.GetPropertyValue("Description"),
                (string)device.GetPropertyValue("Location")
                ));
            }

            collection.Dispose();
           
            string LastAdded = devices[0].Location.Substring(6, 4);
            Console.WriteLine(LastAdded);
            return Convert.ToInt32(LastAdded);
        }

        catch (Exception e)
        {
            Console.WriteLine(e);
            return 0;
        }
    }


 class USBDeviceInfo
{
    public USBDeviceInfo(string deviceID, string Description, string location)
    {
        this.DeviceID = deviceID;
        this.Desc = Description;
        this.Location = location;
        
       
    }
    public string DeviceID   { get;}
    public string  Desc      { get;}
    public string Location   { get;}
}

我正在使用此方法来获取您要求的插槽。 事实上,由于我的要求,我使用了最后一个插入 USB 的插槽。 您可以调试并查看 USBDeviceInfo 类的内容,然后将其用于您自己的目的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM