From 19b50c4707d480becd0b54a89cbc6a8a3dcb6ac1 Mon Sep 17 00:00:00 2001 From: Thomas Wiedebusch Date: Sat, 24 Jan 2026 14:32:13 +0100 Subject: [PATCH] MiniPrf: - input checks extended --- .../FM2014/FM2014Core/FM2014.cs | 1 - MiniPrf/Ui/FrmMainMiniPrf.Designer.cs | 1 + MiniPrf/Ui/FrmMainMiniPrf.cs | 416 +++++++++++------- 3 files changed, 267 insertions(+), 151 deletions(-) diff --git a/Common/Hardware/WaterMeter/MechanicalMeter/FM2014/FM2014Core/FM2014.cs b/Common/Hardware/WaterMeter/MechanicalMeter/FM2014/FM2014Core/FM2014.cs index b6969c17..ffe42737 100644 --- a/Common/Hardware/WaterMeter/MechanicalMeter/FM2014/FM2014Core/FM2014.cs +++ b/Common/Hardware/WaterMeter/MechanicalMeter/FM2014/FM2014Core/FM2014.cs @@ -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; diff --git a/MiniPrf/Ui/FrmMainMiniPrf.Designer.cs b/MiniPrf/Ui/FrmMainMiniPrf.Designer.cs index 3c01eb49..5039cd5d 100644 --- a/MiniPrf/Ui/FrmMainMiniPrf.Designer.cs +++ b/MiniPrf/Ui/FrmMainMiniPrf.Designer.cs @@ -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 // diff --git a/MiniPrf/Ui/FrmMainMiniPrf.cs b/MiniPrf/Ui/FrmMainMiniPrf.cs index cb204144..58553179 100644 --- a/MiniPrf/Ui/FrmMainMiniPrf.cs +++ b/MiniPrf/Ui/FrmMainMiniPrf.cs @@ -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 /// /// /// - /// + /// /// - Initial. /// 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 /// /// /// - /// + /// /// - Initial. /// 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; - } - } - } - /// - /// Redirect keystroke 'Enter' to leaving the cell event - /// - /// - /// - /// - /// - Initial. - /// - private void tbxDutPulsesPerCm_KeyDown(Object sender, KeyEventArgs e) - { - if (e.KeyCode == Keys.Enter) - { - tbxDutPulsesPerCm_Leave(this, null); - } - } - /// - /// After manual input of the DUT pulses per cubic meter the scale REF to DUT has to be recalculated - /// - /// - /// - /// - /// - Initial. - /// - 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(); - } - } - } - - /// - /// Redirect keystroke 'Enter' to leaving the cell event - /// - /// - /// - /// - /// - Initial. - /// - private void tbxManualInputVolumeLiters_KeyStoke(Object sender, KeyEventArgs e) - { - if (e.KeyCode == Keys.Enter) - { - tbxManualInputVolumeLiters_Leave(this, null); - } - } - /// - /// 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'. - /// - /// - /// - /// - /// - Initial. - /// - 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; - } + })); + } + /// + /// Redirect keystroke 'Enter' to leaving the cell event + /// + /// + /// + /// + /// - Initial. + /// + 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); + }); + } + } + + /// + /// After manual input of the DUT pulses per cubic meter the scale REF to DUT has to be recalculated + /// + /// + /// + /// + /// - Initial. + /// + 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(); + } + } + })); + } + + /// + /// Redirect keystroke 'Enter' to leaving the cell event + /// + /// + /// + /// + /// - Initial. + /// + 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); + }); + } + } + + /// + /// 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'. + /// + /// + /// + /// + /// - Initial. + /// + 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; + } + } + })); + } /// @@ -1054,6 +1156,19 @@ namespace Sensus.MiniPrf.Ui } + /// + /// Select next control on enter key pressed + /// + /// + /// + 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 + } }