我试图通过USB将设备连接到USB而不是串口转USB。使用SerialPort类是不合适的。有没有办法用c#连接?我试过使用LibUsbDotNet库,但程序找不到我电脑上连接的机器。
Using Cypress EZ USB driver to connect to the machine
有人可以帮我解决问题吗?感谢
private void comboBox1_DropDown(object sender, EventArgs e)
{
mRegDevices = UsbDevice.AllDevices;
foreach (UsbRegistry regDevice in mRegDevices)
{
// add the Vid, Pid, and usb device description to the dropdown display.
// NOTE: There are many more properties available to provide you with more device information.
// See the LibUsbDotNet.Main.SPDRP enumeration.
string sItem = String.Format("Vid:{0} Pid:{1} {2}",
regDevice.Vid.ToString("X4"),
regDevice.Pid.ToString("X4"),
regDevice.FullName);
comboBox1.Items.Add(sItem);
}
}
上面的代码来自LibUsbDotNet的示例代码。列出ComboBox中的所有可用设备但未显示任何内容。
答案 0 :(得分:0)
下面的代码将列出您的PC检测到的所有通用串行总线控制器。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace CsharpUSBConnetctionInfo
{
class Program
{
static void Main(string[] args)
{
foreach (var usbDevice in usbDevices)
{
Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}", usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
}
Console.ReadLine();
}
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub")) collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeivceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeivceID;
this.Description = description;
}
public string DeviceID
{
get;
private set;
}
public string PnpDeviceID
{
get;
private set;
}
public string Description
{
get;
private set;
}
}
}