private void Window_Loaded(object sender, RoutedEventArgs e) { // Get a list of serial port names. string[] ports = null; try { ports = SerialPort.GetPortNames(); } catch (Exception) { cbPortNames.Items.Add("No available COM Ports"); } // Put each available port name into the ComboBox control. foreach (string port in ports) { var cbItem = new ComboBoxItem(); cbItem.Content = port; cbPortNames.Items.Add(cbItem); } // read configuration file modbusPort = new CommunicationData(); modbusPort.LoadConfiguration(); // set visual items if advanced menus are enabled and available if (!modbusPort.IsEnabledAdvancedMenus || tbiConnection == null) { // tbiCommunication.Visibility = Visibility.Hidden; // tbiConnection.Visibility = Visibility.Hidden; return; } tbiCommunication.Visibility = Visibility.Visible; tbiConnection.Visibility = Visibility.Visible; var portIdx = Array.IndexOf(ports, modbusPort.ComPortName); cbPortNames.SelectedIndex = (portIdx>=0)?portIdx:0; tbSlaveId.Text = modbusPort.ModbusSlave.ToString(); tbInterval.Text = modbusPort.ModbusInterval.ToString(); tbDbServer.Text = modbusPort.DbServer; tbDbName.Text = modbusPort.DbDatabase; tbDbUser.Text = modbusPort.DbUser; tbDbPassword.Text = modbusPort.DbPassword; }
private void ButtonApply_Click(object sender, RoutedEventArgs e) { bool needToSaveConfig = false; if (testedPort != null && tbSlaveId != null) { modbusPort.ComPortName = testedPort.ComPortName; modbusPort.ComPortBaudRate = testedPort.ComPortBaudRate; modbusPort.SetParity(testedPort.ComPortParity.ToString()); var slaveId = 0; int.TryParse(tbSlaveId.Text, out slaveId); if (slaveId > 0 && slaveId < 248 && modbusPort.ModbusSlave != slaveId) { modbusPort.ModbusSlave = slaveId; needToSaveConfig = true; } if (tbDbServer != null && modbusPort.DbServer != tbDbServer.Text) { modbusPort.DbServer = tbDbServer.Text.Trim(); needToSaveConfig = true; } if (tbDbName != null && modbusPort.DbDatabase != tbDbName.Text) { modbusPort.DbDatabase = tbDbName.Text.Trim(); needToSaveConfig = true; } if (tbDbUser != null && modbusPort.DbUser != tbDbUser.Text) { modbusPort.DbUser = tbDbUser.Text.Trim(); needToSaveConfig = true; } if (tbDbPassword != null && modbusPort.DbPassword != tbDbPassword.Text) { modbusPort.DbPassword = tbDbPassword.Text.Trim(); needToSaveConfig = true; } } if (needToSaveConfig) { modbusPort.SaveConfiguration(); } testedPort = null; DisableApplyButton(); }
private void ButtonTest_Click(object sender, RoutedEventArgs e) { var portName = (cbPortNames.SelectedItem != null) ? ((ComboBoxItem)cbPortNames.SelectedItem).Content.ToString() : ""; var baudRateString = (cbPortSpeeds.SelectedItem != null) ? ((ComboBoxItem)cbPortSpeeds.SelectedItem).Content.ToString() : ""; var baudRate = int.Parse(baudRateString); var parity = (cbPortParity.SelectedItem != null) ? ((ComboBoxItem)cbPortParity.SelectedItem).Content.ToString() : ""; var testPort = new CommunicationData(); testPort.ComPortName = portName; testPort.ComPortBaudRate = baudRate; testPort.SetParity(parity); var test = testPort.TestPort(); if (lblConnResult != null) { lblConnResult.Content = (test) ? "Communication OK" : "Communication Failed"; } testedPort = (test) ? testPort : null; if (btnApply != null) { btnApply.IsEnabled = test; } }