/*----------------------------------------------------------------------------- Module: CANoeFDX Interfaces: - ------------------------------------------------------------------------------- Fast Data eXchange (FDX) data definition for connecting a HIL system over UPD to CANoe Implementation of dedicated dispatcher to match data definition ------------------------------------------------------------------------------- Copyright (c) Vector Informatik GmbH. All rights reserved. -----------------------------------------------------------------------------*/ using System; namespace FDXClient { /// /// This class implements an own A429DataDispatcher to overwrite the data handler for status and data exchange /// public class A429DataDispatcher : FDXDispatcher { private uint _lastGrossWeight; /// /// This function overrides the empty data handler in the base class /// /// Received exchange data public override void OnDataExchange(DataExchangeCommand exData) { switch (exData.GroupId) { case ExchangeData.A429InputData.sGroupId: { var dt = ExchangeData.A429InputData.DeSerialize(exData.DataBytes); Console.Out.WriteLine("SigFuel_Temp_And_Advisory_Warning: {0} Probe_Capacitance: {1}", dt.FuelTempAndAdvisoryWarning, dt.ProbeCapacitance); break; } case ExchangeData.A429OutputData.sGroupId: { var dt = ExchangeData.A429OutputData.DeSerialize(exData.DataBytes); if (this._lastGrossWeight != dt.GrossWeight) { Console.Out.WriteLine("Gross_Weight: {0}", dt.GrossWeight); this._lastGrossWeight = dt.GrossWeight; } break; } default: { Console.Out.WriteLine("Invalid GroupId received: {0}", exData.GroupId); break; } } } /// /// This function overrides the status handler in base class to set the internal communication status. Nevertheless the statushandler /// in the base class is called . /// /// public override void OnStatus(StatusCommand comStat) { if (FDXHelperMeasurementState.CurrentMeasurementState != comStat.MeasurementState) { base.OnStatus(comStat); FDXHelperMeasurementState.CurrentMeasurementState = comStat.MeasurementState; } } } namespace ExchangeData { [Serializable] public class A429InputData { public const ushort sGroupId = 1; public short FuelTempAndAdvisoryWarning { get; set; } public float ProbeCapacitance { get; set; } public byte Reserved1 { get; private set; } /// /// This function serialize the class to a byte array /// /// byte array with the serialized data public byte[] Serialize() { using (var m = new System.IO.MemoryStream()) { using (var writer = new System.IO.BinaryWriter(m)) { writer.Write(this.FuelTempAndAdvisoryWarning); writer.Write(this.ProbeCapacitance); writer.Write(this.Reserved1); } return m.ToArray(); } } /// /// This function deserialize from a byte array and return a new class filled /// with the data /// /// serialized data that contains the information /// a new class filled with the deserialized data public static A429InputData DeSerialize(byte[] data) { var a429InputData = new A429InputData(); using (var m = new System.IO.MemoryStream(data)) { using (var reader = new System.IO.BinaryReader(m)) { a429InputData.FuelTempAndAdvisoryWarning = reader.ReadInt16(); a429InputData.ProbeCapacitance = reader.ReadByte(); a429InputData.Reserved1 = reader.ReadByte(); } } return a429InputData; } } [Serializable] public class A429OutputData { public const ushort sGroupId = 2; public uint GrossWeight { get; set; } public byte Reserved1 { get; private set; } /// /// This function serialize the class to a byte array /// /// byte array with the serialized data public byte[] Serialize() { using (var m = new System.IO.MemoryStream()) { using (var writer = new System.IO.BinaryWriter(m)) { writer.Write(this.GrossWeight); writer.Write(this.Reserved1); } return m.ToArray(); } } /// /// This function deserialize from a byte array and return a new class filled /// with the data /// /// serialized data that contains the information /// a new class filled with the deserialized data public static A429OutputData DeSerialize(byte[] data) { var a429OutputData = new A429OutputData(); using (var m = new System.IO.MemoryStream(data)) { using (var reader = new System.IO.BinaryReader(m)) { a429OutputData.GrossWeight = reader.ReadUInt32(); a429OutputData.Reserved1 = reader.ReadByte(); } } return a429OutputData; } } [Serializable] public class A429OvhdData { public const byte sGroupId = 3; //In case of SysVar-Arrays we need 4 Bytes padding at the begin public int Reserved1 { get; private set; } /// /// Getter and Setter for SysMainEng1 (first bit in System variable-array) /// public byte SysMainEng1 { get { return ((this._internalData[0] & 0x01) > 0) ? (byte)1 : (byte)0; } set { if(value == 1) this._internalData[0] = (byte)(this._internalData[0] | 0x01); else { this._internalData[0] = (byte)(this._internalData[0] & 0xFE); } } } /// /// Getter and Setter for SysMainEng2 (second bit in System variable-array) /// public byte SysMainEng2 { get { return ((this._internalData[0] & 0x02) > 0) ? (byte)1 : (byte)0; } set { if (value == 1) this._internalData[0] = (byte)(this._internalData[0] | 0x02); else { this._internalData[0] = (byte)(this._internalData[0] & 0xFD); } } } /// /// Getter and Setter for SysMainEng3 (third bit in System variable-array) /// public byte SysMainEng3 { get { return ((this._internalData[0] & 0x04) > 0) ? (byte)1 : (byte)0; } set { if (value == 1) this._internalData[0] = (byte)(this._internalData[0] | 0x04); else { this._internalData[0] = (byte)(this._internalData[0] & 0xFB); } } } /// /// Getter and Setter for SysMainEng4 (fourth bit in System variable-array) /// public byte SysMainEng4 { get { return ((this._internalData[0] & 0x08) > 0) ? (byte)1 : (byte)0; } set { if (value == 1) this._internalData[0] = (byte)(this._internalData[0] | 0x08); else { this._internalData[0] = (byte)(this._internalData[0] & 0xF7); } } } /// /// Getter and Setter for SysTTankL (seventh bit in System variable-array) /// public byte SysTTankL { get { return ((this._internalData[0] & 0x40) > 0) ? (byte)1 : (byte)0; } set { if (value == 1) this._internalData[0] = (byte)(this._internalData[0] | 0x40); else { this._internalData[0] = (byte)(this._internalData[0] & 0xBF); } } } /// /// Getter and Setter for SysTTankR (eighth bit in System variable-array) /// public byte SysTTankR { get { return ((this._internalData[0] & 0x80) > 0) ? (byte)1 : (byte)0; } set { if (value == 1) this._internalData[0] = (byte)(this._internalData[0] | 0x80); else { this._internalData[0] = (byte)(this._internalData[0] & 0x7F); } } } public ushort Reserved2 { get; private set; } private readonly byte[] _internalData = new byte[2]; /// /// This function serialize the class to a byte array /// /// byte array with the serialized data public byte[] Serialize() { using (var m = new System.IO.MemoryStream()) { using (var writer = new System.IO.BinaryWriter(m)) { writer.Write(this.Reserved1); writer.Write(this._internalData.Length); writer.Write(this._internalData); writer.Write(this.Reserved2); } return m.ToArray(); } } // This function deserialize from a byte array and return a new class filled //with the data public static A429OvhdData DeSerialize(byte[] data) { var ovhdData = new A429OvhdData(); using (var m = new System.IO.MemoryStream(data)) { using (var reader = new System.IO.BinaryReader(m)) { ovhdData.Reserved1 = reader.ReadInt32(); ovhdData._internalData[0] = reader.ReadByte(); ovhdData._internalData[1] = reader.ReadByte(); ovhdData.Reserved2 = reader.ReadByte(); } } return ovhdData; } } } }