'CommErr error' codes from RFID comm. routines instead of 'bool success'.

This commit is contained in:
Milan Hanajik 2015-08-17 16:02:30 +02:00
parent 7b134f7ba5
commit e349d6811d

View File

@ -15,6 +15,19 @@ using TBF.BenchControl.WaterMeters.iPerl;
namespace TBF.BenchControl.TestMethods.iPerlCommunication
{
public enum CommErr
{
None = 0,
Disabled,
OpenPort,
Read,
Write,
CmdActive,
CmdTest,
Verify,
}
public partial class iPerlCommunicationForm : Form, GenericDevices.IHasCompleted
{
private static readonly ILog log = LogManager.GetLogger(typeof(iPerlCommunicationForm));
@ -99,8 +112,8 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
#endregion DLL_Interface
const int CommTimeout = 500;
const int MaxCommRetries = 3;
const int CommTimeout = 250;
const int MaxCommRetries = 5;
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'
@ -436,28 +449,28 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
wmFound = true;
if (wm.DebugLevel == Entities.DebugMode.Normal)
{
bool success;
CommErr error;
string resultStr = string.Empty;
if (activity.ToLower().Contains(ReadConfigurationStr.ToLower())) success = ReadConfiguration(wm, ref resultStr);
else if (activity.ToLower().Contains(SetTestModeStr.ToLower())) success = SetTestMode(wm, ref resultStr);
else if (activity.ToLower().Equals(SetActiveModeStr.ToLower())) success = SetActiveMode(wm, ref resultStr);
else if (activity.ToLower().Equals(ReadCalibrationStr.ToLower())) success = ReadCalibration(wm, ref resultStr);
else if (activity.ToLower().Equals(WriteCalibrationFactorStr.ToLower())) success = WriteCalibrationFactor(wm, ref resultStr);
else if (activity.ToLower().Equals(ResetQ2CorrectionStr.ToLower())) success = ResetQ2Correction(wm, ref resultStr);
else if (activity.ToLower().Equals(WriteQ2CorrectionStr.ToLower())) success = WriteQ2Correction(wm, ref resultStr);
if (activity.ToLower().Contains(ReadConfigurationStr.ToLower())) error = ReadConfiguration(wm, ref resultStr);
else if (activity.ToLower().Contains(SetTestModeStr.ToLower())) error = SetTestMode(wm, ref resultStr);
else if (activity.ToLower().Equals(SetActiveModeStr.ToLower())) error = SetActiveMode(wm, ref resultStr);
else if (activity.ToLower().Equals(ReadCalibrationStr.ToLower())) error = ReadCalibration(wm, ref resultStr);
else if (activity.ToLower().Equals(WriteCalibrationFactorStr.ToLower())) error = WriteCalibrationFactor(wm, ref resultStr);
else if (activity.ToLower().Equals(ResetQ2CorrectionStr.ToLower())) error = ResetQ2Correction(wm, ref resultStr);
else if (activity.ToLower().Equals(WriteQ2CorrectionStr.ToLower())) error = WriteQ2Correction(wm, ref resultStr);
else
{
success = true;
error = CommErr.None;
resultStr = "Invalid activity";
}
if (success) OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, resultStr));
else if (wm.Disabled) OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, "Watermeter is disabled"));
if (error == CommErr.None) OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, resultStr));
else if (wm.Disabled || error == CommErr.Disabled) OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, "Watermeter is disabled"));
else
{
OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, activity + " failed !!!"));
rfidDataLogger.ErrorFormat("Group={0}, Port={1}, {2} failed !!!", currentGroup, currentPort, activity);
OnCommCompleted(null, new CommCompletedEventArgs(threadId, wmNr, string.Format("{0} failed ({1}) !!!", activity, error)));
rfidDataLogger.ErrorFormat("Group={0}, Port={1}, {2} failed ({3}) !!!", currentGroup, currentPort, activity, error);
wm.Disabled = true;
}
}
@ -482,7 +495,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
/// <param name="wm">Water meter object</param>
/// <param name="resultStr">String passed to caller</param>
/// <returns>true on success</returns>
static bool ReadConfiguration(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
static CommErr ReadConfiguration(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
{
///
/// The activity is "Read configuration" (this enables the watermeter, resets error flag)
@ -493,11 +506,11 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
wm.Disabled = false;
}
if (wm.Disabled) return false;
if (wm.Disabled) return CommErr.Disabled;
if (OpenPort(wm.RfidComPortNr) != 0) return false; /// Open RFID port
if (OpenPort(wm.RfidComPortNr) != 0) return CommErr.OpenPort; /// Open RFID port
bool success = false;
CommErr error = CommErr.Read;
/// Read configuration
byte[] config = null;
@ -505,7 +518,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
{
if (0 == ReadRequestPort(MessageID.Configuration, 0, ConfigStruct.Length, out config, CommTimeout))
{
success = true;
error = CommErr.None;
wm.ConfigStruct = ConfigStruct.FromByteArray(config);
resultStr = wm.ConfigStruct.ToString(1);
break;
@ -514,7 +527,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
closePort(); /// Close RFID port
return success;
return error;
}
@ -527,9 +540,9 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
/// <param name="wm">Water meter object</param>
/// <param name="resultStr">String passed to caller</param>
/// <returns>true on success</returns>
static bool SetTestMode(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
static CommErr SetTestMode(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
{
if (wm.Disabled) return false;
if (wm.Disabled) return CommErr.Disabled;
Byte testModeConfig = 0xA0; /// Default value
///
@ -546,73 +559,76 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
if (wm.ConfigStruct.MeterState == MeterState.Test && wm.ConfigStruct.TestModeConfig == testModeConfig)
{
/// Already in the correct test mode
return true;
return CommErr.None;
}
/// Communication necessary
if (OpenPort(wm.RfidComPortNr) != 0) return false; /// Open RFID port
if (OpenPort(wm.RfidComPortNr) != 0) return CommErr.OpenPort; /// Open RFID port
bool success = true;
CommErr error = CommErr.None;
if (success && (wm.ConfigStruct.TestModeConfig != testModeConfig) && (wm.ConfigStruct.MeterState != MeterState.Active))
if (error==CommErr.None && (wm.ConfigStruct.TestModeConfig != testModeConfig) && (wm.ConfigStruct.MeterState != MeterState.Active))
{
/// Switch to Active mode in order to change TestModeConfig
success = false;
error = CommErr.CmdActive;
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 (0 == WriteRequestPort(MessageID.Command, 0, 1, cmd, CommTimeout)) { error = CommErr.None; break; }
}
}
if (success && (wm.ConfigStruct.TestModeConfig != testModeConfig))
if (error == CommErr.None && (wm.ConfigStruct.TestModeConfig != testModeConfig))
{
/// Change the TestModeConfig if necessary
success = false;
error = CommErr.Write;
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;
error = CommErr.None;
break;
}
}
}
if (success)
if (error == CommErr.None)
{
/// Switch to test mode
success = false;
error = CommErr.CmdTest;
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; }
if (0 == WriteRequestPort(MessageID.Command, 0, 1, cmd, CommTimeout)) { error = CommErr.None; break; }
}
}
/// Now the meter should be in the Test mode ... verify
if (success)
if (error == CommErr.None)
{
/// Verify the configuration
success = false;
error = CommErr.Verify;
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);
if (success) resultStr = wm.ConfigStruct.ToString(1);
break;
if (wm.ConfigStruct.MeterState == MeterState.Test)
{
error = CommErr.None;
resultStr = wm.ConfigStruct.ToString(1);
break;
}
}
}
}
closePort(); /// Close RFID port
return success;
return error;
}
@ -623,24 +639,24 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
/// <param name="wm">Water meter object</param>
/// <param name="resultStr">String passed to caller</param>
/// <returns>true on success</returns>
static bool SetActiveMode(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
static CommErr SetActiveMode(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
{
if (wm.Disabled) return false;
if (OpenPort(wm.RfidComPortNr) != 0) return false; /// Open RFID port
if (wm.Disabled) return CommErr.Disabled;
bool success = false;
if (OpenPort(wm.RfidComPortNr) != 0) return CommErr.OpenPort; /// Open RFID port
CommErr error = CommErr.CmdActive;
/// Switch to active mode
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 (0 == WriteRequestPort(MessageID.Command, 0, 1, cmd, CommTimeout)) { error = CommErr.None; break; }
}
if (success)
if (error == CommErr.None)
{
success = false;
error = CommErr.Verify;
/// Read configuration
byte[] cfg_0_3 = null;
@ -649,16 +665,19 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
if (0 == ReadRequestPort(MessageID.Configuration, 0, 4, out cfg_0_3, CommTimeout))
{
wm.ConfigStruct.Update(0, cfg_0_3);
success = (wm.ConfigStruct.MeterState == MeterState.Active);
if (success) resultStr = wm.ConfigStruct.ToString(1);
break;
if (wm.ConfigStruct.MeterState == MeterState.Active)
{
error = CommErr.None;
resultStr = wm.ConfigStruct.ToString(1);
break;
}
}
}
}
closePort(); /// Close RFID port
return success;
return error;
}
@ -668,13 +687,13 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
/// <param name="wm">Water meter object</param>
/// <param name="resultStr">String passed to caller</param>
/// <returns>true on success</returns>
static bool ReadCalibration(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
static CommErr ReadCalibration(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
{
if (wm.Disabled) return false;
if (wm.Disabled) return CommErr.Disabled;
if (OpenPort(wm.RfidComPortNr) != 0) return false; /// Open RFID port
if (OpenPort(wm.RfidComPortNr) != 0) return CommErr.OpenPort; /// Open RFID port
bool success = false;
CommErr error = CommErr.Read;
/// Read calibration
byte[] calib = null;
@ -682,7 +701,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
{
if (0 == ReadRequestPort(MessageID.Calibration, 0, CalibrationStruct.Length, out calib, CommTimeout))
{
success = true;
error = CommErr.None;
wm.CalibrationStruct = CalibrationStruct.FromByteArray(calib);
resultStr = wm.CalibrationStruct.ToString();
break;
@ -691,7 +710,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
closePort(); /// Close RFID port
return success;
return error;
}
@ -702,13 +721,13 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
/// <param name="wm">Water meter object</param>
/// <param name="resultStr">String passed to caller</param>
/// <returns>true on success</returns>
static bool WriteCalibrationFactor(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
static CommErr WriteCalibrationFactor(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
{
if (wm.Disabled) return false;
if (wm.Disabled) return CommErr.Disabled;
if (OpenPort(wm.RfidComPortNr) != 0) return false; /// Open RFID port
if (OpenPort(wm.RfidComPortNr) != 0) return CommErr.OpenPort; /// Open RFID port
bool success = false;
CommErr error = CommErr.Write;
UInt16 factor = wm.CalibrationFactor; /// Original calibration factor
@ -717,12 +736,12 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
byte[] data = new byte[2] { (byte)(newCalFactor & 0x00FF), (byte)((newCalFactor >> 8) & 0x00FF) };
for (int j = 0; j < MaxCommRetries; j++)
{
if (0 == WriteRequestPort(MessageID.Calibration, 2, 2, data, CommTimeout)) { success = true; break; }
if (0 == WriteRequestPort(MessageID.Calibration, 2, 2, data, CommTimeout)) { error = CommErr.None; break; }
}
if (success)
if (error == CommErr.None)
{
success = false;
error = CommErr.Verify;
/// Read calibration
byte[] calib_2_3 = null;
@ -730,7 +749,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
{
if (0 == ReadRequestPort(MessageID.Calibration, 2, 2, out calib_2_3, CommTimeout))
{
success = true;
error = CommErr.None;
wm.CalibrationStruct.Update(calib_2_3, 2);
resultStr = wm.CalibrationStruct.ToString();
break;
@ -740,7 +759,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
closePort(); /// Close RFID port
return success;
return error;
}
@ -754,13 +773,13 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
/// <param name="wm">Water meter object</param>
/// <param name="resultStr">String passed to caller</param>
/// <returns>true on success</returns>
static bool ResetQ2Correction(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
static CommErr ResetQ2Correction(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
{
if (wm.Disabled) return false;
if (wm.Disabled) return CommErr.Disabled;
if (OpenPort(wm.RfidComPortNr) != 0) return false; /// Open RFID port
if (OpenPort(wm.RfidComPortNr) != 0) return CommErr.OpenPort; /// Open RFID port
bool success = false;
CommErr error = CommErr.Write;
/// Write zero Q2 correction
Byte q2CorrRFlow = 0;
@ -769,12 +788,12 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
for (int j = 0; j < MaxCommRetries; j++)
{
if (0 == WriteRequestPort(MessageID.MetrologyMemory, Q2CorrFactorsAddr, 2, wrData, CommTimeout)) { success = true; break; }
if (0 == WriteRequestPort(MessageID.MetrologyMemory, Q2CorrFactorsAddr, 2, wrData, CommTimeout)) { error = CommErr.None; break; }
}
if (success)
if (error == CommErr.None)
{
success = false;
error = CommErr.Verify;
/// Read calibration
byte[] rdData = null;
@ -783,7 +802,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
if ((0 == ReadRequestPort(MessageID.MetrologyMemory, Q2CorrFactorsAddr, 2, out rdData, CommTimeout)) &&
(rdData != null) && (rdData.Length == 2) && (rdData[0] == 0) && (rdData[1] == 0))
{
success = true;
error = CommErr.None;
resultStr = "Q2 correction factors reset to 0";
break;
}
@ -792,7 +811,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
closePort(); /// Close RFID port
return success;
return error;
}
@ -803,13 +822,13 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
/// <param name="wm">Water meter object</param>
/// <param name="resultStr">String passed to caller</param>
/// <returns>true on success</returns>
static bool WriteQ2Correction(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
static CommErr WriteQ2Correction(WaterMeters.iPerl.WaterMeter wm, ref string resultStr)
{
if (wm.Disabled) return false;
if (wm.Disabled) return CommErr.Disabled;
if (OpenPort(wm.RfidComPortNr) != 0) return false; /// Open RFID port
if (OpenPort(wm.RfidComPortNr) != 0) return CommErr.OpenPort; /// Open RFID port
bool success = false;
CommErr error = CommErr.Write;
/// Write Q2 corrections
Byte q2CorrRFlow = 0;
@ -818,12 +837,12 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
for (int j = 0; j < MaxCommRetries; j++)
{
if (0 == WriteRequestPort(MessageID.MetrologyMemory, Q2CorrFactorsAddr, 2, wrData, CommTimeout)) { success = true; break; }
if (0 == WriteRequestPort(MessageID.MetrologyMemory, Q2CorrFactorsAddr, 2, wrData, CommTimeout)) { error = CommErr.None; break; }
}
if (success)
if (error == CommErr.None)
{
success = false;
error = CommErr.Verify;
/// Read calibration
byte[] rdData = null;
@ -832,7 +851,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
if ((0 == ReadRequestPort(MessageID.MetrologyMemory, Q2CorrFactorsAddr, 2, out rdData, CommTimeout)) &&
(rdData != null) && (rdData.Length == 2) && (rdData[0] == q2CorrRFlow) && (rdData[1] == q2CorrRFlow))
{
success = true;
error = CommErr.None;
resultStr = string.Format("Q2 correction factors set: RightFlow = {0}, LeftFlow = {1}",
q2CorrRFlow, q2CorrRFlow);
break;
@ -840,7 +859,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
}
}
return success;
return error;
}