Using System.IO.Ports;
Using System.Management; After adding this sentence, there is still a red wavy line hint that you need to manually add the DLL reference (VS2012)
[ComVisibleAttribute (TRUE)]//Add COM visibility under namespaces
String tmpstr1 = ""; String tmpport = ""; list<string> usefulport = new list<string> (); private void Openacclecomprot () {//com lookup/objectquery query = new ObjectQuery ("SELECT * FROM Win32_serialport "); Win32_USBControllerDevice ManagementObjectSearcher searcher = new ManagementObjectSearcher (query); Usefulport.clear (); int loadcount = 0; Find/foreach (ManagementObject obj2 in searcher) based on the feature you set. 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; } } }
The most basic USB device structure
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;} }
The structure of USB serial port
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;};
Find serial-objectquery ("SELECT * from Win32_serialport")