122 lines
3.8 KiB
C#
122 lines
3.8 KiB
C#
using System;
|
|
using System.Globalization;
|
|
|
|
namespace TBF.BenchControl.WaterMeters.iPerl
|
|
{
|
|
public class OptoTelegramRaw
|
|
{
|
|
public static readonly int Length = 42;
|
|
|
|
public OptoTelegramFlags flags;
|
|
|
|
public UInt32 EmfRaw;
|
|
public Int16 MagneticField;
|
|
public Int16 FlowRaw;
|
|
public Int32 VolumeRaw;
|
|
public Int16 Impedance;
|
|
public Int64 Timestamp;
|
|
public byte CheckSum;
|
|
|
|
public OptoTelegramRaw()
|
|
{
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
if (flags == OptoTelegramFlags.OK)
|
|
{
|
|
return string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",
|
|
EmfRaw.ToString("X6"),
|
|
MagneticField.ToString("X4"),
|
|
FlowRaw.ToString("X4"),
|
|
VolumeRaw.ToString("X6"),
|
|
Impedance.ToString("X4"),
|
|
Timestamp.ToString("X8"),
|
|
CheckSum.ToString("X2"));
|
|
}
|
|
else if (flags == OptoTelegramFlags.SyncError)
|
|
{
|
|
return "Sychronization error";
|
|
}
|
|
else if (flags == OptoTelegramFlags.InvalidTelegram)
|
|
{
|
|
return "Invalid telegram";
|
|
}
|
|
else
|
|
{
|
|
return "???";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parses optical telegram and returns OptoTelegramRaw object
|
|
/// </summary>
|
|
/// <description>
|
|
/// Create a configuration structure from a complete byte array
|
|
///
|
|
/// Telegram description:
|
|
///
|
|
/// AAAAAA[tab]BBBB[tab]CCCC[tab]DDDDDD[tab]EEEE[tab]FFFFFFFF[tab]GG[cr][lf] (42 bytes)
|
|
///
|
|
/// Data Comment Type Calculate to decimal
|
|
/// ----------------------------------------------------------------
|
|
/// AAAAAA EMF Int24 Value * 0.000000333
|
|
/// BBBB Magnetic field Int16 Value
|
|
/// CCCC Flow Int16 Value * 0.225 * Scalig factor
|
|
/// DDDDDD Volume Int24 Value / 16000 * Scaling factor
|
|
/// EEEE Impedance Int16 Value
|
|
/// FFFFFFFF Timestamp Uint32 Value / 8192
|
|
/// GG Checksum Byte
|
|
/// ----------------------------------------------------------------
|
|
///
|
|
/// Example:
|
|
/// FFFFFE 51EA 0000 65324E 0087 F6319DFF 86
|
|
/// FFDD3A 51F9 0000 65324E 0088 F631A60B 45
|
|
/// ...
|
|
/// </description>
|
|
/// <param name="data">A complete byte array data</param>
|
|
/// <returns>ConfigStruct or null when byte array was not complete</returns>
|
|
public bool UpdateFromString(string telegram)
|
|
{
|
|
if ((telegram == null) || (telegram.Length < Length) ||
|
|
(telegram[6] != '\t') || (telegram[11] != '\t') || (telegram[16] != '\t') ||
|
|
(telegram[23] != '\t') || (telegram[28] != '\t') || (telegram[37] != '\t') ||
|
|
(telegram[40] != '\r') || (telegram[41] != '\n'))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool f1 = UInt32.TryParse(telegram.Substring(0, 6), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out EmfRaw);
|
|
bool f2 = Int16.TryParse(telegram.Substring(7, 4), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out MagneticField);
|
|
bool f3 = Int16.TryParse(telegram.Substring(12, 4), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out FlowRaw);
|
|
bool f4 = Int32.TryParse(telegram.Substring(17, 6), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out VolumeRaw);
|
|
bool f5 = Int16.TryParse(telegram.Substring(24, 4), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out Impedance);
|
|
bool f6 = Int64.TryParse(telegram.Substring(29, 8), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out Timestamp);
|
|
bool f7 = byte.TryParse(telegram.Substring(38, 2), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out CheckSum);
|
|
|
|
return f1 && f2 && f3 && f4 && f5 && f6 && f7;
|
|
}
|
|
|
|
|
|
public static OptoTelegramRaw FromString(string telegram)
|
|
{
|
|
OptoTelegramRaw opto = new OptoTelegramRaw();
|
|
|
|
if (opto.UpdateFromString(telegram))
|
|
{
|
|
return opto;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
public void SetFlags(OptoTelegramFlags flags)
|
|
{
|
|
this.flags = flags;
|
|
}
|
|
}
|
|
}
|