public void UpdateSerialPortSettings() { Stop(); var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); configuration.AppSettings.Settings["SerialPortName"].Value = SerialPortName; configuration.AppSettings.Settings["BitsPerSeconds"].Value = SelectedBitsPerSeconds.ToString(); configuration.AppSettings.Settings["DataBits"].Value = SelectedDataBits.ToString(); configuration.AppSettings.Settings["Parity"].Value = SelectedParity.ToString(); configuration.AppSettings.Settings["StopBits"].Value = SelectedStopBits.ToString(); configuration.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); Start(); }
/// <summary> /// Initiate serial port communication. /// </summary> private void StartListening() { try { if (_SerialPort != null && _SerialPort.IsOpen) { _SerialPort.Dispose(); _SerialPort.Close(); logger.Log(LogLevel.Debug, "SerialPort.Dispose() & SerialPort.Close() are executed on StartListening() method."); } _SerialPort.PortName = SelectedCommPort.DeviceID; _SerialPort.BaudRate = SelectedBaudRate; _SerialPort.Parity = SelectedParity; _SerialPort.DataBits = SelectedDataBits; _SerialPort.StopBits = SelectedStopBits; _SerialPort.Open(); logger.Log(LogLevel.Debug, "SerialPort.Open() is executed."); _SerialPort.DtrEnable = IsDTR; _SerialPort.RtsEnable = IsRTS; OutputText = ""; OnPropertyChanged("OutputText"); EnableDisableSettings = false; OnPropertyChanged("EnableDisableSettings"); logger.Log(LogLevel.Info, "Connected to: " + SelectedCommPort.DeviceID + ", " + SelectedBaudRate.ToString() + " baud, Parity." + SelectedParity.ToString() + ", " + SelectedDataBits.ToString() + ", StopBits." + SelectedStopBits.ToString() + ", RTS=" + IsRTS.ToString() + ", DTR=" + IsDTR.ToString()); _SerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedEvent); logger.Log(LogLevel.Debug, "Ready to receive data..."); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); logger.Log(LogLevel.Error, "EXCEPTION raised: " + ex.ToString()); } WindowTitle = AppTitle + " (" + GetConnectionStatus() + ")"; OnPropertyChanged("WindowTitle"); }