60 lines
2.1 KiB
C#
60 lines
2.1 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
namespace TBF.Rig.ControlBoard.Uni
|
|
{
|
|
public class DivTransitionData
|
|
{
|
|
public const int MessageLen = 255;
|
|
public const byte MessageCode = (byte)'K';
|
|
|
|
/// Diverter transition data
|
|
const int samplesCount = 245;
|
|
int DiverterTime_ms;
|
|
byte[] CurveRaw;
|
|
|
|
DivTransitionData()
|
|
{
|
|
CurveRaw = new byte[samplesCount];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns tru when receive data fit the message frame
|
|
/// </summary>
|
|
/// <param name="rcvdData">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[] rcvdData, int[] cumulativeSums, int rcvdBytesCount, int offset)
|
|
{
|
|
if (offset + MessageLen > rcvdBytesCount) return false;
|
|
|
|
int checksum = cumulativeSums[offset + MessageLen - 3] - cumulativeSums[offset];
|
|
|
|
return rcvdData[offset] == Const.STX &&
|
|
rcvdData[offset + 1] == 0x01 &&
|
|
rcvdData[offset + 2] == ((MessageLen - 5) & 0xFF) &&
|
|
rcvdData[offset + 3] == MessageCode &&
|
|
rcvdData[offset + MessageLen - 2] == (checksum & 0xFF) &&
|
|
rcvdData[offset + MessageLen - 1] == ((checksum >> 8) & 0xFF);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Proces received data
|
|
/// </summary>
|
|
/// <param name="rcvdData">Received data</param>
|
|
/// <param name="offset">Offset of the message</param>
|
|
public static DivTransitionData GetData(byte[] rcvdData, int offset)
|
|
{
|
|
var data = new DivTransitionData();
|
|
data.DiverterTime_ms = Const.DivTimeResolution * rcvdData[offset + 4];
|
|
for (int i = 0; i < samplesCount; i++)
|
|
{
|
|
data.CurveRaw[i] = rcvdData[offset + 6 + i];
|
|
}
|
|
return data;
|
|
}
|
|
}
|
|
}
|