public static List<COMPortFinder> GetCOMPortsInfo() { List<COMPortFinder> COMPortFinderList = new List<COMPortFinder>(); ConnectionOptions options = ProcessConnection.ProcessConnectionOptions(); ManagementScope connectionScope = ProcessConnection.ConnectionScope(Environment.MachineName, options, @"\root\CIMV2"); ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0"); ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery); using (comPortSearcher) { string caption = null; foreach (ManagementObject obj in comPortSearcher.Get()) { if (obj != null) { object captionObj = obj["Caption"]; if (captionObj != null) { caption = captionObj.ToString(); if (caption.Contains("(COM")) { COMPortFinder COMPortFinder = new COMPortFinder(); COMPortFinder.Name = caption.Substring(caption.LastIndexOf("(COM")).Replace("(", string.Empty).Replace(")", string.Empty); COMPortFinder.Description = caption; COMPortFinderList.Add(COMPortFinder); } } } } } return COMPortFinderList; }
public static List <COMPortFinder> GetCOMPortsInfo() { List <COMPortFinder> COMPortFinderList = new List <COMPortFinder>(); ConnectionOptions options = ProcessConnection.ProcessConnectionOptions(); ManagementScope connectionScope = ProcessConnection.ConnectionScope(Environment.MachineName, options, @"\root\CIMV2"); ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0"); ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery); using (comPortSearcher) { string caption = null; foreach (ManagementObject obj in comPortSearcher.Get()) { if (obj != null) { object captionObj = obj["Caption"]; if (captionObj != null) { caption = captionObj.ToString(); if (caption.Contains("(COM")) { COMPortFinder COMPortFinder = new COMPortFinder(); COMPortFinder.Name = caption.Substring(caption.LastIndexOf("(COM")).Replace("(", string.Empty).Replace(")", string.Empty); COMPortFinder.Description = caption; COMPortFinderList.Add(COMPortFinder); } } } } } return(COMPortFinderList); }
public override void Start() { string embedDriverArgs = moduleInfo.Args()[0]; string comPortName = embedDriverArgs; if (!String.IsNullOrEmpty(comPortName)) { if (comPortName.Contains(MBED)) { comPortName = comPortName.Replace(MBED + " - ", ""); } this.portName = comPortName; } connectionChecker = new SafeThread(delegate() { CheckComConnection(); }, "ComPort connection-checker", logger); if (InitializePort() == true) { try { serialPort.Open(); connectionChecker.Start(); while (!connectionChecker.IsAlive()) { ; } } catch (Exception) { List <COMPortFinder> comportList = COMPortFinder.GetCOMPortsInfo(); foreach (COMPortFinder comPortInfo in comportList) { if (comPortInfo.Description.Contains(MBED)) { this.portName = comPortInfo.Name; break; } } InitializePort(); //serialPort.Open(); } //testPort(); } //.................instantiate the port VPortInfo portInfo = GetPortInfoFromPlatform(embedDriverArgs); mbedPort = InitPort(portInfo); // ..... initialize the list of roles we are going to export and bind to the role List <VRole> listRole = new List <VRole>() { RoleMbedSoftUps.Instance }; BindRoles(mbedPort, listRole); //.................register the port after the binding is complete RegisterPortWithPlatform(mbedPort); }