private void FormMain_Load(object sender, EventArgs e) { _defaultSettings = new PortSettingsEntity(); //Object instance containing default settings _currentSettings = new PortSettingsEntity(); //Object instance storing current settings from user InitializeComPort(_defaultSettings); InitializeComPort(_currentSettings); _composer = new CmdComposer(this); }
private void InitializeComPort(PortSettingsEntity handle) { string[] port_list = System.IO.Ports.SerialPort.GetPortNames(); if (port_list.Length > 0) { handle.PortName = port_list[0]; //Default com port is the first one on the list handle.BaudRate = 9600; handle.DataBits = 8; handle.StopBits = System.IO.Ports.StopBits.One; handle.Parity = System.IO.Ports.Parity.None; } }
private void UpdateSettings(PortSettingsEntity entity) { if (_viewModel == null) { _viewModel = new PortSettingsViewModel(); } _viewModel.Entity = entity; //Pass reference to view model _viewModel.PortName = comboBoxPortName.SelectedItem.ToString(); _viewModel.BaudRate = comboBoxBaud.SelectedItem.ToString(); _viewModel.DataBits = comboBoxDataBit.SelectedItem.ToString(); _viewModel.Parity = comboBoxParity.SelectedItem.ToString(); _viewModel.StopBits = comboBoxStopBit.SelectedItem.ToString(); _viewModel.Handshake = comboBoxFlowControl.SelectedItem.ToString(); }
public void Initialize(PortSettingsEntity defaultSettings, PortSettingsEntity currentSettings) { if (defaultSettings != null && currentSettings != null) { FillComboboxPortName(); FillComboboxBaudRate(); FillComboboxDataBit(); FillComboboxParity(); FillComboboxStopBit(); FillComboboxHandshake(); SelectSettings(currentSettings); _defaultSettings = defaultSettings; _currentSettings = currentSettings; } }
private void FormMain_Load(object sender, EventArgs e) { _defaultSettings = new PortSettingsEntity(); //Object instance containing default settings _currentSettings = new PortSettingsEntity(); //Object instance storing current settings from user InitializeComPort(_defaultSettings); InitializeComPort(_currentSettings); _composer = new CmdComposer(this); //Delegate this.textUpdaterDelegate = new UpdateUIDelegate(generalAcknowledge); this.timedOutputStatusUpdaterDelegate = new UpdateUIDelegate(updateTimedOutputStatus); this.timedInputPeriodUpdaterDelegate = new UpdateUIDelegate(updateTimedOutputPeriod); this.digitalInputUpdaterDelegate = new UpdateUIDelegate(updateDigitalInputStatus); this.adcChannel1UpdaterDelegate = new UpdateUIDelegate(updateAdcChannel1Value); this.adcChannel2UpdaterDelegate = new UpdateUIDelegate(updateAdcChannel2Value); this.adcChannel3UpdaterDelegate = new UpdateUIDelegate(updateAdcChannel3Value); }
private void SelectSettings(PortSettingsEntity settings) { int index = comboBoxPortName.Items.IndexOf(settings.PortName); if (index >= 0) { comboBoxPortName.SelectedIndex = index; } else { //comboBoxPortName.SelectedIndex = 0; } comboBoxBaud.SelectedIndex = comboBoxBaud.Items.IndexOf(settings.BaudRate.ToString()); comboBoxDataBit.SelectedIndex = comboBoxDataBit.Items.IndexOf(settings.DataBits.ToString()); comboBoxParity.SelectedIndex = comboBoxParity.Items.IndexOf(settings.Parity.ToString()); comboBoxStopBit.SelectedIndex = comboBoxStopBit.Items.IndexOf(settings.StopBits.ToString()); comboBoxFlowControl.SelectedIndex = comboBoxFlowControl.Items.IndexOf(settings.Handshake.ToString()); }
private void Connect(PortSettingsEntity handle) { if (handle != null) { //Configure serial port serialPort1.PortName = handle.PortName; serialPort1.BaudRate = (int)handle.BaudRate; serialPort1.DataBits = handle.DataBits; serialPort1.Parity = handle.Parity; serialPort1.Handshake = handle.Handshake; serialPort1.ReadTimeout = handle.ReadTimeout; serialPort1.WriteTimeout = handle.WriteTimeout; serialPort1.DiscardNull = handle.DiscardNull; serialPort1.RtsEnable = handle.RtsEnable; serialPort1.NewLine = handle.Newline; serialPort1.Encoding = handle.Encoding; try { if (serialPort1.IsOpen == false) { serialPort1.Open(); //attempt to open the configured serial port if (serialPort1.IsOpen) { //Com Port connected connectToolStripMenuItem.Enabled = false; disconnectToolStripMenuItem.Enabled = true; tbAscii.ReadOnly = false; } } } catch (Exception) { MessageBox.Show("Unable to open serial port"); } } }