iPerlCommunication: testModeConfig parsed and set, correct wm.Disabled behavior.

This commit is contained in:
Milan Hanajik 2015-08-14 19:20:47 +02:00
parent f85fbd0b55
commit 5f5ac87f42
3 changed files with 110 additions and 42 deletions

View File

@ -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"));
}
}

View File

@ -112,28 +112,34 @@ namespace TBF.BenchControl.WaterMeters.iPerl
/// <summary>
/// Update the configuration structure from an incomplete byte array
/// </summary>
/// <param name="data">Byte array data</param>
/// <param name="offset">Offset of byte array data in ConfigStruct</param>
/// <param name="data">Byte array data</param>
/// <returns>true when successful, false when data are not appropriate</returns>
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];

View File

@ -68,23 +68,20 @@ namespace TBF.BenchControl.WaterMeters.iPerl
}
bool disabled;
/// <summary>
/// ConfigStruct of the water meter obtained or updated by iPerlCommunication
/// </summary>
/// <summary> ConfigStruct of the water meter obtained or updated by iPerlCommunication </summary>
public ConfigStruct ConfigStruct;
/// <summary>
/// CalibrationStruct of the water meter obtained or updated by iPerlCommunication
/// </summary>
/// <summary> CalibrationStruct of the water meter obtained or updated by iPerlCommunication </summary>
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
}
}