查找串口-ObjectQuery("SELECT * FROM Win32_SerialPort")

2021-05-02 18:29

阅读:671

标签:命名   nbsp   omv   ros   mes   user   library   query   pid   

技术分享

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

  

 

查找串口-ObjectQuery("SELECT * FROM Win32_SerialPort")

标签:命名   nbsp   omv   ros   mes   user   library   query   pid   

原文地址:http://www.cnblogs.com/zhangkun35268/p/7760524.html


评论


亲,登录后才可以留言!