Files
tbf/TBF/BenchControl/ControlBoard/Uni/GeneralData.cs
T

245 lines
12 KiB
C#

///
/// Copyright (c) 2021 Sensus Slovensko a.s.
///
using System;
namespace TBF.BenchControl.ControlBoard.Uni
{
public class GeneralData
{
public const int MessageLen = 303;
public const byte MessageCode = (byte)' ';
/// General data
public byte UnusedCode; /// ???, byte 4
public ulong Status; /// byte 5..9
public uint Status4; /// byte 9
public uint Status5;
public uint Status6; /// Parallel flow meters mask: bit0=0x01=Et1, bit1=0x02=Et2, bit2=0x04=Et3
public uint Chyby485;
public bool PosliKohut;
public int TolerRV;
public float[] ZCasPohybu = new float[2];
public ulong Kohuty = 0;
public ulong KohutyH = 0;
public ulong Vystupy;
public uint DajFilt;
public uint[] KFilter = new uint[8];
public uint[] Parametre = new uint[5];
public uint NastavRV;
public uint StabRV;
public uint[] StavDA = new uint[2];
public float ZStavDA2;
public float[] referenceFreq = new float[4];
public uint[] etPulses = new uint[Const.WMsCount + 1]; /// WMsCount = 20
public uint etPulsesK;
public uint EtPulses2;
public uint EtPulses3;
public uint[] wMeterPuls = new uint[Const.WMsCount + 1]; /// WMsCount = 20
public double[] ImpulseTime = new double[Const.WMsCount + 1]; /// WMsCount = 20
public uint[] wMeterReference = new uint[Const.WMsCount + 1]; /// WMsCount = 20
public uint[] rValveStatus = new uint[Const.RVsCount + 1 + 1]; /// RVsCount = 8, posledny rValveStatus[9] je prudovy
public float[] ValveMoveTime = new float[Const.RVsCount];
public ulong rRoute;
public ulong rRouteH;
public int[] divTime = new int[3];
public double Ttime; /// s
public double TimeRef; /// s
public uint[] beginSt = new uint[Const.WMsCount + 1]; /// WMsCount = 20
public float referenceFlow;
public float[] RValvePosition = new float[Const.RVsCount + 1 + 1]; /// RVsCount = 8
public ulong Vstupy;
public int[] AnalogyRaw = new int[Const.ADCsCount];
public int[] TemperatureRaw = new int[8];
public float[] ReferenceVolume = new float[2];
public GeneralData()
{
}
/// <summary>
/// Returns tru when receive data fit the message frame
/// </summary>
/// <param name="data">Received data</param>
/// <param name="cumulativeSums">Cumulative sums of received data bytes</param>
/// <param name="rcvdBytesCount">Received bytes count</param>
/// <param name="offset">Offset of the message</param>
/// <returns>true when frame fits data</returns>
public static bool FrameFitsData(byte[] data, int[] cumulativeSums, int rcvdBytesCount, int offset)
{
if (offset + MessageLen > rcvdBytesCount) return false;
int checksum = cumulativeSums[offset + MessageLen - 3] - cumulativeSums[offset];
return data[offset] == Const.STX &&
data[offset + 1] == 0x01 &&
data[offset + 2] == ((MessageLen - 5) & 0xFF) &&
data[offset + 3] == MessageCode &&
data[offset + MessageLen - 2] == (checksum & 0xFF) &&
data[offset + MessageLen - 1] == ((checksum >> 8) & 0xFF);
}
/// <summary>
/// Auxiliary functions
/// </summary>
/// <param name="data">Received data</param>
/// <param name="offs">Offset to the files</param>
/// <returns></returns>
short GetInt16(byte[] data, int offs) { return BitConverter.ToInt16(data, offs); }
ushort GetUInt16(byte[] data, int offs) { return BitConverter.ToUInt16(data, offs); }
uint GetUInt24(byte[] data, int offs) { return BitConverter.ToUInt32(data, offs) & 0x00FFFFFF; }
uint GetUInt32(byte[] data, int offs) { return BitConverter.ToUInt32(data, offs); }
ulong GetUInt40(byte[] data, int offs) { return BitConverter.ToUInt64(data, offs) & 0x000000FFFFFFFFFF; }
ulong GetUInt64(byte[] data, int offs) { return BitConverter.ToUInt64(data, offs); }
/// <summary>
/// Proces received data
/// </summary>
/// <param name="data">Received data</param>
/// <param name="offs">Offset of the message</param>
public void UpdateData(byte[] data, int offs)
{
UnusedCode = data[offs + (int)Poz.Start]; /// Byte 4
Status = GetUInt40(data, offs + (int)Poz.Status); /// Byte 5,6,7,8,9
Status4 = data[offs + (int)Poz.Status + 4]; /// Byte 9 (duplicated)
Vystupy = GetUInt64(data, offs + (int)Poz.Vyst); /// Byte 10 .. 17
StavDA[1] = GetUInt16(data, offs + (int)Poz.SDA2); /// Byte 18,19
Vstupy = GetUInt40(data, offs + (int)Poz.Vst); /// Byte 20,21,22,23,24
/// A/D converters: Feedback from RV-s and diverters
for (int i = 0; i < Const.ADCsCount; i++) /// Byte 25 .. 56
{
AnalogyRaw[i] = GetInt16(data, offs + (int)Poz.Analogy + 2 * i);
}
/// Groch temperature meters
for (int i = 0; i < 8; i++) /// byte 57 .. 72
{
TemperatureRaw[i] = GetInt16(data, offs + (int)Poz.Groch + 2 * i);
}
etPulses[0] = GetUInt24(data, offs + (int)Poz.Et1); /// Byte 73,74,75
etPulsesK = GetUInt24(data, offs + (int)Poz.EtK); /// Byte 76,77,78
/// TODO /// byte 79
Status5 = data[offs + (int)Poz.Stat5]; /// Byte 80
Status6 = data[offs + (int)Poz.Stat6]; /// Byte 81
/// TODO /// byte 82
for (int i = 0; i < Const.RVsCount; i++) /// Byte 83 .. 90
{
rValveStatus[i + 1] = ((uint)data[offs + (int)Poz.RVs + i] >> 3) & 7;
}
/// 4-20 mA reg. valve
uint stavRegQ = (uint)((Status >> 16) & 0xFF);
rValveStatus[Const.RVsCount + 1] = (stavRegQ >> 4) & 2;
/// 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
Ttime = Convert.ToDouble(ttimeRaw) * Const.TimeResolution; /// convert to [s]
ImpulseTime[0] = Ttime;
/// Water meters (pulses count, gated test time, gated etalon pulses count)
int ix1 = offs + (int)Poz.CT1; /// Byte 91 .. 250
int ix2 = offs + (int)Poz.CTR1;
for (int wmNr = 1; wmNr <= Const.WMsCount; wmNr++, ix1 += 6, ix2 += 2)
{
if ((Status & (ulong)StatusP.SynchroMethod) != 0)
{
ushort pulses = GetUInt16(data, ix2);
wMeterPuls[wmNr] = pulses;
etPulses[wmNr] = (pulses == 0) ? 0 : GetUInt24(data, ix1);
ImpulseTime[wmNr] = (pulses == 0) ? 0 : GetUInt24(data, ix1 + 3) * Const.TimeResolution;
}
else
{
wMeterPuls[wmNr] = GetUInt24(data, ix1);
etPulses[wmNr] = etPulses[0];
ImpulseTime[wmNr] = Ttime;
}
}
StavDA[0] = GetUInt16(data, offs + (int)Poz.SDA); /// Byte 255,256
byte spare1 = data[offs + (int)Poz.spare1]; /// Byte 257
/// Impulzy perioda (???)
uint[] par1 = new uint[3];
uint[] par2 = new uint[3];
par2[0] = GetUInt16(data, offs + (int)Poz.Per1); /// Byte 258,259
par1[0] = GetUInt16(data, offs + (int)Poz.Per1 + 2); /// Byte 260,261
par2[1] = GetUInt16(data, offs + (int)Poz.Per2); /// Byte 262.263
par1[1] = GetUInt16(data, offs + (int)Poz.Per2 + 2); /// Byte 264,265
par2[2] = GetUInt16(data, offs + (int)Poz.Per3); /// Byte 266,267
par1[2] = GetUInt16(data, offs + (int)Poz.Per3 + 2); /// Byte 268,269
///
referenceFreq[0] = (par2[0] != 0) ? (5.0f * par1[0] / par2[0]) : 0.0f;
referenceFreq[1] = referenceFreq[2] = referenceFreq[3] = 0;
///
if ((Status & (ulong)StatusP.TestWithRefFlowmtr) != 0 && (Status & (ulong)StatusP.FlowmtrIdMask) == 0)
{
/// Measurement with parallel flowmeters
if ((Status6 & 0x01) != 0)
referenceFreq[1] = referenceFreq[0];
if ((Status6 & 0x02) != 0)
referenceFreq[2] = (par2[1] != 0) ? (5.0f * par1[1] / par2[1]) : 0.0f;
if ((Status6 & 0x04) != 0)
referenceFreq[3] = (par2[2] != 0) ? (5.0f * par1[2] / par2[2]) : 0.0f;
referenceFreq[0] = referenceFreq[1] + referenceFreq[2] + referenceFreq[3];
}
divTime[0] = Const.DivTimeResolution * data[offs + (int)Poz.CasKl]; /// Byte 271
byte spare2 = data[offs + (int)Poz.spare2]; /// Byte 272
Chyby485 = GetUInt16(data, offs + (int)Poz.Chyb485); /// Byte 273, 274 !!!!!!!!!!!!!!!
uint timeRefRaw = GetUInt24(data, offs + (int)Poz.Tim2); /// Byte 274,275,276 !!!!!!!!!!!!!!!
TimeRef = Convert.ToDouble(timeRefRaw) / 1000.0; /// convert to [s]
for (int i = 0; i < Const.RVsCount; i++) /// Byte 278 .. 293
{
ValveMoveTime[i] = 10 * GetUInt16(data, offs + (int)Poz.CasKoh + 2*i);
}
EtPulses2 = GetUInt24(data, offs + (int)Poz.ETX2); /// Byte 294,295,296
EtPulses3 = GetUInt24(data, offs + (int)Poz.ETX3); /// Byte 297,298.299 !!!!!!!!!!!!!!!
}
}
/// <summary>
/// Byte positions in rcvdData buffer
/// </summary>
public enum Poz : int
{
Start = 4,
Status = Start + 1, /// 5 .. 9
Vyst = Status + 5, /// 10 .. 17
SDA2 = Vyst + 8, /// 18,19
Vst = SDA2 + 2, /// 20 .. 24
Analogy = Vst + 5, /// 25 .. 56
Groch = Analogy + 2 * Const.ADCsCount, /// 57 (16 analog inputs)
Et1 = Groch + 16, /// 73,74,75
EtK = Et1 + 3, /// 76,77,78
Stat5 = EtK + 4, /// 80
Stat6 = Stat5 + 1, /// 81
RVs = Stat6 + 2, /// 83
CT1 = RVs + 8, /// 91
CTR1 = CT1 + 6 * Const.WMsCount, /// 211
Ttime = CTR1 + 2 * Const.WMsCount, /// 251
SDA = Ttime + 4, /// 255
spare1 = SDA + 2, /// 257
Per1 = spare1 + 1, /// 258
Per2 = Per1 + 4, /// 262
Per3 = Per2 + 4, /// 266
CasKl = Per3 + 5, /// 271
spare2 = CasKl + 1, /// 272
Chyb485 = spare2 + 1, /// 273
Tim2 = Chyb485 + 1, /// 274
CasKoh = Tim2 + 4, /// 278
ETX2 = CasKoh + 2 * Const.RVsCount, /// 294
ETX3 = ETX2 + 3, /// 297
}
}