diff --git a/TestBenchFramework/BenchControl/TestMethods/FlyingStart/FlyingStartSeq.cs b/TestBenchFramework/BenchControl/TestMethods/FlyingStart/FlyingStartSeq.cs index 3053bf36a..6e56a04a7 100644 --- a/TestBenchFramework/BenchControl/TestMethods/FlyingStart/FlyingStartSeq.cs +++ b/TestBenchFramework/BenchControl/TestMethods/FlyingStart/FlyingStartSeq.cs @@ -571,6 +571,15 @@ namespace TBF.BenchControl.TestMethods.FlyingStart meterRslt.VolumeMeter = Math.Abs(iPerl.VolumeLtrEnd - iPerl.VolumeLtrStart); meterRslt.VolumeRef = tstRslt.VolumeCTV * meterRslt.TestTime / tstRslt.TestTime; iPerl.VolumeLtrRef = meterRslt.VolumeRef; +#if IPERL + meterRslt.WaterMeter.CalibFactor = (iPerl.CalibrationStruct != null) ? iPerl.CalibrationStruct.Calibration : 0; + meterRslt.WaterMeter.Q2Correction = iPerl.Q2Correction; + meterRslt.WaterMeter.Q2CorrRFlow = iPerl.Q2CorrRFlow; + meterRslt.WaterMeter.Q2CorrLFlow = iPerl.Q2CorrLFlow; +#endif + iPerl.LastTestResult2 = iPerl.LastTestResult; /// Save shift previous test result + iPerl.LastTestResult = meterRslt; /// Save this test result + iPerl.NominalTestFlow = 500.0 * (test.Qfrom + test.Qto); /// Ave. + convert to liter/hour } else if (cameraRoi != null) { @@ -598,13 +607,6 @@ namespace TBF.BenchControl.TestMethods.FlyingStart && (meterRslt.Error <= test.ErrLimHi - test.Uncertainty); meterRslt.TestDone = true; tstRslt.TestDone = true; - - if (iPerl != null) - { - iPerl.LastTestResult2 = iPerl.LastTestResult; /// Save shift previous test result - iPerl.LastTestResult = meterRslt; /// Save this test result - iPerl.NominalTestFlowLph = 500.0 * (test.Qfrom + test.Qto); /// Ave. + convert to liter/hour - } } } } diff --git a/TestBenchFramework/BenchControl/TestMethods/FlyingStartMassCollection/FlyingStartMassCollectionSeq.cs b/TestBenchFramework/BenchControl/TestMethods/FlyingStartMassCollection/FlyingStartMassCollectionSeq.cs index 235577027..fe9a1e843 100644 --- a/TestBenchFramework/BenchControl/TestMethods/FlyingStartMassCollection/FlyingStartMassCollectionSeq.cs +++ b/TestBenchFramework/BenchControl/TestMethods/FlyingStartMassCollection/FlyingStartMassCollectionSeq.cs @@ -77,8 +77,8 @@ namespace TBF.BenchControl.TestMethods.FlyingStartMassCollection for (int i = 0; i < Config.Data.WMsCount; i++) { TestMethods.iPerlCommunication.iPerlHead.IperlHead iPerl = ((sensPath.RegisterReaders != null) && (i < sensPath.RegisterReaders.Length)) - ? (sensPath.RegisterReaders[i] as TestMethods.iPerlCommunication.iPerlHead.IperlHead) - : null; + ? (sensPath.RegisterReaders[i] as TestMethods.iPerlCommunication.iPerlHead.IperlHead) + : null; if (iPerl != null) { /// TODO: Reimplement @@ -928,6 +928,15 @@ namespace TBF.BenchControl.TestMethods.FlyingStartMassCollection meterRslt.VolumeMeter = Math.Abs(iPerl.VolumeLtrEnd - iPerl.VolumeLtrStart); meterRslt.VolumeRef = tstRslt.VolumeCTV * meterRslt.TestTime / tstRslt.TestTime; iPerl.VolumeLtrRef = meterRslt.VolumeRef; +#if IPERL + meterRslt.WaterMeter.CalibFactor = (iPerl.CalibrationStruct != null) ? iPerl.CalibrationStruct.Calibration : 0; + meterRslt.WaterMeter.Q2Correction = iPerl.Q2Correction; + meterRslt.WaterMeter.Q2CorrRFlow = iPerl.Q2CorrRFlow; + meterRslt.WaterMeter.Q2CorrLFlow = iPerl.Q2CorrLFlow; +#endif + iPerl.LastTestResult2 = iPerl.LastTestResult; /// Save shift previous test result + iPerl.LastTestResult = meterRslt; /// Save this test result + iPerl.NominalTestFlow = 500.0 * (test.Qfrom + test.Qto); /// Ave. + convert to liter/hour } else if (cameraRoi != null) { @@ -955,13 +964,6 @@ namespace TBF.BenchControl.TestMethods.FlyingStartMassCollection && (meterRslt.Error <= test.ErrLimHi - test.Uncertainty); meterRslt.TestDone = true; tstRslt.TestDone = true; - - if (iPerl != null) - { - iPerl.LastTestResult2 = iPerl.LastTestResult; /// Save shift previous test result - iPerl.LastTestResult = meterRslt; /// Save this test result - iPerl.NominalTestFlowLph = 500.0 * (test.Qfrom + test.Qto); /// Ave. + convert to liter/hour - } } } } diff --git a/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs b/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs index d3da9f45a..8b911ba26 100644 --- a/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs +++ b/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs @@ -33,6 +33,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication Verify, WrongIPerlType, CalibFactorOoRange, + MissingTest, } @@ -239,6 +240,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication const string ResetQ2CorrectionStr = "Reset Q2 correction"; const string WriteQ2CorrectionStr = "Write Q2 correction"; const string WriteQ2CorrectionAltStr = "Write Q2 correction Alt"; + const string WriteQ2CorrectionGreeceStr = "Write Q2 correction Greece"; const string Reset2HzCorrectionStr = "Reset 2Hz correction"; const string Write2HzCorrectionStr = "Write 2Hz correction"; @@ -681,8 +683,18 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication else if (activity.ToLower().Contains(WriteCalibrationFactorStr.ToLower())) error = WriteCalibrationFactor(ihead, wm, ref resultStr); else if (activity.ToLower().Equals(NormalizeCalibrationFactorStr.ToLower())) error = NormalizeCalibrationFactor(ihead, wm, ref resultStr); else if (activity.ToLower().Equals(ResetQ2CorrectionStr.ToLower())) error = ResetQ2Correction(ihead, wm, ref resultStr); - else if (activity.ToLower().Equals(WriteQ2CorrectionStr.ToLower())) error = WriteQ2Correction(ihead, wm, ref resultStr, false); /// standard iPerl - else if (activity.ToLower().Equals(WriteQ2CorrectionAltStr.ToLower())) error = WriteQ2Correction(ihead, wm, ref resultStr, true); /// DEWA iPerl + else if (activity.ToLower().Equals(WriteQ2CorrectionStr.ToLower())) + { + error = WriteQ2Correction(ihead, wm, ref resultStr, Q2CorrType.Standard, null, false); + } + else if (activity.ToLower().Equals(WriteQ2CorrectionAltStr.ToLower())) + { + error = WriteQ2Correction(ihead, wm, ref resultStr, Q2CorrType.Dewa, null, false); + } + else if (activity.ToLower().Contains(WriteQ2CorrectionGreeceStr.ToLower())) + { + error = WriteQ2Correction(ihead, wm, ref resultStr, Q2CorrType.Greece, activity.Substring(WriteQ2CorrectionGreeceStr.Length).Trim(), true); + } else if (activity.ToLower().Equals(Reset2HzCorrectionStr.ToLower())) error = Reset2HzCorrection(ihead, wm, ref resultStr); else if (activity.ToLower().Equals(Write2HzCorrectionStr.ToLower())) error = Write2HzCorrection(ihead, wm, ref resultStr); else @@ -914,15 +926,15 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication byte[] cfg_0_3 = null; for (int j = 0; j < cfg.MaxCommRetries; j++) { - if (wm.ConfigStruct == null) + if (ihead.ConfigStruct == null) { - if (0 == ReadRequestPort(wm, MessageID.Configuration, 0, 4, out cfg_0_3, cfg.CommTimeout)) + if (0 == ReadRequestPort(ihead, MessageID.Configuration, 0, 4, out cfg_0_3, cfg.CommTimeout)) { - wm.ConfigStruct.Update(0, cfg_0_3); - if (wm.ConfigStruct.MeterState == MeterState.Active) + ihead.ConfigStruct.Update(0, cfg_0_3); + if (ihead.ConfigStruct.MeterState == MeterState.Active) { error = CommErr.None; - resultStr = wm.ConfigStruct.ToString(1); + resultStr = ihead.ConfigStruct.ToString(1); break; } } @@ -1013,20 +1025,58 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication UInt16.TryParse(arguments[0], out factorLimitLo) && factorLimitLo > 0 && UInt16.TryParse(arguments[1], out factorLimitHi) && factorLimitHi > 0) { - newCalibFactor = ihead.CalculateNewCalibFactor(ihead.CalibrationFactor, factorLimitLo, factorLimitHi); + if (ihead.LastTestResult == null || !ihead.LastTestResult.TestDone) return CommErr.MissingTest; + + newCalibFactor = ihead.CalculateNewCalibFactor(ihead.LastTestResult, ihead.CalibrationFactor, factorLimitLo, factorLimitHi); } else if (arguments.Length == 1 && UInt16.TryParse(arguments[0], out val) && val > 0) { newCalibFactor = val; /// Update with specified value } - else + else if (arguments.Length == 1 && wm.GetTestData(arguments[0]) != null) + { + /// Get test data is test name + Results.Entities.TestData adjustTestData = wm.GetTestData(arguments[0]); + Results.Entities.MeterTestRslt adjustTestRslt; + if (adjustTestData == null) + { + return CommErr.MissingTest; + } + else if (adjustTestData.Repeats == 1) + { + /// Find a test result if Repeats == 1 + adjustTestRslt = wm.GetMeterTestRslt(arguments[0]); + + if (adjustTestRslt == null || !adjustTestRslt.TestDone) return CommErr.MissingTest; + } + else + { + /// Calculate a summarized test result if Repeats > 1 + adjustTestRslt = new Results.Entities.MeterTestRslt(); + for (int i = 1; i <= adjustTestData.Repeats; i++) + { + Results.Entities.MeterTestRslt oneMTR = wm.GetMeterTestRslt(Utils.TestTitle(adjustTestData, i)); + if (oneMTR == null || !oneMTR.TestDone) return CommErr.MissingTest; + + adjustTestRslt.VolumeMeter += oneMTR.VolumeMeter; + adjustTestRslt.VolumeRef += oneMTR.VolumeRef; + } + } + + newCalibFactor = ihead.CalculateNewCalibFactor(adjustTestRslt, ihead.CalibrationFactor, ihead.FactorLimitLo, ihead.FactorLimitHi); + } + else { - newCalibFactor = ihead.CalculateNewCalibFactor(ihead.CalibrationFactor, ihead.FactorLimitLo, ihead.FactorLimitHi); + if (ihead.LastTestResult == null || !ihead.LastTestResult.TestDone) return CommErr.MissingTest; + + newCalibFactor = ihead.CalculateNewCalibFactor(ihead.LastTestResult, ihead.CalibrationFactor, ihead.FactorLimitLo, ihead.FactorLimitHi); } } else { - newCalibFactor = ihead.CalculateNewCalibFactor(ihead.CalibrationFactor, ihead.FactorLimitLo, ihead.FactorLimitHi); + if (ihead.LastTestResult == null || !ihead.LastTestResult.TestDone) return CommErr.MissingTest; + + newCalibFactor = ihead.CalculateNewCalibFactor(ihead.LastTestResult, ihead.CalibrationFactor, ihead.FactorLimitLo, ihead.FactorLimitHi); } if (newCalibFactor == 0) return CommErr.CalibFactorOoRange; @@ -1194,12 +1244,12 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication byte[] rdData = null; for (int j = 0; j < cfg.MaxCommRetries; j++) { - if ((0 == ReadRequestPort(wm, MessageID.MetrologyMemory, Q2CorrFactorsAddr, 2, out rdData, cfg.CommTimeout)) && + if ((0 == ReadRequestPort(ihead, MessageID.MetrologyMemory, Q2CorrFactorsAddr, 2, out rdData, cfg.CommTimeout)) && (rdData != null) && (rdData.Length == 2) && (rdData[0] == 0) && (rdData[1] == 0)) { error = CommErr.None; resultStr = "Q2 correction factors reset to 0"; - wm.Q2CorrectionFactor = 0; + ihead.Q2CorrectionFactor = 0; break; } } @@ -1257,12 +1307,12 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication byte[] rdData = null; for (int j = 0; j < cfg.MaxCommRetries; j++) { - if ((0 == ReadRequestPort(wm, MessageID.MetrologyMemory, Hz2CorrFactorsAddr, 1, out rdData, cfg.CommTimeout)) && + if ((0 == ReadRequestPort(ihead, MessageID.MetrologyMemory, Hz2CorrFactorsAddr, 1, out rdData, cfg.CommTimeout)) && (rdData != null) && (rdData.Length == 2) && (rdData[0] == 0) && (rdData[1] == 0)) { error = CommErr.None; resultStr = "2Hz correction reset to 0"; - wm.Q2CorrectionFactor = 0; + ihead.Q2CorrectionFactor = 0; break; } } @@ -1288,12 +1338,60 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication /// Water meter object /// String passed to caller /// true on success - static CommErr WriteQ2Correction(IperlHead ihead, WaterMeters.WaterMeter.WaterMeter wm, ref string resultStr, bool DEWA) + static CommErr WriteQ2Correction(IperlHead ihead, Results.Entities.WaterMeter wm, ref string resultStr, Q2CorrType q2CorrType, string q2TestName, bool updateQ2adjByCalibFactor) { - if (ihead.CommFailed) return CommErr.CommFailed; - if (ihead.LastTestResult == null) return CommErr.CommFailed; /// This should never happen + //if (ihead == null || ihead.CommFailed) return CommErr.CommFailed; - ihead.Q2ErrorWOCorrection = ihead.LastTestResult.Error; + Results.Entities.MeterTestRslt q2adjResult; + Results.Entities.TestData q2adjTestData; + /// + if (string.IsNullOrEmpty(q2TestName)) + { + /// Use ihead.LastTestResult is no test name is specified as an argument + q2adjResult = ihead.LastTestResult; + q2adjTestData = q2adjResult.TestRslt.TestData; + if (q2adjResult == null || !q2adjResult.TestDone) return CommErr.MissingTest; + } + else + { + /// Get test data is test name + q2adjTestData = wm.GetTestData(q2TestName); + if (q2adjTestData == null) + { + return CommErr.MissingTest; + } + else if (q2adjTestData.Repeats == 1) + { + /// Find a test result if Repets == 1 + q2adjResult = wm.GetMeterTestRslt(q2TestName); + if (q2adjResult == null || !q2adjResult.TestDone) return CommErr.MissingTest; + } + else + { + /// Calculate a summarized test result if Repeats > 1 + q2adjResult = new Results.Entities.MeterTestRslt(); + for (int i = 1; i <= q2adjTestData.Repeats; i++) + { + Results.Entities.MeterTestRslt oneMTR = wm.GetMeterTestRslt(string.Format("{0} ({1}/{2})", q2TestName, i, q2adjTestData.Repeats)); + if (oneMTR == null || !oneMTR.TestDone) return CommErr.MissingTest; + + q2adjResult.VolumeMeter += oneMTR.VolumeMeter; + q2adjResult.VolumeRef += oneMTR.VolumeRef; + } +#if IPERL + if (updateQ2adjByCalibFactor) + { + if (wm.OrigCalibFactor == 0) return CommErr.CalibFactorOoRange; + q2adjResult.VolumeMeter *= ((double)wm.CalibFactor / (double)wm.OrigCalibFactor); + } +#endif + q2adjResult.Error = Formulas.ErrorFromVolumes(q2adjResult.VolumeMeter, q2adjResult.VolumeRef); + } + } + /// + if (q2adjResult == null) return CommErr.CommFailed; /// This should never happen + + ihead.Q2ErrorWOCorrection = q2adjResult.Error; ihead.Q2CorrectionDone = false; ihead.Q2CorrRFlow = 0; @@ -1301,18 +1399,18 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication Byte q2CorrRFlow = 0; Byte q2CorrLFlow = 0; - if (DEWA == false) + if (q2CorrType == Q2CorrType.Standard) { /// /// Standard process /// - if (Math.Abs(ihead.LastTestResult.Error) <= 0.5) + if (Math.Abs(ihead.Q2ErrorWOCorrection) <= 0.5) { resultStr = string.Format("Q2 correction = 0 (writing bypassed)"); return CommErr.None; } - if (!ihead.CalculateQ2CorrectionFactor(ihead.LastTestResult, out q2Correction)) + if (!ihead.CalculateQ2CorrectionFactor(q2adjResult, q2adjTestData, out q2Correction)) { return CommErr.None; /// Q2 error is too large, water meter failed anyhow } @@ -1320,33 +1418,46 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication q2CorrRFlow = (byte)((int)Math.Round(0.5 * q2Correction) & 0x000000FF); q2CorrLFlow = (byte)((int)Math.Round(q2Correction) & 0x000000FF); } - else + else if (q2CorrType == Q2CorrType.Dewa) { /// - /// DEWA process + /// Process for DEWA /// - if (ihead.LastTestResult.Error >= 0 && ihead.LastTestResult.Error <= 1.0) + if (ihead.Q2ErrorWOCorrection >= 0 && ihead.Q2ErrorWOCorrection <= 1.0) { resultStr = string.Format("Q2 correction = 0 (writing bypassed)"); return CommErr.None; } - if (!ihead.CalculateQ2CorrectionFactor(ihead.LastTestResult, out q2Correction)) + if (!ihead.CalculateQ2CorrectionFactor(q2adjResult, q2adjTestData, out q2Correction)) { return CommErr.None; /// Q2 error is too large, water meter failed anyhow } - if (ihead.LastTestResult.Error < 0) + if (ihead.Q2ErrorWOCorrection < 0) { q2CorrRFlow = (byte)((int)Math.Round(1.1 * q2Correction) & 0x000000FF); q2CorrLFlow = (byte)((int)Math.Round(1.1 * q2Correction) & 0x000000FF); } - else /// if (wm.LastTestResult.Error > 1.0) + else /// if (wm.Q2ErrorWOCorrection > 1.0) { q2CorrRFlow = (byte)((int)Math.Round(0.5 * q2Correction) & 0x000000FF); q2CorrLFlow = (byte)((int)Math.Round(0.5 * q2Correction) & 0x000000FF); } } + else if (q2CorrType == Q2CorrType.Greece) + { + /// + /// Process for Greece + /// + if (!ihead.CalculateQ2CorrectionFactor(q2adjResult, q2adjTestData, out q2Correction)) + { + return CommErr.None; /// Q2 error is too large, water meter failed anyhow + } + + q2CorrRFlow = (byte)((int)Math.Round(q2Correction) & 0x000000FF); + q2CorrLFlow = (byte)((int)Math.Round(q2Correction) & 0x000000FF); + } if (OpenPort(ihead) != 0) return CommErr.OpenPort; /// Open RFID port @@ -1375,7 +1486,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication byte[] rdData = null; for (int j = 0; j < cfg.MaxCommRetries; j++) { - if ((0 == ReadRequestPort(wm, MessageID.MetrologyMemory, Q2CorrFactorsAddr, 2, out rdData, cfg.CommTimeout)) && + if ((0 == ReadRequestPort(ihead, MessageID.MetrologyMemory, Q2CorrFactorsAddr, 2, out rdData, cfg.CommTimeout)) && (rdData != null) && (rdData.Length == 2) && (rdData[0] == q2CorrRFlow) && (rdData[1] == q2CorrLFlow)) { error = CommErr.None; @@ -1388,7 +1499,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication #if IPERL if (wm != null) { - wm.Q2ErrorWOCorrection = ihead.Q2ErrorWOCorrection; + wm.Q2ErrWOCorrection = ihead.Q2ErrorWOCorrection; wm.Q2CorrectionDone = ihead.Q2CorrectionDone; wm.Q2Correction = ihead.Q2Correction; wm.Q2CorrRFlow = ihead.Q2CorrRFlow; @@ -1490,8 +1601,16 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication { error = CommErr.None; resultStr = string.Format("2Hz factor = {0}", (SByte)hz2CorrectionByte); - rfidDataLogger.Warn(wm.Name + ": " + resultStr); - wm.Hz2CorrectionFactor = hz2CorrectionFactor; + rfidDataLogger.Warn(ihead.Name + ": " + resultStr); + ihead.Hz2CorrectionFactor = hz2CorrectionFactor; + #if IPERL + if (wm != null) + { + wm.Diff2Hz8Hz = ihead.Diff2Hz8Hz; + wm.Hz2CorrectionDone = ihead.Hz2CorrectionDone; + wm.Hz2Correction = ihead.Hz2CorrectionFactor; + } +#endif break; } } @@ -1503,6 +1622,14 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication rfidDataLogger.Warn(ihead.Name + ": " + resultStr); ihead.Hz2CorrectionDone = true; ihead.Hz2CorrectionFactor = hz2CorrectionFactor; + #if IPERL + if (wm != null) + { + wm.Diff2Hz8Hz = ihead.Diff2Hz8Hz; + wm.Hz2CorrectionDone = ihead.Hz2CorrectionDone; + wm.Hz2Correction = ihead.Hz2CorrectionFactor; + } + #endif } #endif