using System; using System.Linq; using System.Globalization; namespace GenesisZeroFlow { public static partial class DecodeTelegram { public static DecodedData[] DecodedDataDecodeTelegram(String rxData, DecodedData[] decodedData) { #region Definitions and initializations DecodedData[] DecodingResult = DecodedDataHelper.GetDefaultDecodedDataArray(Constants.NumberOfPaths); #region CA1062 if ((decodedData == null)||(rxData == null)) return DecodingResult; #endregion DecodingResult[Constants.PATH0] = ((DecodedData)(decodedData[Constants.PATH0].Clone())); DecodingResult[Constants.PATH1] = ((DecodedData)(decodedData[Constants.PATH1].Clone())); DecodingResult[Constants.PATH2] = ((DecodedData)(decodedData[Constants.PATH2].Clone())); #endregion #region Prepare RX data String[] HexValues = rxData.Split(' '); #endregion #region Call decoder #region Decode protocol H DecodingResult = DecodeTelegram.DecodeProtocolH(HexValues, DecodingResult); #endregion #endregion #region Return data return DecodingResult; #endregion } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.UInt16.ToString(System.String)")] public static DecodedData[] DecodeProtocolH(String[] hexValues, DecodedData[] inputDecodedData) { #region Definitions and initializations const UInt16 TelegramLength = 15; DecodedData[] ReturnData = DecodedDataHelper.GetDefaultDecodedDataArray(Constants.NumberOfPaths); if ((inputDecodedData == null) || (hexValues == null)) return ReturnData; ReturnData[Constants.PATH0] = ((DecodedData)(inputDecodedData[Constants.PATH0].Clone())); ReturnData[Constants.PATH1] = ((DecodedData)(inputDecodedData[Constants.PATH1].Clone())); ReturnData[Constants.PATH2] = ((DecodedData)(inputDecodedData[Constants.PATH2].Clone())); Int32 temp = 0; UInt16 DatIndex = 0; UInt16 DecodedPath = Constants.PATH0; String ProtocolVersion = String.Empty; #endregion try { #region Decode comment ("DATA CHANNEL") if ((hexValues[0] == "@h") && (hexValues.Count() == TelegramLength)) { #region Reset data ReturnData[Constants.PATH0].DecodingSucceeded = false; ReturnData[Constants.PATH1].DecodingSucceeded = false; ReturnData[Constants.PATH2].DecodingSucceeded = false; ReturnData[Constants.PATH0].MessageType = String.Empty; ReturnData[Constants.PATH1].MessageType = String.Empty; ReturnData[Constants.PATH2].MessageType = String.Empty; #endregion #region Decode foreach (String HexValue in hexValues) { switch (DatIndex) { #region Decode telegram ("DATA CHANNEL") #region Protocol version case 0: ProtocolVersion = HexValue; break; #endregion #region Decode PATH case 1: if (UInt16.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out DecodedPath)) { DecodedPath--; ReturnData[Constants.PATH0].Path = DecodedPath; ReturnData[Constants.PATH1].Path = DecodedPath; ReturnData[Constants.PATH2].Path = DecodedPath; } ReturnData[DecodedPath].ProtocolVersion = ProtocolVersion; break; #endregion #region ERROR case 2: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) ReturnData[DecodedPath].Error = temp; else ReturnData[DecodedPath].Error = DataError.Unknown; break; #endregion #region Total time of flight case 3: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) ReturnData[DecodedPath].TotalTimeOfFlightAverage = temp * ZeroOffsetTestConstants.DeltaTofAverageFactor / 1000000; // value in µs break; #endregion #region Delta time of flight case 4: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) ReturnData[DecodedPath].DeltaTimeOfFlightAverage = temp * ZeroOffsetTestConstants.DeltaTofAverageFactor; // value in ps break; #endregion #region Raw delta volume case 5: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) ReturnData[DecodedPath].RawDeltaVolume = (Double)temp; break; #endregion #region Raw accumulated volume case 6: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) ReturnData[DecodedPath].RawDeltaVolumeAccu = (Double)temp; break; #endregion #region Raw delta volume scaling case 7: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) ReturnData[DecodedPath].RawDeltaVolumeScaling = (Double)temp; break; #endregion #region Sample interval case 8: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) ReturnData[DecodedPath].SampleInterval = (Double)temp; break; #endregion #region Amplitude up case 9: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) ReturnData[DecodedPath].AmplitudeUp = (Double)(temp / Math.Pow(2, 22)); break; #endregion #region Amplitude down case 10: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) ReturnData[DecodedPath].AmplitudeDown = (Double)(temp / Math.Pow(2, 22)); break; #endregion #region Temperature case 11: if (Int32.TryParse(HexValue,NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) ReturnData[DecodedPath].Temperature = temp; break; #endregion #region Temperature scale case 12: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) { ReturnData[DecodedPath].TemperatureScale = temp; ReturnData[DecodedPath].CalculatedTemperature = ((Double)ReturnData[DecodedPath].Temperature) / Math.Pow(2, (Double)temp); } break; #endregion #region Time case 13: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) ReturnData[DecodedPath].Time = temp; break; #endregion #region CRC case 14: if (Int32.TryParse(HexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp)) { ReturnData[DecodedPath].Crc = temp; } break; #endregion #region Default default: break; #endregion #endregion } #region Decoding finished if (DatIndex == 14) { #region Reset index DatIndex = 0; #endregion #region Set data ReturnData[DecodedPath].MessageType = Message.Data; ReturnData[DecodedPath].Flow = ((ReturnData[DecodedPath].RawDeltaVolume / ReturnData[DecodedPath].RawDeltaVolumeScaling) / (ReturnData[DecodedPath].SampleInterval / 65536)) * Constants.FlowFactor; ReturnData[Constants.PATH0].DecodingSucceeded = true; ReturnData[Constants.PATH1].DecodingSucceeded = true; ReturnData[Constants.PATH2].DecodingSucceeded = true; #endregion #region Return data return ReturnData; #endregion } else DatIndex++; // switch to next position of data #endregion } #endregion return ReturnData; } #endregion } catch (Exception) { //ErrorLog.Log(LogErrReason.Exeption, "DecodeTelegram.DecodeProtocolH() failed", $"0x{ex.HResult.ToString("X", getGlobalCulture())}index = {DatIndex.ToString(getGlobalCulture())}; path = {DecodedPath.ToString(getGlobalCulture())}", getGlobalCulture); } return ReturnData; } public static DecodedData[] Reset() { DecodedData[] data = new DecodedData[Constants.NumberOfPaths]; for (UInt16 Path = Constants.PATH0; Path < Constants.NumberOfPaths; Path++) data [Path] = new DecodedData(); return data; } } }