public SerialPortSetting GetSetting() { SerialPortSetting setting = new SerialPortSetting(); if (g_sensor.Visibility == System.Windows.Visibility.Visible) { setting.IP = textBoxIP.Text.Trim(); int port = 0; if (int.TryParse(textBoxPort.Text.Trim(), out port)) { } setting.Port = port; } else { if (string.IsNullOrWhiteSpace(this.comboBoxPortNames.Text)) { throw new Exception("请选择使用的端口名称!"); } setting.PortName = this.comboBoxPortNames.Text; int baudRate; if (!int.TryParse(this.textBoxBaudRate.Text, out baudRate) || baudRate <= 0) { throw new Exception("波特率应该为大于0的整数"); } setting.BaudRate = baudRate; setting.Parity = (Parity)this.comboBoxParity.SelectedValue; setting.DataBits = int.Parse(this.comboBoxDataBits.Text.ToString()); setting.StopBits = (StopBits)this.comboBoxStopBits.SelectedValue; } return(setting); }
public void SetSetting(SerialPortSetting setting) { if (string.IsNullOrEmpty(setting.IP)) { g_sensor.Visibility = System.Windows.Visibility.Collapsed; g_com.Visibility = System.Windows.Visibility.Visible; this.Height = 200; Ini(); this.comboBoxPortNames.SelectedValue = setting.PortName; this.textBoxBaudRate.Text = setting.BaudRate.ToString(); this.comboBoxStopBits.SelectedValue = setting.StopBits; this.comboBoxParity.SelectedValue = setting.Parity; this.comboBoxDataBits.Text = setting.DataBits.ToString(); } else { this.Height = 150; g_com.Visibility = System.Windows.Visibility.Collapsed; g_sensor.Visibility = System.Windows.Visibility.Visible; textBoxIP.Text = setting.IP; textBoxPort.Text = setting.Port.ToString(); } }