public static async Task <bool> TestPort(string comPort) { string result = null; Debug.WriteLine($"Testing port {comPort}"); SerialPort testedPort = new SerialPort(comPort, 9600, Parity.None, 8, StopBits.One); try { testedPort.Open(); if (!_connectedOnce) { testedPort.Write("test"); testedPort.Close(); await Task.Delay(1500); testedPort.Open(); } testedPort.Write(_askingString); testedPort.ReadTimeout = 2000; char[] buffer = new char[_responseString.Length]; result = testedPort.ReadLine().TrimEnd('\r'); if (result != _responseString) { Debug.WriteLine($"Port {comPort} said {result}"); } else { _devicePort = testedPort; _devicePort.DataReceived += SerialRead; Debug.WriteLine($"Port {comPort} is IR receiver !"); IRReceiverComPortChanged?.Invoke(comPort); _connectedOnce = true; return(true); } } catch (Exception e) { Debug.WriteLine($"{comPort} : {e.Message}"); } testedPort.Close(); return(false); }
public static void ClearCurrentPort() { _devicePort?.Close(); _devicePort = null; IRReceiverComPortChanged?.Invoke(null); }