/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// using System; using System.Windows.Forms; using System.IO.Ports; using Common; using Config.Entities; using TBF.Rig.Generic; namespace TBF.Rig.Ambient.Comet { public partial class AmbientCfgCtrl : UserControl, IComponentCfgCtrl { public bool ShowMore { get { return false; } } AmbientCfg config; public IComponentCfg Config { get { return config as IComponentCfg; } set { config = value as AmbientCfg; Redraw(); } } public AmbientCfgCtrl() { InitializeComponent(); parityComboBox.Items.Add(Parity.None.ToString()); parityComboBox.Items.Add(Parity.Even.ToString()); parityComboBox.Items.Add(Parity.Odd.ToString()); stopBitsComboBox.Items.Add(StopBits.None.ToString()); stopBitsComboBox.Items.Add(StopBits.One.ToString()); stopBitsComboBox.Items.Add(StopBits.OnePointFive.ToString()); stopBitsComboBox.Items.Add(StopBits.Two.ToString()); handshakeComboBox.Items.Add(Handshake.None.ToString()); handshakeComboBox.Items.Add(Handshake.RequestToSend.ToString()); handshakeComboBox.Items.Add(Handshake.XOnXOff.ToString()); } private void AbbientCfgCtrl_Load(object sender, EventArgs e) { Redraw(); } public void Closing() { } void Redraw() { if (config == null) return; /// Control was not loaded, settings were not changed classNameLabel.Text = config.Factory.ClassName; nameTextBox.Text = config.Name; comPortNrTextBox.Text = config.ComPortNr.ToString(); baudRateTextBox.Text = config.BaudRate.ToString(); parityComboBox.Text = config.Parity.ToString(); dataBitsTextBox.Text = config.DataBits.ToString(); stopBitsComboBox.Text = config.StopBits.ToString(); handshakeComboBox.Text = config.Handshake.ToString(); setDtrCheckBox.Checked = config.SetDtrToOne; } public void Unlock() { nameTextBox.Enabled = true; comPortNrTextBox.Enabled = true; baudRateTextBox.Enabled = true; parityComboBox.Enabled = true; dataBitsTextBox.Enabled = true; stopBitsComboBox.Enabled = true; handshakeComboBox.Enabled = true; setDtrCheckBox.Enabled = true; } public CfgUpdateFlags UpdateCfg() { CfgUpdateFlags flags = CfgUpdateFlags.RestartRqrd; if (config == null) return CfgUpdateFlags.Error; /// Control was not loaded, settings were not changed config.Name = nameTextBox.Text; config.ComPortNr = int.Parse(comPortNrTextBox.Text); config.BaudRate = int.Parse(baudRateTextBox.Text); config.Parity = Common.Utils.GetParity(parityComboBox.SelectedItem.ToString()); config.DataBits = int.Parse(dataBitsTextBox.Text); config.StopBits = Common.Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString()); config.Handshake = Common.Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString()); config.SetDtrToOne = setDtrCheckBox.Checked; return flags; } public CfgUpdateFlags VerifyCfg(ref string message) { CfgUpdateFlags flags = CfgUpdateFlags.None; int dummy; if (!int.TryParse(comPortNrTextBox.Text, out dummy) || dummy < 1 || dummy > 999) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Serial Port Number' is not valid"; } if (!int.TryParse(baudRateTextBox.Text, out dummy) || dummy < 150 || dummy > 115200) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Baud Rate' is not valid"; } if (!int.TryParse(dataBitsTextBox.Text, out dummy) || dummy < 7 || dummy > 8) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Data Bits' is not valid"; } return flags; } } }