tbf/TBF/Rig/Modbus/ConductivityMeter/ConductivityMeterCfgCtrl.cs

258 lines
9.0 KiB
C#

using Common;
using Config.Entities;
using System;
using System.Windows.Forms;
using TBF.Rig.Generic;
using TBF.UI.Bench.Components;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace TBF.Rig.Modbus.ConductivityMeter
{
public partial class ConductivityMeterCfgCtrl : UserControl, IComponentCfgCtrl
{
ComponentParametersDlg parent;
public bool ShowMore { get { return false; } }
ConductivityMeterCfg config;
public IComponentCfg Config
{
get { return config as IComponentCfg; }
set
{
config = value as ConductivityMeterCfg;
Redraw();
}
}
public ConductivityMeterCfgCtrl()
{
InitializeComponent();
}
private void ConductivityMeterCfgCtrl_Load(object sender, EventArgs e)
{
parent = ParentForm as ComponentParametersDlg;
if (parent == null) return;
// Parent selection: Modbus component
if (parent.CmpntEntities != null)
{
foreach (var cmpnt in parent.CmpntEntities)
{
if (TbfComponents.CmpntFactoryFromClassName(cmpnt.ClassName) is Modbus.Common.Factory)
{
parentNameComboBox.Items.Add(cmpnt.Name);
}
}
}
// Function 03/04
fnComboBox.Items.Add(ModbusReadFunction.ReadHoldingRegisters_03.ToString());
fnComboBox.Items.Add(ModbusReadFunction.ReadInputRegisters_04.ToString());
// Raw format
rawFormatComboBox.Items.Add(RawFormat.Counts16bit.ToString());
rawFormatComboBox.Items.Add(RawFormat.Milliamps_x1000.ToString());
Redraw();
}
public void Closing() { }
void Redraw()
{
if (config == null) return;
componentNameLabel.Text = config.Factory.ClassName;
nameTextBox.Text = config.Name;
parentNameComboBox.Text = string.IsNullOrEmpty(config.ParentName) ? "---" : config.ParentName;
modbusAddressTextBox.Text = config.ModbusAddress.ToString();
fnComboBox.Text = config.ReadFunction.ToString();
firstRegisterTextBox.Text = config.FirstRegister.ToString();
registerCountTextBox.Text = config.RegisterCount.ToString();
enablePollingCheckBox.Checked = config.EnablePolling;
channelTextBox.Text = config.Channel.ToString();
rawFormatComboBox.Text = config.RawFormat.ToString();
raw4TextBox.Text = config.RawAt4mA.ToString();
raw20TextBox.Text = config.RawAt20mA.ToString();
cond4TextBox.Text = config.CondAt4mA.ToString();
cond20TextBox.Text = config.CondAt20mA.ToString();
unitTextBox.Text = config.MsrdUnit;
formatTextBox.Text = config.MsrdFormat;
limLoTextBox.Text = config.MsrdValLimLo.ToString();
limHiTextBox.Text = config.MsrdValLimHi.ToString();
freshnessTextBox.Text = config.FreshnessMs.ToString();
}
public void Unlock()
{
nameTextBox.Enabled = true;
parentNameComboBox.Enabled = true;
modbusAddressTextBox.Enabled = true;
fnComboBox.Enabled = true;
firstRegisterTextBox.Enabled = true;
registerCountTextBox.Enabled = true;
enablePollingCheckBox.Enabled = true;
channelTextBox.Enabled = true;
rawFormatComboBox.Enabled = true;
raw4TextBox.Enabled = true;
raw20TextBox.Enabled = true;
cond4TextBox.Enabled = true;
cond20TextBox.Enabled = true;
unitTextBox.Enabled = true;
formatTextBox.Enabled = true;
limLoTextBox.Enabled = true;
limHiTextBox.Enabled = true;
freshnessTextBox.Enabled = true;
diagnosticsButton.Enabled = true;
}
public CfgUpdateFlags VerifyCfg(ref string message)
{
CfgUpdateFlags flags = CfgUpdateFlags.None;
int iVal;
double dVal;
if (!parentNameComboBox.Items.Contains(parentNameComboBox.Text))
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid 'Parent Component'";
}
if (!int.TryParse(modbusAddressTextBox.Text, out iVal) || iVal < 1 || iVal > 247)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "'Modbus Address' must be between 1 and 247";
}
if (!fnComboBox.Items.Contains(fnComboBox.Text))
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid 'Function (03/04)'";
}
if (!int.TryParse(firstRegisterTextBox.Text, out iVal) || iVal < 0 || iVal > 65535)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "'First Register' must be between 0 and 65535";
}
if (!int.TryParse(registerCountTextBox.Text, out iVal) || iVal < 1 || iVal > 4)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "'Register Count' must be between 1 and 4";
}
if (!int.TryParse(channelTextBox.Text, out iVal) || iVal < 0)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid 'Channel'";
}
if (!rawFormatComboBox.Items.Contains(rawFormatComboBox.Text))
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid 'Raw Format'";
}
if (!double.TryParse(raw4TextBox.Text, out dVal) || !double.TryParse(raw20TextBox.Text, out dVal))
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid raw calibration values (Raw @ 4 mA / Raw @ 20 mA)";
}
if (!double.TryParse(cond4TextBox.Text, out dVal) || !double.TryParse(cond20TextBox.Text, out dVal))
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid conductivity scaling values (Cond @ 4 mA / Cond @ 20 mA)";
}
if (!int.TryParse(freshnessTextBox.Text, out iVal) || iVal < 100)
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "'Data Timeout (ms)' must be at least 100";
}
return flags;
}
public CfgUpdateFlags UpdateCfg()
{
if (config == null) return CfgUpdateFlags.Error;
config.Name = nameTextBox.Text;
config.ParentName = parentNameComboBox.Text.Equals("---") ? string.Empty : parentNameComboBox.Text;
config.ModbusAddress = (byte)int.Parse(modbusAddressTextBox.Text);
foreach (ModbusReadFunction fn in Enum.GetValues(typeof(ModbusReadFunction)))
{
if (fnComboBox.Text == fn.ToString()) { config.ReadFunction = fn; break; }
}
config.FirstRegister = (ushort)int.Parse(firstRegisterTextBox.Text);
config.RegisterCount = (ushort)int.Parse(registerCountTextBox.Text);
config.EnablePolling = enablePollingCheckBox.Checked;
config.Channel = int.Parse(channelTextBox.Text);
foreach (RawFormat rf in Enum.GetValues(typeof(RawFormat)))
{
if (rawFormatComboBox.Text == rf.ToString()) { config.RawFormat = rf; break; }
}
config.RawAt4mA = double.Parse(raw4TextBox.Text);
config.RawAt20mA = double.Parse(raw20TextBox.Text);
config.CondAt4mA = double.Parse(cond4TextBox.Text);
config.CondAt20mA = double.Parse(cond20TextBox.Text);
config.MsrdUnit = unitTextBox.Text;
config.MsrdFormat = formatTextBox.Text;
config.MsrdValLimLo = double.Parse(limLoTextBox.Text);
config.MsrdValLimHi = double.Parse(limHiTextBox.Text);
config.FreshnessMs = int.Parse(freshnessTextBox.Text);
return CfgUpdateFlags.RestartRqrd;
}
private void diagnosticsButton_Click(object sender, EventArgs e)
{
if (config == null) return;
ConductivityMeter cmpnt =
TbfComponents.FindComponent(config.Name) as ConductivityMeter;
if (cmpnt == null)
{
MessageBox.Show(
"Runtime component was not found. Diagnostics are available only while the component is initialized.",
"Diagnostics",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
cmpnt.ShowDiagnostics();
}
}
}