private void reconnect() { if (Properties.Settings.Default.CommPort != "") { try { if (mm != null) { mm.Disconnect(); } } catch (Exception) { } try { mm = new ModbusMasterSerial(ModbusSerialType.RTU, Properties.Settings.Default.CommPort, 19200, 8, Parity.None, StopBits.One, Handshake.None); setCommPortMenu(Properties.Settings.Default.CommPort); mm.RxTimeout = 500; mm.Connect(); commStatus.Text = "COMM OK"; } catch (Exception) { commStatus.Text = "COMM ERR"; } } }
/// <summary> /// When the application closes, save settings and close serial port. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormModbus_FormClosed(object sender, FormClosedEventArgs e) { // Close the Modbus device. _mbMaster?.Disconnect(); // Update application settings from the form. Properties.Settings.Default.Port = comboBoxSerialPort.Text; Properties.Settings.Default.BaudRate = Convert.ToUInt16(comboBoxBaudRate.Text); Properties.Settings.Default.StopBits = Convert.ToUInt16(comboBoxStopBits.Text); Properties.Settings.Default.Parity = comboBoxParity.Text; Properties.Settings.Default.Mode = comboBoxMode.Text; Properties.Settings.Default.Address = Convert.ToUInt16(textBoxDeviceAddressDecimal.Text); // Store the current values of the application settings properties. // If this call is omitted, then the settings will not be saved after the program quits. Properties.Settings.Default.Save(); }