public static List <Win32SerialPortClass> GetWin32SerialPortClassDevices() { List <Win32SerialPortClass> result = new List <Win32SerialPortClass>(); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_SerialPort")) { try { foreach (var o in searcher.Get()) { var managementObject = (ManagementObject)o; Win32SerialPortClass portInfo = new Win32SerialPortClass(); MapObject2Win32SerialPortClass(managementObject, portInfo); if (!portInfo.Caption.Contains("LPT")) { result.Add(portInfo); } } } catch (Exception ex) { "GetWin32SerialPortClassDevices Error {0} {1}"._DLOG(ex.Message, ex.StackTrace); } } return(result); }
public static List <Win32SerialPortClass> GetWin32SerialPortClassDevices(string condition) { List <Win32SerialPortClass> result = new List <Win32SerialPortClass>(); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_SerialPort where " + condition)) { foreach (var o in searcher.Get()) { var managementObject = (ManagementObject)o; Win32SerialPortClass portInfo = new Win32SerialPortClass(); MapObject2Win32SerialPortClass(managementObject, portInfo); if (!portInfo.Caption.Contains("LPT")) { result.Add(portInfo); } } } return(result); }
private static void MapObject2Win32SerialPortClass(ManagementBaseObject managementObject, Win32SerialPortClass portInfo) { MapObject2BaseWin32DeviceClass(managementObject, portInfo); if (managementObject.Properties["Binary"].Value != null) { portInfo.Binary = (bool?)managementObject.Properties["Binary"].Value; } if (managementObject.Properties["MaxBaudRate"].Value != null) { portInfo.MaxBaudRate = (uint?)managementObject.Properties["MaxBaudRate"].Value; } if (managementObject.Properties["MaximumInputBufferSize"].Value != null) { portInfo.MaximumInputBufferSize = (uint?)managementObject.Properties["MaximumInputBufferSize"].Value; } if (managementObject.Properties["MaximumOutputBufferSize"].Value != null) { portInfo.MaximumOutputBufferSize = (uint?)managementObject.Properties["MaximumOutputBufferSize"].Value; } if (managementObject.Properties["OSAutoDiscovered"].Value != null) { portInfo.OsAutoDiscovered = (bool?)managementObject.Properties["OSAutoDiscovered"].Value; } if (managementObject.Properties["ProviderType"].Value != null) { portInfo.ProviderType = (string)managementObject.Properties["ProviderType"].Value; } if (managementObject.Properties["SettableBaudRate"].Value != null) { portInfo.SettableBaudRate = (bool?)managementObject.Properties["SettableBaudRate"].Value; } if (managementObject.Properties["SettableDataBits"].Value != null) { portInfo.SettableDataBits = (bool?)managementObject.Properties["SettableDataBits"].Value; } if (managementObject.Properties["SettableFlowControl"].Value != null) { portInfo.SettableFlowControl = (bool?)managementObject.Properties["SettableFlowControl"].Value; } if (managementObject.Properties["SettableParity"].Value != null) { portInfo.SettableParity = (bool?)managementObject.Properties["SettableParity"].Value; } if (managementObject.Properties["SettableParityCheck"].Value != null) { portInfo.SettableParityCheck = (bool?)managementObject.Properties["SettableParityCheck"].Value; } if (managementObject.Properties["SettableRLSD"] != null) { if (managementObject.Properties["SettableRLSD"].Value != null) { portInfo.SettableRlsd = (bool?)managementObject.Properties["SettableRLSD"].Value; } } if (managementObject.Properties["SettableStopBits"] != null) { if (managementObject.Properties["SettableStopBits"].Value != null) { portInfo.SettableStopBits = (bool?)managementObject.Properties["SettableStopBits"].Value; } } if (managementObject.Properties["Supports16BitMode"] != null) { if (managementObject.Properties["Supports16BitMode"].Value != null) { portInfo.Supports16BitMode = (bool?)managementObject.Properties["Supports16BitMode"].Value; } } if (managementObject.Properties["SupportsDTRDSR"] != null) { if (managementObject.Properties["SupportsDTRDSR"].Value != null) { portInfo.SupportsDtrdsr = (bool?)managementObject.Properties["SupportsDTRDSR"].Value; } } if (managementObject.Properties["SupportsElapsedTimeouts"] != null) { if (managementObject.Properties["SupportsElapsedTimeouts"].Value != null) { portInfo.SupportsElapsedTimeouts = (bool?)managementObject.Properties["SupportsElapsedTimeouts"].Value; } } if (managementObject.Properties["SupportsIntTimeouts"] != null) { if (managementObject.Properties["SupportsIntTimeouts"].Value != null) { portInfo.SupportsIntTimeouts = (bool?)managementObject.Properties["SupportsIntTimeouts"].Value; } } if (managementObject.Properties["SupportsParityCheck"] != null) { if (managementObject.Properties["SupportsParityCheck"].Value != null) { portInfo.SupportsParityCheck = (bool?)managementObject.Properties["SupportsParityCheck"].Value; } } if (managementObject.Properties["SupportsRLSD"] != null) { if (managementObject.Properties["SupportsRLSD"].Value != null) { portInfo.SupportsRlsd = (bool?)managementObject.Properties["SupportsRLSD"].Value; } } if (managementObject.Properties["SupportsRTSCTS"] != null) { if (managementObject.Properties["SupportsRTSCTS"].Value != null) { portInfo.SupportsRtscts = (bool?)managementObject.Properties["SupportsRTSCTS"].Value; } } if (managementObject.Properties["SupportsSpecialCharacters"] != null) { if (managementObject.Properties["SupportsSpecialCharacters"].Value != null) { portInfo.SupportsSpecialCharacters = (bool?)managementObject.Properties["SupportsSpecialCharacters"].Value; } } if (managementObject.Properties["SupportsXOnXOff"] != null) { if (managementObject.Properties["SupportsXOnXOff"].Value != null) { portInfo.SupportsXonXOff = (bool?)managementObject.Properties["SupportsXOnXOff"].Value; } } if (managementObject.Properties["SupportsXOnXOffSet"] != null) { if (managementObject.Properties["SupportsXOnXOffSet"].Value != null) { portInfo.SupportsXonXOffSet = (bool?)managementObject.Properties["SupportsXOnXOffSet"].Value; } } }