public async Task <bool> SystemHello(int timeoutMs = 4000) { // This is a ping event. Return's true if device responds, // false if the valid response is not received before the given timeout var cmd = _bglib.BLECommandSystemHello(); await _bglib.SendCommandAsync(cmd).ConfigureAwait(false); bool didTimeout = false; using (var cts = new CancellationTokenSource(timeoutMs)) { try { await _cmdRespWaitHandle.WaitAsync(cts.Token).ConfigureAwait(false); } catch (OperationCanceledException) { didTimeout = true; } } return(!didTimeout && _lastResponseArgs is HelloEventArgs); }
private void btnOpenPort_Click(object sender, EventArgs e) { string portName = cmbPorts.SelectedItem as string; if (portName == null) { return; } if (serialPort != null) { try { serialPort.Dispose(); } catch (Exception ex) { WriteTxtLog($"{serialPort.PortName} dispose failed. {ex}"); } } serialPort = new SerialPort(portName); serialPort.Handshake = Handshake.RequestToSend; serialPort.BaudRate = 115200; serialPort.DataBits = 8; serialPort.StopBits = StopBits.One; serialPort.Parity = Parity.None; serialPort.WriteTimeout = 1000; serialPort.DataReceived += DataReceivedHandler; serialPort.ErrorReceived += ErrorReceivedHandler; try { serialPort.Open(); } catch (Exception ex) { WriteTxtLog($"Port open failed: {portName}: {ex}"); } WriteTxtLog($"Port opened: {portName}"); SendCommand(bglib.BLECommandSystemHello()); }