【问题标题】:How to find usb devices in c#?如何在 C# 中找到 USB 设备?
【发布时间】:2013-08-14 16:10:07
【问题描述】:

我需要遍历连接到计算机的端口,并找到特定的设备。请看下图:

您可以看到有 4 个设备名称具有相同的供应商和产品 ID。但是,我需要找到第一个端口,它以蓝色突出显示。似乎他们唯一的区别是朋友的名字(描述是朋友的名字)。

在 C#.net 中实现这一目标的最简单方法是什么?我已经在“qt”中完成了这项工作,我需要知道如何使用 vs 2010 专业版在 C#、.net 框架 4 中完成这项工作。我已经解决了this 之类的问题,但正如您所见,它们对我的情况没有帮助。

【问题讨论】:

标签: c# .net visual-studio-2010 usb


【解决方案1】:

如果您使用libusbdotnet,您应该可以执行以下操作:

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

然后您应该能够迭代 usbDevices 并检查正确的FullName。虽然还没有测试过,所以它是理论上的。

更新: 试过了,效果很好 - 有什么问题?为什么要因为自己的无能而投反对票?

这也可以:

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);
    }
}

【讨论】:

  • 非常感谢您的回复。我没有用过我们的lib。第二种方法似乎没有给出我想要的东西。我无法得到朋友的名字,不是吗
  • 无论如何,对于 libusb,它是否有一个特殊的设置系统,或者它可以像任何 c# lib 一样轻松使用吗?请帮忙
  • 关于否决票,有人也否决了我的问题:(
  • 第一种方法可能无法工作,具体取决于硬件 - 我想它有点错误,但第二种方法应该可以工作,并且描述应该使您能够比较不同的设备。您也可以尝试将 Win32_USBHub 换成 Win32_USBDevice 或 Win32_PnPEntity。另一个想法是你试试 device.GetPropertyValue("Name").
  • 我之前使用的另一个选项是 D2XX FTDI 驱动程序 - ftdichip.com/Drivers/D2XX.htm 这是一个 DLL 库,您可以下载和访问 USB,就像它是一个 com 端口一样。
猜你喜欢
  • 2021-11-07
  • 1970-01-01
  • 2011-01-17
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多