Author Topic: Detecting a device based on PID/VID/SID?  (Read 19583 times)

goodnewsjim

  • Frequent Contributor
  • ****
  • Posts: 52
Detecting a device based on PID/VID/SID?
« on: December 03, 2013, 04:11:24 pm »
Hello,

The example given in WINUSB_CS detects a device automatically.

I want to use several(at once)nearly identical microcontrollers except for their SID.

Can I be given a tip on how to specify connecting to the one with the right PID/VID/SID?
Or even better, making an array of connections just by looking at all the SIDs that show up based on looking for a specific PID/VID?

Any help is appreciated, even clues to get me moving would be good.  My C++ version looked for a specific vid/pid, but the C# one does things a bit too automatically for what I need.

Thank you for reading
,Jim

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Detecting a device based on PID/VID/SID?
« Reply #1 on: December 03, 2013, 05:29:38 pm »
You can use WinUsb_ControlTransfer to request a device descriptor (containing the Vendor ID and Product ID) and a serial number string (if that's what you mean by SID):

http://msdn.microsoft.com/en-us/library/windows/hardware/ff540219%28v=vs.85%29.aspx

goodnewsjim

  • Frequent Contributor
  • ****
  • Posts: 52
Re: Detecting a device based on PID/VID/SID?
« Reply #2 on: December 04, 2013, 12:37:09 pm »
Thank you again Jan.  I'll look into it :)
You have such quick replies too...

goodnewsjim

  • Frequent Contributor
  • ****
  • Posts: 52
Re: Detecting a device based on PID/VID/SID?
« Reply #3 on: December 04, 2013, 04:20:19 pm »
Jan,

Hmmm...
I do not understand how to read the Microsoft documentation. 
Also there are no examples on Microsoft's site.
I'm kinda lost...

Would you believe I actually have a coded up micro controller which handles USB requests, does some stuff, and fires back communication on USB?  I'm not a totally clueless person in general :)  This(VID/PID/SID specific communication) is actually the last thing I need before my project is done.

Random thoughts:
What would the line look like I'm trying to write? Does it go before or after finding a general connection?
If I have multiple devices plugged into my computer with different SID and same VID/PID, will I be able to pick which device the data gets sent to?

Again, I'm pretty lost, but this is the very last thing I need to do dealing with USB.
Whatever help I can get would be welcome.

,Jim

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Detecting a device based on PID/VID/SID?
« Reply #4 on: December 04, 2013, 09:41:02 pm »
A search on

WinUsb_ControlTransfer C#

brings up this example:

http://winusbnet.googlecode.com/svn-history/r15/trunk/WinUSBNet/API/WinUSBDevice.cs

You would find and open a handle to a WinUSB device as usual, then do the control transfers to find out if you have the device you want.

You will also need to understand the contents of the descriptors. See the USB 2.0 specification, my USB Complete book, or search the Internet.

goodnewsjim

  • Frequent Contributor
  • ****
  • Posts: 52
Re: Detecting a device based on PID/VID/SID?
« Reply #5 on: December 05, 2013, 02:12:03 pm »
Thanks Jan,

I hope I'm not wearing out your patience with my questions.  I may be done forever with working with USB code if I could get the right handle based on VID/PID/SID.  I think the syntax would be maybe one or two lines, but I just don't know it.  I looked through that example you gave and didn't see any vendor,product or serial IDs.  It is helpful that you told me the algorithm: 1) Get handle 2) Check if VID/PID/SID match... I'm just not exactly sure how to check the VID of a handle I have.  Also I wonder if another handle is gotten, will it be a new handle(in case of two devices attached)?

Whether or not I'll get around to reading your book, I might just end up buying it anyway to be cool.  You helped me a ton in the forum, and your source code provided for free.

,Jim

goodnewsjim

  • Frequent Contributor
  • ****
  • Posts: 52
Re: Detecting a device based on PID/VID/SID?
« Reply #6 on: December 06, 2013, 01:43:06 pm »
So I think I should be looking in FindMyDevice.  Where is Vendor ID found in a data structure, and where should I check for it in this code?

private Boolean FindMyDevice()
      {
         Boolean deviceFound;
         String devicePathName = "";
         Boolean lastDevice;
         Boolean success;

         try
         {
            if (!(myDeviceDetected))
            {

               //  Convert the device interface GUID String to a GUID object:

               System.Guid winUsbDemoGuid =
                  new System.Guid(WINUSB_DEMO_GUID_STRING);

               // Fill an array with the device path names of all attached devices with matching GUIDs.

               deviceFound = myDeviceManagement.FindDeviceFromGuid
                  (winUsbDemoGuid,
                  ref devicePathName);

               if (deviceFound == true)
               {
                  success = myWinUsbDevice.GetDeviceHandle(devicePathName);
                        
                  if (success)
                  {
                     lstResults.Items.Add("Device detected:");

                     ScrollToBottomOfListBox();

                     myDeviceDetected = true;

                     // Save DevicePathName so OnDeviceChange() knows which name is my device.

                     myDevicePathName = devicePathName;
                  }
                  else
                  {
                     // There was a problem in retrieving the information.

                     myDeviceDetected = false;
                     myWinUsbDevice.CloseDeviceHandle();
                  }
               }

               if (myDeviceDetected)
               {

                  // The device was detected.
                  // Register to receive notifications if the device is removed or attached.

                  success = myDeviceManagement.RegisterForDeviceNotifications
                     (myDevicePathName,
                     frmMy.Handle,
                     winUsbDemoGuid,
                     ref deviceNotificationHandle);

                  if (success)
                  {
                     myWinUsbDevice.InitializeDevice();

                     //Commented out due to unreliable response from WinUsb_QueryDeviceInformation.                            
                     //DisplayDeviceSpeed();
                  }
               }
               else
               {
                  lstResults.Items.Add("Device not found.");
                  //cmdSendandReceiveViaBulkTransfers.Enabled = true;
                  //cmdSendAndReceiveViaInterruptTransfers.Enabled = true;
               }
            }
            else
            {
               lstResults.Items.Add("Device detected.");
            }

            ScrollToBottomOfListBox();

            return myDeviceDetected;

         }
         catch (Exception ex)
         {
            throw;
         }
      }

goodnewsjim

  • Frequent Contributor
  • ****
  • Posts: 52
Re: Detecting a device based on PID/VID/SID?
« Reply #7 on: December 06, 2013, 01:52:26 pm »
Ok guys, I found the VID is in the string:
devicePathName

I should be fine now.