7

I need to iterate through the ports connected to the computer, and find a specific device. Please have a look at the below image:

enter image description here

You can see there are 4 device names with the same vendor and product id. but, i need to find the the port of the first one, which is highlighted in blue colour. it seems like the only difference they have is friend name (the description is friend name).

What is the easiest way of achieving this in C#.net? I have done this in 'qt' and I need to know how this can be done in C#, .net framework 4, using vs 2010 professional. I have gone through questions like this but as you can see, they are no help to my situation.

Community
  • 1
  • 1
PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • possible duplicate of [Get List of connected USB Devices](http://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices) – Cᴏʀʏ Mar 27 '14 at 13:46

1 Answers1

9

If you use libusbdotnet you should be able to do something like this:

public static void RetrieveUSBDevices(int vid, int pid)
{
        var usbFinder = new UsbDeviceFinder(vid, pid);
        var usbDevices = new UsbRegDeviceList();
        usbDevices = usbDevices.FindAll(usbFinder);
}

You should then be able to iterate the usbDevices and check for the correct FullName. Haven´t tested this though so it´s theoretical.

UPDATE: Tried that and it worked fine - what is the problem? Why vote down due to your own incompetence?

This would also work:

private static void Method()
{
var list = GetMyUSBDevices();
//Iterate list here and use Description to find exact device
}


private static List<UsbDevice> GetMyUSBDevices()
{
    var vid = 32903;
    var pid = 36;

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

    var usbDevice = 
        (from ManagementBaseObject device in collection 
        select new UsbDevice(
        (string) device.GetPropertyValue("DeviceID"), 
        (string) device.GetPropertyValue("Description"))).ToList();

    var devices = new List<UsbDevice>();

    foreach (var device in collection)
    {
        devices.Add(new UsbDevice(
        (string)device.GetPropertyValue("DeviceID"),
        (string)device.GetPropertyValue("Description")
        ));
    }

    collection.Dispose();

    return (from d in devices where d.DeviceId.Contains("VID_") && d.DeviceId.Contains("PID_") && d.PID.Equals(pid) && d.VID.Equals(vid) select d).ToList();
}

public class UsbDevice
{
    public UsbDevice(string deviceId, string description)
    {
        DeviceId = deviceId;
        Description = description;
    }

    public string DeviceId { get; private set; }
    public string Description { get; private set; }

    public int VID 
    {
        get { return int.Parse(GetIdentifierPart("VID_"), System.Globalization.NumberStyles.HexNumber); }
    }

    public int PID
    {
        get { return int.Parse(GetIdentifierPart("PID_"), System.Globalization.NumberStyles.HexNumber); }
    }

    private string GetIdentifierPart(string identifier)
    {
        var vidIndex = DeviceId.IndexOf(identifier, StringComparison.Ordinal);
        var startingAtVid = DeviceId.Substring(vidIndex + 4);
        return startingAtVid.Substring(0, 4);
    }
}
Marcus
  • 8,230
  • 11
  • 61
  • 88
  • Thanks a lot for the reply. I have not used us lib. The second method it seems like it is not giving what I was looking for. I can't get the friend name there isn't it – PeakGen Aug 13 '13 at 10:16
  • Anyway in case of libusb, does it has a special setup system or can it be easily used like any c# lib? Please help – PeakGen Aug 13 '13 at 10:19
  • About down votes, someone has downvoted my question as well :( – PeakGen Aug 13 '13 at 10:22
  • The first method may not work depending on hardware - I guess it is a little bit buggy but the second should work and description should enable you to compare the different devices. You can also try to exchange Win32_USBHub for Win32_USBDevice or Win32_PnPEntity. Another idea is that you try device.GetPropertyValue("Name"). – Marcus Aug 13 '13 at 10:22
  • Another option that I´ve used before is the D2XX FTDI driver - http://www.ftdichip.com/Drivers/D2XX.htm which is a DLL library that you can download and access the USB as if it was a com port. – Marcus Aug 13 '13 at 10:27
  • Thanks. Will try and let you know – PeakGen Aug 13 '13 at 19:15
  • I downloaded libusbdotnet but I can't find any API? – PeakGen Aug 15 '13 at 04:25
  • Anyway, the Product ID and Vendor ID contains letters, so how can I pass them as int ? – PeakGen Aug 15 '13 at 05:02
  • PID and VID are hexadecimal numbers 0-9A-F.If you use the code provided you can see that this is taken care of in the VID and PID properties - return int.Parse(GetIdentifierPart("VID_"), System.Globalization.NumberStyles.HexNumber); – Marcus Aug 15 '13 at 06:11
  • how get port name? ex com1 – AliRam Aug 16 '22 at 09:20