private async void ConfigPortForm_Load(object sender, EventArgs e) { // Add the fixed options that cannot change AddFixedComboBoxItems(); try { string[] portNames = await AddComPortComboBoxItemsAsync(); PortNameComboBox.Items.Clear(); PortNameComboBox.Items.AddRange(portNames); } catch (Exception ex) { //_log.Error(ex, "AddComPortComboBoxItemsAsync: GetPortNames failed - {0}", ex.Message); myErrorProvider.SetError(PortNameComboBox, string.Format("GetPortNames failed - {0}", ex.Message)); } IsLoaded = true; if (_delayConfigLoad == null) { string portName = PortNameComboBox.Items.Count > 0 ? (string)PortNameComboBox.Items[0] : null; _delayConfigLoad = SerialPortConfig.Default(portName); } else { SetSerialPortConfig(_delayConfigLoad); } Validate(); ValidateChildren(); }
public static SerialPortConfig Create(SerialPort port) { var config = new SerialPortConfig(); config.GetPortConfiguration(port); return(config); }
public void SetSerialPortConfig(SerialPortConfig config) { if (config == null) { throw new ArgumentNullException("Configuration is null"); } //if (!IsLoaded) { throw new InvalidOperationException("UserControl is not loaded yet"); } _delayConfigLoad = config; if (IsLoaded) { SetSelectedIndex(PortNameComboBox, config.PortName); SetSelectedIndex(BaudRateComboBox, config.BaudRate); SetSelectedIndex(DataBitsComboBox, config.DataBits); SetSelectedIndexEnum(ParityComboBox, config.Parity); SetSelectedIndexEnum(StopBitsComboBox, config.StopBits); SetSelectedIndexEnum(HandshakeComboBox, config.Handshake); } }
public SerialPortConfig GetSerialPortConfig() { var config = new SerialPortConfig(); if (config == null) { throw new InvalidOperationException("Configuration is null"); } if (!IsLoaded) { throw new InvalidOperationException("Form is not loaded yet"); } GetSelectedValue(PortNameComboBox, x => { config.PortName = x; }); GetSelectedValue(BaudRateComboBox, x => { config.BaudRate = x; }); GetSelectedValue(DataBitsComboBox, x => { config.DataBits = x; }); GetSelectedValue(ParityComboBox, (Parity x) => { config.Parity = x; }); GetSelectedValue(StopBitsComboBox, (StopBits x) => { config.StopBits = x; }); GetSelectedValue(HandshakeComboBox, (Handshake x) => { config.Handshake = x; }); return(config); }
private void ConfigButton_Click(object sender, EventArgs e) { if (_configForm == null || _configForm.IsDisposed) { _configForm = new ConfigPortForm(); _configForm.Owner = this; } var config = new SerialPortConfig(); config.GetPortConfiguration(_port); var handle = _configForm.Handle; // Force the form to load _configForm.SetSerialPortConfig(config); var result = _configForm.ShowDialog(this); if (result == DialogResult.OK) { config = _configForm.GetSerialPortConfig(); config.SetPortConfiguration(_port); } }