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

37 lines
1.0 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames;
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));
}
}