MiniPrf: - input checks extended

This commit is contained in:
Thomas Wiedebusch 2026-01-24 14:32:13 +01:00
parent 487ceee7b4
commit 19b50c4707
3 changed files with 267 additions and 151 deletions

View File

@ -1131,7 +1131,6 @@ namespace Sensus.Common.Hardware.WaterMeter.MechanicalMeter.FM2014.FM2014Core
private Boolean Read(CmdName cmdName, out String responseStr)
{
responseStr = "";
var cmdCodeStr = GetCmdStr(cmdName);
var info = $"{Resources.StrCommResponse}: {GetCmdInfo(cmdName)}";
var statusReturn = StatusReturn.Failed;

View File

@ -316,6 +316,7 @@
this.cbxAttenuation.TabIndex = 6;
this.cbxAttenuation.Text = "3";
this.cbxAttenuation.SelectedIndexChanged += new System.EventHandler(this.cbxAttenuation_SelectedIndexChanged);
this.cbxAttenuation.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cbxAttenuation_KeyDown);
//
// lblRefPulsePerVolume
//

View File

@ -42,6 +42,8 @@ using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sensus.Common.Hardware.WaterMeter.MechanicalMeter.FM2014.FM2014Core.Consts;
@ -582,16 +584,18 @@ namespace Sensus.MiniPrf.Ui
tbxScaleRefToDut.Text = $@"{Fm2014.RefToDutScale_norm:F4}";
// Display error if scale doesn't fit
if (Math.Abs(Fm2014.RefToDutScale_norm -
(Single)Fm2014.Ref_pulse_per_cm / Fm2014.Dut_pulse_per_cm) > FM2014.RefToDutScaleMin ||
var tempRefToDutScale_norm = (Double)Fm2014.Ref_pulse_per_cm / Fm2014.Dut_pulse_per_cm;
if (tempRefToDutScale_norm < FM2014.RefToDutScaleMin ||
tempRefToDutScale_norm > FM2014.RefToDutScaleMax ||
Fm2014.RefToDutScale_norm < FM2014.RefToDutScaleMin ||
Fm2014.RefToDutScale_norm > FM2014.RefToDutScaleMax)
{
{
tbxScaleRefToDut.BackColor = ColorProcessFailed;
tbxScaleRefToDut.Text = Resources.StrError;
btnSaveRegulationSetup.Enabled = false;
return false;
}
return true;
}
@ -853,14 +857,35 @@ namespace Sensus.MiniPrf.Ui
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2023-Jan-14..18" author="Thomas Wiedebusch">
/// <remarks date="2023-Jan-14..24" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
private void tbxRefPulsesPerCm_KeyDown(Object sender, KeyEventArgs e)
{
// Catch the enter key, recalculate all settings
if (e.KeyCode == Keys.Enter)
{
tbxRefPulsesPerCm_Leave(this, null);
SelectNextControl(ActiveControl, true, true, true, true);
}
else if (e.KeyCode != Keys.Back &&
e.KeyCode != Keys.Left &&
e.KeyCode != Keys.Right &&
!Regex.IsMatch($"{e.KeyCode}", @"[0-9]"))
{
e.SuppressKeyPress = true;
}
else
{
// Take the new input and calculate the results after the input has been taken after this event!
Task.Factory.StartNew(() =>
{
Thread.Sleep(1);
}).ContinueWith(delegate
{
tbxRefPulsesPerCm_Leave(this, null);
});
}
}
@ -869,157 +894,35 @@ namespace Sensus.MiniPrf.Ui
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2023-Jan-14..20" author="Thomas Wiedebusch">
/// <remarks date="2023-Jan-14..24" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
private void tbxRefPulsesPerCm_Leave(Object sender, EventArgs e)
{
// Setup FM2014
if (Fm2014 != null && int.TryParse(tbxRefPulsePerCm.Text, out var pulses_per_cm))
Invoke(new Action(() =>
{
//Backup the actual setting to detect changes
var backupPulses_per_cm = Fm2014.Ref_pulse_per_cm;
Fm2014.Ref_pulse_per_cm = (UInt32)pulses_per_cm;
// Output the Fm2014 setting to avoid wrong display of invalid ranges as this will be
// limited during the setup of the FM2014 property!
tbxRefPulsePerCm.Text = $@"{Fm2014.Ref_pulse_per_cm:D}";
// During setup of the 'Ref_pulse_per_cm' the 'RefToDutScaleStr' will be generated
if (!UpdateRefToDutScale())
{
tbxRefPulsePerCm.BackColor = ColorProcessFailed;
if (Fm2014 == null)
return;
}
// Check if values have changed and need to be updated in the standalone setup
if (backupPulses_per_cm != Fm2014.Ref_pulse_per_cm)
if (string.IsNullOrEmpty(tbxRefPulsePerCm.Text))
{
SetupHasChanged();
}
// SPECIAL BACKUP AND RESTORE FOR REF CALIBRATION
// If the REF pulses per cubic meter have been changed manually, the 'Manual REF Calibration'
// fields have to be cleared if those are different. A backup to restore it may be useful.
if (!tbxRefPulsePerCm.Text.Equals(tbxRefCalibrationResultPulsePerCm.Text) &&
!string.IsNullOrEmpty(tbxRefCalibrationResultPulsePerCm.Text))
{
_backupMeasuredRefPulsesStr = tbxMeasuredRefPulses.Text;
tbxMeasuredRefPulses.Text = "";
_backupRefCalibrationResultPulsePerCmStr = tbxRefCalibrationResultPulsePerCm.Text;
tbxRefCalibrationResultPulsePerCm.Text = "";
_backupWeightScaleVolumeLitersStr = tbxManualInputVolumeLiters.Text;
tbxManualInputVolumeLiters.Text = "";
}
// Restore values if REF pulses per cubic meters is back to the calibrated one
else if (tbxRefPulsePerCm.Text.Equals(_backupRefCalibrationResultPulsePerCmStr))
{
tbxMeasuredRefPulses.Text = _backupMeasuredRefPulsesStr;
tbxRefCalibrationResultPulsePerCm.Text = _backupRefCalibrationResultPulsePerCmStr;
tbxManualInputVolumeLiters.Text = _backupWeightScaleVolumeLitersStr;
}
}
}
/// <summary>
/// Redirect keystroke 'Enter' to leaving the cell event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2023-Jan-14" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
private void tbxDutPulsesPerCm_KeyDown(Object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
tbxDutPulsesPerCm_Leave(this, null);
}
}
/// <summary>
/// After manual input of the DUT pulses per cubic meter the scale REF to DUT has to be recalculated
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2023-Jan-14..20" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
private void tbxDutPulsesPerCm_Leave(Object sender, EventArgs e)
{
// Setup FM2014
if (Fm2014 != null && int.TryParse(tbxDutPulsePerCm.Text, out var pulses_per_cm))
{
//Backup the actual setting to detect changes
var backupPulses_per_cm = Fm2014.Dut_pulse_per_cm;
// Try to set the new calculated REF pulses limited by the property setter
Fm2014.Dut_pulse_per_cm = (UInt32)pulses_per_cm;
// Output the Fm2014 setting to avoid wrong display of invalid ranges as this will be
// limited during the setup of the FM2014 property!
tbxDutPulsePerCm.Text = $@"{Fm2014.Dut_pulse_per_cm:D}";
// During setup of the 'Dut_pulse_per_cm' the 'RefToDutScaleStr' will be generated
if (!UpdateRefToDutScale())
{
tbxDutPulsePerCm.BackColor = ColorProcessFailed;
return;
}
// Check if values have changed and need to be updated in the standalone setup
if (backupPulses_per_cm != Fm2014.Dut_pulse_per_cm)
{
SetupHasChanged();
}
}
}
/// <summary>
/// Redirect keystroke 'Enter' to leaving the cell event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2023-Jan-15" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
private void tbxManualInputVolumeLiters_KeyStoke(Object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
tbxManualInputVolumeLiters_Leave(this, null);
}
}
/// <summary>
/// After manual input of measured weight scale or reservoir in liters this will calculate the
/// REF pulses per cubic meter and copy from REF pulses per cubic meter in 'Manual REF Calibration'
/// to 'Regulation Setup'.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2023-Jan-15..20" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
private void tbxManualInputVolumeLiters_Leave(Object sender, EventArgs e)
{
// Calculate REF pulses per cubic meter
if (int.TryParse(tbxMeasuredRefPulses.Text, out var pulses) && pulses > 0 &&
int.TryParse(tbxManualInputVolumeLiters.Text, out var liters) && liters > 0)
{
//Backup the actual setting to detect changes
var backupPulses_per_cm = Fm2014.Ref_pulse_per_cm;
// Calculate the new pulse ratio based on the manual calibration
var pulses_per_cm = (UInt16)(pulses / (liters / 1000.0f));
// Try to set the new calculated REF pulses limited by the property setter
Fm2014.Ref_pulse_per_cm = pulses_per_cm;
// If this succeeded, then update the new manual calibrated value
if (pulses_per_cm == Fm2014.Ref_pulse_per_cm)
{
tbxRefCalibrationResultPulsePerCm.Text = $@"{Fm2014.Ref_pulse_per_cm:D}";
tbxRefPulsePerCm.Text = $@"{Fm2014.Ref_pulse_per_cm:D}";
tbxManualInputVolumeLiters.BackColor = ColorStandardInputField;
tbxRefCalibrationResultPulsePerCm.BackColor = ColorStandardDisplayField;
}
// Setup FM2014
if (int.TryParse(tbxRefPulsePerCm.Text, out var pulses_per_cm))
{
//Backup the actual setting to detect changes
var backupPulses_per_cm = Fm2014.Ref_pulse_per_cm;
Fm2014.Ref_pulse_per_cm = (UInt32)pulses_per_cm;
// Output the Fm2014 setting to avoid wrong display of invalid ranges as this will be
// limited during the setup of the FM2014 property!
tbxRefPulsePerCm.Text = $@"{Fm2014.Ref_pulse_per_cm:D}";
// During setup of the 'Ref_pulse_per_cm' the 'RefToDutScaleStr' will be generated
if (!UpdateRefToDutScale())
{
tbxManualInputVolumeLiters.BackColor = ColorProcessFailed;
tbxRefPulsePerCm.BackColor = ColorProcessFailed;
return;
}
@ -1028,14 +931,213 @@ namespace Sensus.MiniPrf.Ui
{
SetupHasChanged();
}
// SPECIAL BACKUP AND RESTORE FOR REF CALIBRATION
// If the REF pulses per cubic meter have been changed manually, the 'Manual REF Calibration'
// fields have to be cleared if those are different. A backup to restore it may be useful.
if (!tbxRefPulsePerCm.Text.Equals(tbxRefCalibrationResultPulsePerCm.Text) &&
!string.IsNullOrEmpty(tbxRefCalibrationResultPulsePerCm.Text))
{
_backupMeasuredRefPulsesStr = tbxMeasuredRefPulses.Text;
tbxMeasuredRefPulses.Text = "";
_backupRefCalibrationResultPulsePerCmStr = tbxRefCalibrationResultPulsePerCm.Text;
tbxRefCalibrationResultPulsePerCm.Text = "";
_backupWeightScaleVolumeLitersStr = tbxManualInputVolumeLiters.Text;
tbxManualInputVolumeLiters.Text = "";
}
// Restore values if REF pulses per cubic meters is back to the calibrated one
else if (tbxRefPulsePerCm.Text.Equals(_backupRefCalibrationResultPulsePerCmStr))
{
tbxMeasuredRefPulses.Text = _backupMeasuredRefPulsesStr;
tbxRefCalibrationResultPulsePerCm.Text = _backupRefCalibrationResultPulsePerCmStr;
tbxManualInputVolumeLiters.Text = _backupWeightScaleVolumeLitersStr;
}
}
else
{
tbxManualInputVolumeLiters.BackColor = ColorProcessFailed;
tbxRefCalibrationResultPulsePerCm.BackColor = ColorProcessFailed;
tbxRefCalibrationResultPulsePerCm.Text = Resources.StrError;
}
}));
}
/// <summary>
/// Redirect keystroke 'Enter' to leaving the cell event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2023-Jan-14..24" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
private void tbxDutPulsesPerCm_KeyDown(Object sender, KeyEventArgs e)
{
// Catch the enter key, recalculate all settings
if (e.KeyCode == Keys.Enter)
{
tbxDutPulsesPerCm_Leave(this, null);
SelectNextControl(ActiveControl, true, true, true, true);
}
else if (e.KeyCode != Keys.Back &&
e.KeyCode != Keys.Left &&
e.KeyCode != Keys.Right &&
!Regex.IsMatch($"{e.KeyCode}", @"[0-9]"))
{
e.SuppressKeyPress = true;
}
else
{
// Take the new input and calculate the results after the input has been taken after this event!
Task.Factory.StartNew(() =>
{
Thread.Sleep(1);
}).ContinueWith(delegate
{
tbxDutPulsesPerCm_Leave(this, null);
});
}
}
/// <summary>
/// After manual input of the DUT pulses per cubic meter the scale REF to DUT has to be recalculated
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2023-Jan-14..24" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
private void tbxDutPulsesPerCm_Leave(Object sender, EventArgs e)
{
Invoke(new Action(() =>
{
if (Fm2014 == null)
return;
if (string.IsNullOrEmpty(tbxDutPulsePerCm.Text))
{
tbxDutPulsePerCm.Text = $@"{Fm2014.Dut_pulse_per_cm:D}";
}
// Setup FM2014
if (int.TryParse(tbxDutPulsePerCm.Text, out var pulses_per_cm))
{
//Backup the actual setting to detect changes
var backupPulses_per_cm = Fm2014.Dut_pulse_per_cm;
// Try to set the new calculated REF pulses limited by the property setter
Fm2014.Dut_pulse_per_cm = (UInt32)pulses_per_cm;
// Output the Fm2014 setting to avoid wrong display of invalid ranges as this will be
// limited during the setup of the FM2014 property!
tbxDutPulsePerCm.Text = $@"{Fm2014.Dut_pulse_per_cm:D}";
// During setup of the 'Dut_pulse_per_cm' the 'RefToDutScaleStr' will be generated
if (!UpdateRefToDutScale())
{
tbxDutPulsePerCm.BackColor = ColorProcessFailed;
return;
}
// Check if values have changed and need to be updated in the standalone setup
if (backupPulses_per_cm != Fm2014.Dut_pulse_per_cm)
{
SetupHasChanged();
}
}
}));
}
/// <summary>
/// Redirect keystroke 'Enter' to leaving the cell event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2023-Jan-15..24" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
private void tbxManualInputVolumeLiters_KeyStoke(Object sender, KeyEventArgs e)
{
// Catch the enter key, recalculate all settings
if (e.KeyCode == Keys.Enter)
{
tbxManualInputVolumeLiters_Leave(this, null);
SelectNextControl(ActiveControl, true, true, true, true);
}
else if (e.KeyCode != Keys.Back &&
e.KeyCode != Keys.Left &&
e.KeyCode != Keys.Right &&
!Regex.IsMatch($"{e.KeyCode}", @"[0-9]"))
{
e.SuppressKeyPress = true;
}
else
{
// Take the new input and calculate the results after the input has been taken after this event!
Task.Factory.StartNew(() =>
{
Thread.Sleep(1);
}).ContinueWith(delegate
{
tbxManualInputVolumeLiters_Leave(this, null);
});
}
}
/// <summary>
/// After manual input of measured weight scale or reservoir in liters this will calculate the
/// REF pulses per cubic meter and copy from REF pulses per cubic meter in 'Manual REF Calibration'
/// to 'Regulation Setup'.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2023-Jan-15..24" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
private void tbxManualInputVolumeLiters_Leave(Object sender, EventArgs e)
{
Invoke(new Action(() =>
{
if (Fm2014 == null)
return;
if (string.IsNullOrEmpty(tbxRefPulsePerCm.Text))
{
tbxManualInputVolumeLiters.Text = @"0";
}
// Calculate REF pulses per cubic meter
if (int.TryParse(tbxMeasuredRefPulses.Text, out var pulses) && pulses > 0 &&
int.TryParse(tbxManualInputVolumeLiters.Text, out var liters) && liters > 0)
{
//Backup the actual setting to detect changes
var backupPulses_per_cm = Fm2014.Ref_pulse_per_cm;
// Calculate the new pulse ratio based on the manual calibration
var pulses_per_cm = (UInt16)(pulses / (liters / 1000.0f));
// Try to set the new calculated REF pulses limited by the property setter
Fm2014.Ref_pulse_per_cm = pulses_per_cm;
// If this succeeded, then update the new manual calibrated value
if (pulses_per_cm == Fm2014.Ref_pulse_per_cm)
{
tbxRefCalibrationResultPulsePerCm.Text = $@"{Fm2014.Ref_pulse_per_cm:D}";
tbxRefPulsePerCm.Text = $@"{Fm2014.Ref_pulse_per_cm:D}";
tbxManualInputVolumeLiters.BackColor = ColorStandardInputField;
tbxRefCalibrationResultPulsePerCm.BackColor = ColorStandardDisplayField;
if (!UpdateRefToDutScale())
{
tbxManualInputVolumeLiters.BackColor = ColorProcessFailed;
return;
}
// Check if values have changed and need to be updated in the standalone setup
if (backupPulses_per_cm != Fm2014.Ref_pulse_per_cm)
{
SetupHasChanged();
}
}
else
{
tbxManualInputVolumeLiters.BackColor = ColorProcessFailed;
tbxRefCalibrationResultPulsePerCm.BackColor = ColorProcessFailed;
tbxRefCalibrationResultPulsePerCm.Text = Resources.StrError;
}
}
}));
}
/// <summary>
@ -1054,6 +1156,19 @@ namespace Sensus.MiniPrf.Ui
}
/// <summary>
/// Select next control on enter key pressed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbxAttenuation_KeyDown(Object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SelectNextControl(ActiveControl, true, true, true, true);
}
}
#endregion Buttons and Controls
#region Event handler
@ -1178,5 +1293,6 @@ namespace Sensus.MiniPrf.Ui
}
#endregion
}
}