160 lines
5.7 KiB
C#
160 lines
5.7 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using TBF.Rig.RegisterReaders.iPerlASICReader.communication.C4.diagnosticLed.parserer;
|
|
|
|
namespace TBF.Rig.RegisterReaders.iPerlASICReader.implementations
|
|
{
|
|
public enum OptoTelegramFlags : byte
|
|
{
|
|
OK = 0,
|
|
OK_TestStart,
|
|
OK_TestEnd,
|
|
InvalidTelegram, /// Wrong telegram format of checksum error
|
|
SyncError,
|
|
}
|
|
|
|
public class OptoTelegramRaw
|
|
{
|
|
private DiagnosticLedState4Data data;
|
|
private static CultureInfo culture;
|
|
///
|
|
/// Strobed value
|
|
///
|
|
public static decimal TestStartTimestampDec;
|
|
|
|
///
|
|
/// Stored values
|
|
///
|
|
public OptoTelegramFlags Flags;
|
|
|
|
public DateTime DateTime; /// From PC
|
|
public float RefFlow; /// [m3/h]
|
|
public int Counter;
|
|
|
|
public Int16 FlowRaw;
|
|
public UInt32 VolumeRaw;
|
|
public Int64 VolumeRawExt;
|
|
public UInt32 Timestamp;
|
|
public Int64 TimestampExt;
|
|
|
|
|
|
///
|
|
/// Calculated values
|
|
///
|
|
public double Flow(double scalingFactor) { return 0.225 * scalingFactor * (double)FlowRaw; }
|
|
public double Volume(double scalingFactor) { return 0.0000625 * scalingFactor * (double)VolumeRawExt; }
|
|
public decimal TimestampDec() { return (decimal)TimestampExt / (decimal)8192; }
|
|
public double VolumeDelta(double scalingFactor,OptoTelegramRaw previous) { return (previous == null) ? 0 : Volume(scalingFactor) - previous.Volume(scalingFactor); }
|
|
public decimal TimeDelta() { return TimestampDec() - TestStartTimestampDec; }
|
|
public string Label()
|
|
{
|
|
if (Flags == OptoTelegramFlags.OK_TestStart) return "#### start test ####";
|
|
else if (Flags == OptoTelegramFlags.OK_TestEnd) return "#### end of test ####";
|
|
else return string.Empty;
|
|
}
|
|
|
|
|
|
static OptoTelegramRaw()
|
|
{
|
|
culture = CultureInfo.CreateSpecificCulture("DE"); /// This is to use comma as decimal number separator
|
|
}
|
|
|
|
public OptoTelegramRaw()
|
|
{
|
|
}
|
|
|
|
public void UpdateFromSmart(DiagnosticLedState4Data data,int counter, float refFlow, ref Int64 volumeRawExtLast, ref Int64 timestampExtLast)
|
|
{
|
|
|
|
DateTime = DateTime.Now;
|
|
Counter = counter;
|
|
RefFlow = refFlow;
|
|
|
|
FlowRaw = data.RawFlow;
|
|
VolumeRaw = data.RawVolume;
|
|
Timestamp = data.AsicTimestamp;
|
|
|
|
|
|
///
|
|
/// Cope with 'VolumeRaw' overflow
|
|
///
|
|
Int64 uncorrected = (Int64)(((UInt64)volumeRawExtLast & 0xFFFFFFFFFF000000UL) | VolumeRaw);
|
|
if (Math.Abs(uncorrected - volumeRawExtLast) <= 0x800000L)
|
|
{
|
|
VolumeRawExt = volumeRawExtLast = uncorrected;
|
|
}
|
|
else if (Math.Abs(uncorrected + 0x1000000L - volumeRawExtLast) <= 0x800000L)
|
|
{
|
|
VolumeRawExt = volumeRawExtLast = uncorrected + 0x1000000L;
|
|
}
|
|
else if (Math.Abs(uncorrected - 0x1000000L - volumeRawExtLast) <= 0x800000L)
|
|
{
|
|
VolumeRawExt = volumeRawExtLast = uncorrected - 0x1000000L;
|
|
}
|
|
else
|
|
{
|
|
VolumeRawExt = volumeRawExtLast = uncorrected;
|
|
}
|
|
|
|
///
|
|
/// Cope with 'Timestamp' overflow
|
|
///
|
|
uncorrected = (Int64)(((UInt64)timestampExtLast & 0xFFFFFFFF00000000UL) | Timestamp);
|
|
if (Math.Abs(uncorrected - timestampExtLast) <= 0x80000000L)
|
|
{
|
|
TimestampExt = timestampExtLast = uncorrected;
|
|
}
|
|
else if (Math.Abs(uncorrected + 0x100000000L - timestampExtLast) <= 0x80000000L)
|
|
{
|
|
TimestampExt = timestampExtLast = uncorrected + 0x100000000L;
|
|
}
|
|
else if (Math.Abs(uncorrected - 0x100000000L - timestampExtLast) <= 0x80000000L)
|
|
{
|
|
TimestampExt = timestampExtLast = uncorrected - 0x100000000L;
|
|
}
|
|
else
|
|
{
|
|
TimestampExt = timestampExtLast = uncorrected;
|
|
}
|
|
|
|
}
|
|
|
|
public void SetFlags(OptoTelegramFlags flags)
|
|
{
|
|
this.Flags = flags;
|
|
}
|
|
|
|
|
|
public string ToString(double scalingFactor, OptoTelegramRaw previous)
|
|
{
|
|
if (Flags == OptoTelegramFlags.SyncError)
|
|
{
|
|
return "Sychronization error";
|
|
}
|
|
else if (Flags == OptoTelegramFlags.InvalidTelegram)
|
|
{
|
|
return "Invalid telegram";
|
|
}
|
|
else /// if (flags == OptoTelegramFlags.OK / OptoTelegramFlags.OK_TestStart / OptoTelegramFlags.OK_TestEnd)
|
|
{
|
|
return string.Format("{0}:{1}:{2}.{3}\t{4} :\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}\t{13}\t{14}\t{15}",
|
|
DateTime.Hour.ToString("D2"),
|
|
DateTime.Minute.ToString("D2"),
|
|
DateTime.Second.ToString("D2"),
|
|
DateTime.Millisecond.ToString("D4"),
|
|
Counter,
|
|
FlowRaw.ToString("X4"),
|
|
VolumeRaw.ToString("X6"),
|
|
Timestamp.ToString("X8"),
|
|
Flow(scalingFactor).ToString("F2", culture),
|
|
Volume(scalingFactor).ToString("F4", culture),
|
|
TimestampDec().ToString("F4", culture),
|
|
(RefFlow * 1000).ToString("F2", culture),
|
|
VolumeDelta(scalingFactor, previous).ToString("F4", culture),
|
|
TimeDelta().ToString("F3", culture),
|
|
scalingFactor.ToString("F1", culture),
|
|
Label());
|
|
}
|
|
}
|
|
}
|
|
} |