/// /// Copyright (c) 2015 Sensus Metering Systems /// Author: Milan Hanajik /// using System; using System.Globalization; using System.Windows.Forms; using System.IO.Ports; using log4net; using TBF.BenchControl.Generic; namespace TBF.BenchControl.MettlerToledo.Multi { public partial class BalanceCfgCtrl : UserControl, IComponentCfgCtrl { static readonly ILog log = LogManager.GetLogger(typeof(BalanceCfgCtrl)); public bool ShowMore { get { return false; } } BalanceCfg config; public IComponentCfg Config { get { return config as IComponentCfg; } set { config = value as BalanceCfg; Redraw(); } } public BalanceCfgCtrl() { 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()); } int GetParityIx(Parity par) { if (par == Parity.None) return 0; if (par == Parity.Even) return 0; if (par == Parity.Odd) return 0; return -1; } Parity GetParity(string str) { if (str.Equals(Parity.None.ToString())) return Parity.None; if (str.Equals(Parity.Even.ToString())) return Parity.Even; if (str.Equals(Parity.Odd.ToString())) return Parity.Odd; return (Parity)(-1); } int GetStopBitsIx(StopBits sb) { if (sb == StopBits.None) return 0; if (sb == StopBits.One) return 1; if (sb == StopBits.OnePointFive) return 2; if (sb == StopBits.Two) return 3; return -1; } StopBits GetStopBits(string str) { if (str.Equals(StopBits.None.ToString())) return StopBits.None; if (str.Equals(StopBits.One.ToString())) return StopBits.One; if (str.Equals(StopBits.OnePointFive.ToString())) return StopBits.OnePointFive; if (str.Equals(StopBits.Two.ToString())) return StopBits.Two; return (StopBits)(-1); } int GetHandshakeIx(Handshake par) { if (par == Handshake.None) return 0; if (par == Handshake.RequestToSend) return 0; if (par == Handshake.XOnXOff) return 0; return -1; } Handshake GetHandshake(string str) { if (str.Equals(Handshake.None.ToString())) return Handshake.None; if (str.Equals(Handshake.RequestToSend.ToString())) return Handshake.RequestToSend; if (str.Equals(Handshake.XOnXOff.ToString())) return Handshake.XOnXOff; return (Handshake)(-1); } private void BalanceCfgCtrl_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; idNrTextBox.Text = config.IdNr.ToString(); capacityTextBox.Text = config.Capacity.ToString(); resolutionTextBox.Text = config.Resolution.ToString(); emptyTextBox.Text = config.Empty.ToString(); formatTextBox.Text = (config.Format.Length > 1) ? config.Format.Substring(1) : config.Format; emptyTimeTextBox.Text = config.EmptyTimeSec.ToString(); 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(); } public void Unlock() { nameTextBox.Enabled = true; idNrTextBox.Enabled = true; capacityTextBox.Enabled = true; resolutionTextBox.Enabled = true; emptyTextBox.Enabled = true; formatTextBox.Enabled = true; emptyTimeTextBox.Enabled = true; comPortNrTextBox.Enabled = true; baudRateTextBox.Enabled = true; parityComboBox.Enabled = true; dataBitsTextBox.Enabled = true; stopBitsComboBox.Enabled = true; handshakeComboBox.Enabled = true; } public CfgUpdateFlags VerifyCfg(ref string message) { CfgUpdateFlags flags = CfgUpdateFlags.None; int idNr; if (!int.TryParse(idNrTextBox.Text, out idNr) || idNr < 1 || idNr > 3) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Id Number' is not valid"; } float mass; if (!Utils.TryParseUFloat(capacityTextBox.Text, out mass) || mass <= 0) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Tank Capacity' is not valid"; } if (!Utils.TryParseUFloat(resolutionTextBox.Text, out mass) || mass <= 0) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Resolution' is not valid"; } if (!Utils.TryParseUFloat(emptyTextBox.Text, out mass) || mass <= 0) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Tank Empty' is not valid"; } int dummy; if (!int.TryParse(formatTextBox.Text, out dummy) || dummy < 0 || dummy > 9) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Decimal points' should be between 0 and 9"; } if (!int.TryParse(emptyTimeTextBox.Text, out dummy) || dummy < 1 || dummy > 999) { flags |= CfgUpdateFlags.Error; message += Environment.NewLine + "'Tank Empty Time' should be between 1 and 999 sec"; } if (idNr == 1) { 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; } 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.IdNr = int.Parse(idNrTextBox.Text); config.Capacity = Utils.ParseUFloat(capacityTextBox.Text); config.Resolution = Utils.ParseUFloat(resolutionTextBox.Text); config.Empty = Utils.ParseUFloat(emptyTextBox.Text); config.Format = "F" + formatTextBox.Text; config.EmptyTimeSec = int.Parse(emptyTimeTextBox.Text); if (config.IdNr == 1) { config.ComPortNr = int.Parse(comPortNrTextBox.Text); config.BaudRate = int.Parse(baudRateTextBox.Text); config.Parity = GetParity(parityComboBox.SelectedItem.ToString()); config.DataBits = int.Parse(dataBitsTextBox.Text); config.StopBits = GetStopBits(stopBitsComboBox.SelectedItem.ToString()); config.Handshake = GetHandshake(handshakeComboBox.SelectedItem.ToString()); } return flags; } private void idNrTextBox_TextChanged(object sender, EventArgs e) { int idNr; if (int.TryParse(idNrTextBox.Text, out idNr)) { bool visible = (idNr == 1); comPortNrLabel.Visible = visible; comPortNrTextBox.Visible = visible; baudRateLabel.Visible = visible; baudRateTextBox.Visible = visible; parityLabel.Visible = visible; parityComboBox.Visible = visible; dataBitsLabel.Visible = visible; dataBitsTextBox.Visible = visible; stopBitsLabel.Visible = visible; stopBitsComboBox.Visible = visible; handshakeLabel.Visible = visible; handshakeComboBox.Visible = visible; } } } }