private void Button_Open_Close_Click(object sender, RoutedEventArgs e) { if (mySerialPort != null) { mySerialPort.Close(); mySerialPort = null; btnOpen.Content = "Open"; } else { try { var cfg = CollectPortCfg(); mySerialPort = new Serial.MySerialPort(cfg, OnSerialPortDataRecv); mySerialPort.Open(); btnOpen.Content = "Close"; } catch (System.Exception ex) { MessageBox.Show(ex.Message); mySerialPort = null; } } }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (mySerialPort != null) { mySerialPort.Close(); mySerialPort = null; } }