Innovenergy_trunk/csharp/Lib/Protocols/Modbus/Tcp/MbapHeader.cs

36 lines
1006 B
C#

using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors;
namespace InnovEnergy.Lib.Protocols.Modbus.Tcp;
internal readonly struct MbapHeader
{
public const Int32 Size = 6;
public ArraySegment<Byte> Data { get; }
public MbWord Id => Data.WordAt(0);
public MbWord Protocol => Data.WordAt(2);
public MbWord FrameLength => Data.WordAt(4);
public MbapHeader(UInt16 transactionId, Int32 frameLength)
{
Data = new ArraySegment<Byte>(new Byte[Size]);
Id.Set(transactionId);
FrameLength.Set(frameLength);
}
public MbapHeader(Byte[] data) : this(new ArraySegment<Byte>(data))
{ }
public MbapHeader(ArraySegment<Byte> data)
{
Data = data;
if (data.Count != Size)
throw new ArgumentOutOfRangeException($"Expecting an array exactly {Size} bytes long!");
if (Protocol != 0)
throw new ArgumentException($"Protocol {Protocol} not supported!", nameof(Protocol));
}
}