public List <HomeOS.Hub.Common.Device> GetDevices() { List <Device> retList = new List <Device>(); //Getting all list of COMPORTs available on the system. List <COMPortFinder> comportList = COMPortFinder.GetCOMPortsInfo(); foreach (COMPortFinder comPortInfo in comportList) { //Checking if COMPORT is our desired COMPORT or not. if (comPortInfo.Description.Contains(MBED)) { string deviceUniqueName = String.Format("{0} - {1}", MBED, comPortInfo.Name); string deviceFriendlyName = comPortInfo.Description; /* * The device object which is filled with necessary information like * which driver should be invoked when this particular device is added to the platform. */ Device device = new Device(deviceFriendlyName, deviceUniqueName, "", DateTime.Now, "HomeOS.Hub.Drivers.MbedDriver", false); /* * intialize the parameters for this device, * these paramenters will be passed to the driver when driver is invoked. */ device.Details.DriverParams = new List <string>() { deviceUniqueName }; retList.Add(device); } } // list of devices will be returned to HomeOS platform/dashboard. return(retList); }
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; }