Correct reading of WM pulses (windowing effect, only lower 16 bits of a counter value are available).
This commit is contained in:
@@ -13,6 +13,8 @@ namespace TBF.Rig.ControlBoard.Uni
|
|||||||
public const int MessageLen = 303;
|
public const int MessageLen = 303;
|
||||||
public const byte MessageCode = (byte)' ';
|
public const byte MessageCode = (byte)' ';
|
||||||
|
|
||||||
|
public bool RestartReadingOfWMPulses = true;
|
||||||
|
|
||||||
/// General data
|
/// General data
|
||||||
public ulong State; /// bytes 5,6,7,8,9,80,81 = 7 bytes = 56 bits
|
public ulong State; /// bytes 5,6,7,8,9,80,81 = 7 bytes = 56 bits
|
||||||
public uint Chyby485;
|
public uint Chyby485;
|
||||||
@@ -34,6 +36,9 @@ namespace TBF.Rig.ControlBoard.Uni
|
|||||||
public Int16[] AnalogInput = new Int16[Const.ADCsCount];
|
public Int16[] AnalogInput = new Int16[Const.ADCsCount];
|
||||||
public int[] TemperatureRaw = new int[8];
|
public int[] TemperatureRaw = new int[8];
|
||||||
|
|
||||||
|
/// Private
|
||||||
|
int[] lastPulses = new int[Const.WMsCount + 1]; /// WMsCount = 20
|
||||||
|
|
||||||
|
|
||||||
public GeneralData()
|
public GeneralData()
|
||||||
{
|
{
|
||||||
@@ -121,7 +126,7 @@ namespace TBF.Rig.ControlBoard.Uni
|
|||||||
RegVStatus[Const.RVsCount + 1] = (stavRegQ >> 4) & 2;
|
RegVStatus[Const.RVsCount + 1] = (stavRegQ >> 4) & 2;
|
||||||
|
|
||||||
/// This must preceed processing bytes 91 .. 250 as Ttime is used there
|
/// This must preceed processing bytes 91 .. 250 as Ttime is used there
|
||||||
uint ttimeRaw = GetUInt32(data, offs + (int)Poz.Ttime); /// Byte 251,252,253,254
|
uint ttimeRaw = GetUInt32(data, offs + (int)Poz.Ttime); /// Byte 251,252,253,254
|
||||||
Ttime = Convert.ToDouble(ttimeRaw) * Const.TimeResolution; /// convert to [s]
|
Ttime = Convert.ToDouble(ttimeRaw) * Const.TimeResolution; /// convert to [s]
|
||||||
ImpulseTime[0] = Ttime;
|
ImpulseTime[0] = Ttime;
|
||||||
|
|
||||||
@@ -133,7 +138,7 @@ namespace TBF.Rig.ControlBoard.Uni
|
|||||||
if ((State & (ulong)StatusP.SynchroMethod) != 0)
|
if ((State & (ulong)StatusP.SynchroMethod) != 0)
|
||||||
{
|
{
|
||||||
ushort pulses = GetUInt16(data, ix2);
|
ushort pulses = GetUInt16(data, ix2);
|
||||||
WMeterPuls[wmNr] = Convert.ToInt32(pulses);
|
WMeterPuls[wmNr] = GetCorrectedPulses(wmNr, pulses, RestartReadingOfWMPulses);
|
||||||
EtPulses[wmNr] = (pulses == 0) ? 0 : Convert.ToInt32(GetUInt24(data, ix1));
|
EtPulses[wmNr] = (pulses == 0) ? 0 : Convert.ToInt32(GetUInt24(data, ix1));
|
||||||
ImpulseTime[wmNr] = (pulses == 0) ? 0 : Const.TimeResolution * Convert.ToDouble(GetUInt24(data, ix1 + 3));
|
ImpulseTime[wmNr] = (pulses == 0) ? 0 : Const.TimeResolution * Convert.ToDouble(GetUInt24(data, ix1 + 3));
|
||||||
}
|
}
|
||||||
@@ -144,6 +149,7 @@ namespace TBF.Rig.ControlBoard.Uni
|
|||||||
ImpulseTime[wmNr] = Ttime;
|
ImpulseTime[wmNr] = Ttime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
RestartReadingOfWMPulses = false;
|
||||||
|
|
||||||
StavDA[0] = GetUInt16(data, offs + (int)Poz.SDA); /// Byte 255,256
|
StavDA[0] = GetUInt16(data, offs + (int)Poz.SDA); /// Byte 255,256
|
||||||
byte spare3 = data[offs + (int)Poz.spare3]; /// Byte 257
|
byte spare3 = data[offs + (int)Poz.spare3]; /// Byte 257
|
||||||
@@ -199,6 +205,44 @@ namespace TBF.Rig.ControlBoard.Uni
|
|||||||
log.InfoFormat("Et={0} State={1:X14} Route={2} Freq={3} EtPulses={4} TTime={5} WMPulses[3]={6} WMRefPulses[3]={7}, Time[3]={8}",
|
log.InfoFormat("Et={0} State={1:X14} Route={2} Freq={3} EtPulses={4} TTime={5} WMPulses[3]={6} WMRefPulses[3]={7}, Time[3]={8}",
|
||||||
(State & 7), State, Utils.ToBin40(((UInt64)Vystupy) >> 8), ReferenceFreq[0], EtPulses[0], Ttime, WMeterPuls[3], EtPulses[3], ImpulseTime[3]);
|
(State & 7), State, Utils.ToBin40(((UInt64)Vystupy) >> 8), ReferenceFreq[0], EtPulses[0], Ttime, WMeterPuls[3], EtPulses[3], ImpulseTime[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get number of pulses corrected for windowing effect
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="wmNr1">1-based water meter number</param>
|
||||||
|
/// <param name="lowerWordOfMeterPulses"></param>
|
||||||
|
/// <param name="firstReading">When true lastPulses[wmNr1] is disregarded and is overwritten with lowerWordOfMeterPulses value</param>
|
||||||
|
/// <returns>Corrected water meter pulses</returns>
|
||||||
|
private int GetCorrectedPulses(int wmNr1, UInt16 lowerWordOfMeterPulses, bool firstReading)
|
||||||
|
{
|
||||||
|
if (firstReading)
|
||||||
|
{
|
||||||
|
lastPulses[wmNr1] = (Int16)lowerWordOfMeterPulses;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int uncorrected = (int)(((uint)lastPulses[wmNr1] & 0xFFFF0000) | lowerWordOfMeterPulses);
|
||||||
|
|
||||||
|
if (Math.Abs(uncorrected - lastPulses[wmNr1]) <= Int16.MaxValue)
|
||||||
|
{
|
||||||
|
lastPulses[wmNr1] = uncorrected;
|
||||||
|
}
|
||||||
|
else if (Math.Abs(uncorrected + 65536 - lastPulses[wmNr1]) <= Int16.MaxValue)
|
||||||
|
{
|
||||||
|
lastPulses[wmNr1] = uncorrected + 65536;
|
||||||
|
}
|
||||||
|
else if (Math.Abs(uncorrected - 65536 - lastPulses[wmNr1]) <= Int16.MaxValue)
|
||||||
|
{
|
||||||
|
lastPulses[wmNr1] = uncorrected - 65536;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lastPulses[wmNr1] = uncorrected; /// Default
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastPulses[wmNr1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -460,6 +460,11 @@ namespace TBF.Rig.ControlBoard.Uni
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((combinedAction.ActionId & ActionID.StartTest) != 0)
|
||||||
|
{
|
||||||
|
Data.RestartReadingOfWMPulses = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (cbCfg.DebugLevel == DebugMode.Normal)
|
if (cbCfg.DebugLevel == DebugMode.Normal)
|
||||||
{
|
{
|
||||||
serialPort.Write(outData, 0, outData.Length);
|
serialPort.Write(outData, 0, outData.Length);
|
||||||
|
|||||||
Reference in New Issue
Block a user