From 3a28e334376ffd4a38ebed6348461af26dd5e0aa Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Fri, 2 Jul 2021 15:50:34 +0200 Subject: [PATCH] OptoTelegramRaw improvements --- .../iPerlHead/OptoTelegramRaw.cs | 120 ++++++++++-------- 1 file changed, 66 insertions(+), 54 deletions(-) diff --git a/TBF/Rig/TestMethods/iPerlCommunication/iPerlHead/OptoTelegramRaw.cs b/TBF/Rig/TestMethods/iPerlCommunication/iPerlHead/OptoTelegramRaw.cs index de848389c..da5b0a1bb 100644 --- a/TBF/Rig/TestMethods/iPerlCommunication/iPerlHead/OptoTelegramRaw.cs +++ b/TBF/Rig/TestMethods/iPerlCommunication/iPerlHead/OptoTelegramRaw.cs @@ -1,5 +1,5 @@ /// -/// Copyright (c) 2015-2017 Sensus Metering Systems +/// Copyright (c) 2015-2021 Sensus Metering Systems /// using System; using System.Globalization; @@ -119,51 +119,61 @@ namespace TBF.Rig.TestMethods.iPerlCommunication.iPerlHead bool f6 = UInt32.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); - bool allOk = f1 && f2 && f3 && f4 && f5 && f6 && f7; + byte calculatedCheckSum = 0; + for (int i = 0; i < Length - 4; i++) + { + calculatedCheckSum += (byte)telegram[i]; + } + + bool allOk = f1 && f2 && f3 && f4 && f5 && f6 && f7 && (calculatedCheckSum == CheckSum); + + if (allOk) + { + /// + /// 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; + } + } + Flags = allOk ? OptoTelegramFlags.OK : OptoTelegramFlags.InvalidTelegram; - /// - /// 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; - } - return allOk; } @@ -184,16 +194,19 @@ namespace TBF.Rig.TestMethods.iPerlCommunication.iPerlHead 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 MagneticFieldRaw); - //bool f3 = Int16.TryParse(telegram.Substring(12, 4), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out FlowRaw); - //bool f4 = UInt32.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 = UInt32.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; + bool f7 = byte.TryParse(telegram.Substring(38, 2), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out CheckSum); - return true; + byte calculatedCheckSum = 0; + for (int i = 0; i < Length - 4; i++) + { + calculatedCheckSum += (byte)telegram[i]; + } + + bool allOk = f7 && (calculatedCheckSum == CheckSum); + + Flags = allOk ? OptoTelegramFlags.OK : OptoTelegramFlags.InvalidTelegram; + + return allOk; } @@ -315,6 +328,5 @@ namespace TBF.Rig.TestMethods.iPerlCommunication.iPerlHead return IperlHead.StartOptoDataCount + (index - IperlHead.MaxOptoDataCount) % IperlHead.EndOptoDataCount; } } - } }