static void Main(string[] args) { _eventHandler += ExitHandler; SetConsoleCtrlHandler(_eventHandler, true); var info = new ComPortInfo() { ComPort = "COM9\0\0\0\0", }; _gpsService = new GpsService(info); _gpsService.RegisterStatusEvent(Action); _gpsService.RegisterDataEvent(GpsdServiceOnLocationChanged); try { _gpsService.Connect(); } catch (UnauthorizedAccessException) { Console.WriteLine("The selected com port is already in use!"); } Console.WriteLine("Press enter to continue..."); Console.ReadKey(); }
/// <summary> /// Find and connect to any VisiLED unit with the given parameters. /// </summary> /// <param name="portName">The port to connect to in format 'COM#'</param> public static VisiLEDComPort AutoConnectComPort(string portName) { var parameters = ComParameters(); var comPorts = ComPortInfo.GetDescriptions().Where(p => p.Port == portName).ToList(); return(comPorts.Any() ? AutoConnectComPort <VisiLEDComPort>(comPorts.Select(p => p.Port).ToList(), parameters) : null); }
public static IEnumerable <ComPortInfo> GetComPortsInfo() { var comPortInfoList = new List <ComPortInfo>(); ConnectionOptions options = ProcessConnection.ProcessConnectionOptions(); ManagementScope connectionScope = ProcessConnection.ConnectionScope(Environment.MachineName, options, @"\root\CIMV2"); var objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0"); var comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery); using (comPortSearcher) { foreach (ManagementObject obj in comPortSearcher.Get()) { if (obj != null) { object captionObj = obj["Caption"]; if (captionObj != null) { string caption = captionObj.ToString(); if (caption.Contains("(COM")) { var comPortInfo = new ComPortInfo(); comPortInfo.Name = caption.Substring(caption.LastIndexOf("(COM", StringComparison.Ordinal)).Replace("(", string.Empty).Replace(")", string.Empty); comPortInfo.Description = caption; comPortInfoList.Add(comPortInfo); } } } } } return(comPortInfoList); }
/// <summary> /// Extension to test if a connected CVLSComPort is a CVLS USB connection. /// </summary> /// <returns>True = CVLSComPort is CVLS USB connection, False otherwise</returns> public bool IsUsb() { var ports = ComPortInfo.GetDescriptions(); var port = ports.FirstOrDefault(p => p.Port == PortName); return(port?.Name.Contains("SCHOTT CV-LS") == true); }
private static ComPortInfo CleanSerialPortName(string name) { ComPortInfo result = new ComPortInfo() { Name = null, Order = 0, }; // Handle weird linux COM port names if (name.StartsWith("/") == true) { if (name.StartsWith("/dev/tty.") == false) { return(result); } result.Name = name; result.Order = name.GetHashCode(); return(result); } // Handle windows COM ports if (validWindowsSerialPortRegex.IsMatch(name) == true) { result.Name = validWindowsSerialPortRegex.Match(name).Value; result.Order = int.Parse(result.Name.Substring(3)); } return(result); }
public static string[] GetSerialPortNames() { string[] ports = SerialPort.GetPortNames(); List <ComPortInfo> portInfos = new List <ComPortInfo>(ports.Length); for (int i = 0; i < ports.Length; i++) { ComPortInfo port = CleanSerialPortName(ports[i]); if (String.IsNullOrEmpty(port.Name) == true) { continue; } portInfos.Add(port); } portInfos.Sort(); string[] finalPorts = new string[portInfos.Count]; for (int i = 0; i < finalPorts.Length; i++) { finalPorts[i] = portInfos[i].Name; } return(finalPorts); }
/// <summary> /// Find and connect to any KL2500LED unit. /// </summary> public static KL2500LEDComPort AutoConnectComPort() { var parameters = ComParameters(); var comPorts = ComPortInfo.GetDescriptions(); return(comPorts.Any() ? AutoConnectComPort <KL2500LEDComPort>(comPorts.Select(p => p.Port).ToList(), parameters) : null); }
/// <summary> /// Find and connect to any CVLS unit with the given parameters. /// </summary> /// <param name="cvlsPortType">Select the CVLSPortType, can use multiple flags</param> /// <param name="serialNumber">The serial number to connect too</param> public static CVLSComPort AutoConnectComPort(CVLSPortType cvlsPortType, int serialNumber) { var parameters = ComParameters(serialNumber); var comPorts = ComPortInfo.GetDescriptions(); if (ComMode(cvlsPortType) == ThreadedComPortBase.ConnectionMode.SelectionRule) { comPorts = comPorts.Where(SelectionRule(cvlsPortType)).ToList(); } return(comPorts.Any() ? AutoConnectComPort <CVLSComPort>(comPorts.Select(p => p.Port).ToList(), parameters) : null); }
private void portSelect_DropDownOpening(object sender, EventArgs e) { portSelect.DropDownItems.Clear(); var item = new ToolStripMenuItem(); item.Click += PortSelect_Click; item.Text = @"Any VisiLEDComPort"; item.Checked = item.Text == portSelect.Text; portSelect.DropDownItems.Add(item); ComPortInfo.GetDescriptions().ForEach(port => { item = new ToolStripMenuItem(); item.Click += PortSelect_Click; item.Text = port.Port; item.Checked = item.Text == portSelect.Text; portSelect.DropDownItems.Add(item); }); }
private static bool DetectComPort() { _defaultComPort = _smartPort.GetPort("0403", "6001"); if (string.IsNullOrWhiteSpace(_defaultComPort)) { SmartLog.WriteLine("Available COM ports"); SmartLog.WriteLine("--------------------------------------------------------"); int i = 1; int selectedOption; var comports = ComPortInfo.GetComPortsInfo(); comports.ForEach(x => { SmartLog.WriteLine($"{i}) {x.Name}:{x.Description}"); i++; }); SmartLog.WriteLine("\nSelect COM Port (Example: Type 1 and press enter for first COM port):"); while (!int.TryParse(Console.ReadLine(), out selectedOption) || (comports.ElementAtOrDefault(selectedOption - 1) == null)) { SmartLog.WriteLine("Invalid Entry\nPlease Enter from available options"); } var selectedComPort = comports.ElementAtOrDefault(selectedOption - 1); if (selectedComPort == null) { SmartLog.WriteLine("Invalid Entry\nPlease Enter from available options"); Console.ReadKey(); return(false); } _defaultComPort = selectedComPort.Name; } return(true); }
public ComPortGpsClient(ComPortInfo connectionData) : base(GpsType.ComPort, connectionData) { }