作者:FC青春一路有你 | 来源:互联网 | 2023-09-17 10:04
usingSystem.IO.Ports;usingSystem.Management;加了这句之后,仍然有红色波浪线提示,说明需要手动添加dll引用(vs2012)[ComVis
using System.IO.Ports;
using System.Management; //加了这句之后,仍然有红色波浪线提示,说明需要手动添加dll引用(vs2012)
[ComVisibleAttribute(true)] //在命名空间下添加com可见性
String tmpstr1 = "";
string tmpPort = "";
List usefulPort = new List();
private void openAccleComprot()
{
//COM查找/
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_SerialPort"); // Win32_USBControllerDevice
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
usefulPort.Clear();
int loadCount = 0;
//根据自己设定的特征查找/
foreach (ManagementObject obj2 in searcher.Get())
{
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0010"))
{
//MessageBox.Show(obj2.Properties["Name"].Value.ToString());
tmpstr1 = obj2.Properties["Name"].Value.ToString();
usefulPort.Add(tmpstr1);
loadCount++;
}
}
string[] allports = SerialPort.GetPortNames();
foreach (string port in allports)
{
if (tmpstr1.Contains(port))
{
//MessageBox.Show(port);
tmpPort = port;
}
}
}
最基本的USB设备结构
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
}
USB串口的结构
https://msdn.microsoft.com/en-us/library/aa394504(v=vs.85).aspx
[Dynamic, provider("CIMWin32"), UUID("{98C7E2C7-D592-11d2-B355-00105A0A323A}"), AMENDMENT]
class Win32_USBController : CIM_USBController
{
uint16 Availability;
string Caption;
uint32 ConfigManagerErrorCode;
boolean ConfigManagerUserConfig;
string CreationClassName;
string Description;
string DeviceID;
boolean ErrorCleared;
string ErrorDescription;
datetime InstallDate;
uint32 LastErrorCode;
string Manufacturer;
uint32 MaxNumberControlled;
string Name;
string PNPDeviceID;
uint16 PowerManagementCapabilities[];
boolean PowerManagementSupported;
uint16 ProtocolSupported;
string Status;
uint16 StatusInfo;
string SystemCreationClassName;
string SystemName;
datetime TimeOfLastReset;
};