简体   繁体   中英

USB Port binding to application - Windows

I wrote application that communicates with specific USB devices (Video encoder). The device itself does not allow to get the device ID, so if I connect two devices I can not identify them. Solution now is to have as many PCs as I have USB devices (encoders)

I would like to know how to be able to dedicate one USB port per application.

To ilustrate how I have now

[Device 1] - [PC 1] - [USB Port 1] - [Application 1]
[Device 2] - [PC 2] - [USB Port 1] - [Application 1]
[Device 3] - [PC 3] - [USB Port 1] - [Application 1]

How I would like to have

[Device 1] \            / [USB Hub / USB Port 1] - [Application 1]
[Device 2]  -- [PC 1] --  [USB Hub / USB Port 2] - [Application 2]
[Device 3] /            \ [USB Hub / USB Port 3] - [Application 3]

I would like to create like this (pseudocode)

main(){

    attachApplicationToUsbPort('USB Hub / USB Port 1')

    CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);

    result = CoCreateInstance(CLSID_CBMDStreamingDiscovery, NULL, CLSCTX_ALL, IID_IBMDStreamingDiscovery, (void**)&m_streamingDiscovery);

    ...
    ...
}

Now I would open three applications and assign each application to specific port.

I don't know what kind of APIs you are using to access that USB device, but every device in Windows has a unique identifier called a Device Instance ID . For a USB device without a serial number, the Device Instance ID will look something like this:

USB\VID_51AD&PID_7D8B\6&11A23516&18&0000

I believe the part after the second back slash should be a function of what port you plug the device into, and I think it should not change when you reboot. You should be able to use that to assign a process to a specific USB device, as long as that device is always plugged into the same port.

I think that's the easy way to do it. On the other hand, if you really want to start dealing with USB hubs and ports in your code, you might take a look at the code for USBView from Microsoft.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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