From 5f5ac87f4279338b6d31ae934257317ab3a60767 Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Fri, 14 Aug 2015 19:20:47 +0200 Subject: [PATCH] iPerlCommunication: testModeConfig parsed and set, correct wm.Disabled behavior. --- .../iPerlCommunicationForm.cs | 125 +++++++++++++----- .../WaterMeters/iPerl/ConfigStruct.cs | 16 ++- .../WaterMeters/iPerl/WaterMeter.cs | 11 +- 3 files changed, 110 insertions(+), 42 deletions(-) diff --git a/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs b/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs index 26dc101e0..6c926da2d 100644 --- a/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs +++ b/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.Globalization; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; @@ -75,8 +76,8 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication const int CommTimeout = 500; const int MaxCommRetries = 3; - const string ReadConfigurationStr = "Read configuration"; - const string SetTestModeStr = "Set Test mode"; /// Example: "Set Test mode 0xA0" + const string ReadConfigurationStr = "Read configuration"; /// Example: "Read configuration" or "Read configuration if enabled" + const string SetTestModeStr = "Set Test mode"; /// Example: "Set Test mode" or "Set Test mode A0" (hexadecimal number is the required 'testModeConfig' const string SetActiveModeStr = "Set Active mode"; const string ReadCalibrationStr = "Read calibration"; const string WriteCalibrationFactorStr = "Write calibration factor"; @@ -401,7 +402,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication wmFound = true; if (wm.DebugLevel == Entities.DebugMode.Normal) { - if (activity.ToLower().Equals(ReadConfigurationStr.ToLower())) ReadConfiguration(wm, threadId, wmNr); + if (activity.ToLower().Contains(ReadConfigurationStr.ToLower())) ReadConfiguration(wm, threadId, wmNr); else if (activity.ToLower().Contains(SetTestModeStr.ToLower())) SetTestMode(wm, threadId, wmNr); else if (activity.ToLower().Equals(SetActiveModeStr.ToLower())) SetActiveMode(wm, threadId, wmNr); else if (activity.ToLower().Equals(ReadCalibrationStr.ToLower())) ReadCalibration(wm, threadId, wmNr); @@ -435,6 +436,13 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication bool success = false; byte[] config = null; + /// + /// The activity is "Read configuration" (this enables the watermeter, resets error flag) + /// or "Read configuration if enabled" (this keeps th error flag). + /// + if (!activity.ToLower().Contains(" if enabled")) { wm.Disabled = false; } + + if (!wm.Disabled) { success = (0 == openPort(wm.RfidComPortNr)); /// Open RFID port @@ -464,6 +472,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication } else { + wm.Disabled = true; OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, "Cannot read configuration")); } } @@ -480,44 +489,92 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication static void SetTestMode(WaterMeters.iPerl.WaterMeter wm, int threadId, int wmNr) { bool success = false; - byte[] cfg_0_3 = null; if (!wm.Disabled) { - string testModeConfigStr; /// TODO - Byte testModeConfig; /// TODO - - success = (openPort(wm.RfidComPortNr) == 0); /// Open RFID port - - if (success) + Byte testModeConfig = 0xA0; /// Default value + /// + if (activity.Length > SetTestModeStr.Length) { - success = false; - - /// Switch to test mode - byte[] cmd = new byte[1] { (byte)7 }; - for (int j = 0; j < MaxCommRetries; j++) + string testModeConfigStr = activity.Substring(SetTestModeStr.Length + 1); + UInt16 byteVal; + if (UInt16.TryParse(testModeConfigStr, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out byteVal) && byteVal <= 255) { - if (0 == WriteRequestPort(MessageID.Command, 0, 1, cmd, CommTimeout)) { success = true; break; } + testModeConfig = (Byte)byteVal; /// Update with specified value } } - if (success) + if (wm.ConfigStruct.MeterState == MeterState.Test && wm.ConfigStruct.TestModeConfig == testModeConfig) { - success = false; - - /// Read configuration - for (int j = 0; j < MaxCommRetries; j++) - { - if (0 == ReadRequestPort(MessageID.Configuration, 0, 4, out cfg_0_3, CommTimeout)) { success = true; break; } - } + /// Already in the correct test mode + success = true; } + else + { + /// Communication necessary + success = (openPort(wm.RfidComPortNr) == 0); /// Open RFID port - closePort(); /// Close RFID port + if (success && (wm.ConfigStruct.TestModeConfig != testModeConfig) && (wm.ConfigStruct.MeterState != MeterState.Active)) + { + /// Switch to Active mode in order to change TestModeConfig + success = false; + byte[] cmd = new byte[1] { (byte)6 }; + for (int j = 0; j < MaxCommRetries; j++) + { + if (0 == WriteRequestPort(MessageID.Command, 0, 1, cmd, CommTimeout)) { success = true; break; } + } + } + + if (success && (wm.ConfigStruct.TestModeConfig != testModeConfig)) + { + /// Change the TestModeConfig if necessary + success = false; + byte[] tstMdCfg = new byte[1] { testModeConfig }; + for (int j = 0; j < MaxCommRetries; j++) + { + if (0 == WriteRequestPort(MessageID.Configuration, 21, 1, tstMdCfg, CommTimeout)) + { + wm.ConfigStruct.Update(21, tstMdCfg); + success = true; + break; + } + } + } + + if (success) + { + /// Switch to test mode + success = false; + byte[] cmd = new byte[1] { (byte)7 }; + for (int j = 0; j < MaxCommRetries; j++) + { + if (0 == WriteRequestPort(MessageID.Command, 0, 1, cmd, CommTimeout)) { success = true; break; } + } + } + + /// Now the meter should be in the Test mode ... verify + if (success) + { + /// Verify the configuration + success = false; + byte[] cfg_0_3 = null; + for (int j = 0; j < MaxCommRetries; j++) + { + if (0 == ReadRequestPort(MessageID.Configuration, 0, 4, out cfg_0_3, CommTimeout)) + { + wm.ConfigStruct.Update(0, cfg_0_3); + success = (wm.ConfigStruct.MeterState == MeterState.Test); + break; + } + } + } + + closePort(); /// Close RFID port + } } if (success) { - wm.ConfigStruct.Update(cfg_0_3, 0); OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, wm.ConfigStruct.ToString(1))); } else if (wm.Disabled) @@ -526,6 +583,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication } else { + wm.Disabled = true; OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, "'Set test mode' failed")); } } @@ -540,7 +598,6 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication static void SetActiveMode(WaterMeters.iPerl.WaterMeter wm, int threadId, int wmNr) { bool success = false; - byte[] cfg_0_3 = null; if (!wm.Disabled) { @@ -563,9 +620,15 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication success = false; /// Read configuration + byte[] cfg_0_3 = null; for (int j = 0; j < MaxCommRetries; j++) { - if (0 == ReadRequestPort(MessageID.Configuration, 0, 4, out cfg_0_3, CommTimeout)) { success = true; break; } + if (0 == ReadRequestPort(MessageID.Configuration, 0, 4, out cfg_0_3, CommTimeout)) + { + wm.ConfigStruct.Update(0, cfg_0_3); + success = true; + break; + } } } @@ -574,7 +637,6 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication if (success) { - wm.ConfigStruct.Update(cfg_0_3, 0); OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, wm.ConfigStruct.ToString(1))); } else if (wm.Disabled) @@ -583,6 +645,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication } else { + wm.Disabled = true; OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, "Cannot switch to active mode")); } } @@ -612,7 +675,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication /// Read calibration for (int j = 0; j < MaxCommRetries; j++) { - if (0 == ReadRequestPort(MessageID.Configuration, 0, CalibrationStruct.Length, out calib, CommTimeout)) { success = true; break; } + if (0 == ReadRequestPort(MessageID.Calibration, 0, CalibrationStruct.Length, out calib, CommTimeout)) { success = true; break; } } } @@ -630,6 +693,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication } else { + wm.Disabled = true; OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, "Cannot read calibration")); } } @@ -690,6 +754,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication } else { + wm.Disabled = true; OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, "Cannot switch to active mode")); } } diff --git a/TestBenchFramework/BenchControl/WaterMeters/iPerl/ConfigStruct.cs b/TestBenchFramework/BenchControl/WaterMeters/iPerl/ConfigStruct.cs index efcc3a278..b38654987 100644 --- a/TestBenchFramework/BenchControl/WaterMeters/iPerl/ConfigStruct.cs +++ b/TestBenchFramework/BenchControl/WaterMeters/iPerl/ConfigStruct.cs @@ -112,28 +112,34 @@ namespace TBF.BenchControl.WaterMeters.iPerl /// /// Update the configuration structure from an incomplete byte array /// - /// Byte array data /// Offset of byte array data in ConfigStruct + /// Byte array data /// true when successful, false when data are not appropriate - public bool Update(byte[] data, int offset) + public bool Update(int offset, byte[] data) { if (offset == 0 && data.Length == 2) { - /// Data containing an iPerl mode of function + /// iPerl mode of function Version = data[0]; MeterState = (MeterState)data[1]; return true; } else if (offset == 0 && data.Length == 4) { - /// Data containing an iPerl mode of function + /// iPerl mode of function and extra 2 bytes Version = data[0]; MeterState = (MeterState)data[1]; return true; } + else if (offset == 21 && data.Length == 1) + { + /// TestModeConfig value + TestModeConfig = data[21 - offset]; + return true; + } else if (offset == 0 && data.Length == Length) { - /// Data containing a complete ConfigStruct + /// Complete ConfigStruct Version = data[0]; MeterState = (MeterState)data[1]; TargetTimeVeryLowBatt = (((UInt32)data[5] * 256 + data[4]) * 256 + data[3]) * 256 + data[2]; diff --git a/TestBenchFramework/BenchControl/WaterMeters/iPerl/WaterMeter.cs b/TestBenchFramework/BenchControl/WaterMeters/iPerl/WaterMeter.cs index 5cb4138c9..2d209ef3f 100644 --- a/TestBenchFramework/BenchControl/WaterMeters/iPerl/WaterMeter.cs +++ b/TestBenchFramework/BenchControl/WaterMeters/iPerl/WaterMeter.cs @@ -68,23 +68,20 @@ namespace TBF.BenchControl.WaterMeters.iPerl } bool disabled; - /// - /// ConfigStruct of the water meter obtained or updated by iPerlCommunication - /// + /// ConfigStruct of the water meter obtained or updated by iPerlCommunication public ConfigStruct ConfigStruct; - /// - /// CalibrationStruct of the water meter obtained or updated by iPerlCommunication - /// + /// CalibrationStruct of the water meter obtained or updated by iPerlCommunication public CalibrationStruct CalibrationStruct; + public ushort CalibrationFactor { get { return CalibrationStruct.Calibration; } } public ushort CalculatedCalibrationFactor { get { - return (UInt16)((double)CalibrationStruct.Calibration * 1.1); /// TODO: real calculation + return (UInt16)((double)CalibrationStruct.Calibration + 0.5); /// TODO: real calculation } }