58 lines
2.0 KiB
C#
58 lines
2.0 KiB
C#
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors;
|
|
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Replies;
|
|
|
|
using static InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Constants;
|
|
using static InnovEnergy.Lib.Protocols.Modbus.Protocol.FunctionCode;
|
|
|
|
|
|
namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Commands;
|
|
|
|
using MbFc = MbByte<FunctionCode>;
|
|
|
|
internal class ReadHoldingRegistersCommandFrame : ModbusFrame
|
|
{
|
|
|
|
internal const Int32 Size = 6;
|
|
|
|
public MbWord ReadAddress => Data.WordAt(2);
|
|
public MbWord NbToRead => Data.WordAt(4);
|
|
|
|
public Int32 ExpectedResponseSize => ReadHoldingRegistersResponseFrame.ExpectedSize(NbToRead);
|
|
|
|
public ReadHoldingRegistersCommandFrame(Byte slave, UInt16 readAddress, UInt16 nValues) : base(Size)
|
|
{
|
|
if (nValues > MaxRegs)
|
|
throw new ArgumentOutOfRangeException($"Maximum number of registers ({MaxRegs}) exceeeded!", nameof(nValues));
|
|
|
|
SlaveAddress.Set(slave);
|
|
FunctionCode.Set(ReadHoldingRegisters);
|
|
ReadAddress .Set(readAddress);
|
|
NbToRead .Set(nValues);
|
|
}
|
|
|
|
|
|
private ReadHoldingRegistersCommandFrame(ArraySegment<Byte> data) : base(data)
|
|
{
|
|
if (data.Count != Size)
|
|
throw new ArgumentException($"Expecting an array of size {Size}", nameof(data));
|
|
|
|
AssertFunctionCode(ReadHoldingRegisters);
|
|
}
|
|
|
|
|
|
public ReadHoldingRegistersResponseFrame VerifyResponse(ReadHoldingRegistersResponseFrame r)
|
|
{
|
|
if (r.SlaveAddress != SlaveAddress)
|
|
throw new UnexpectedResponseFieldException(nameof(r.SlaveAddress), SlaveAddress.ToString(), r.SlaveAddress);
|
|
|
|
if (r.RegistersRead.Count != NbToRead)
|
|
throw new UnexpectedResponseFieldException(nameof(r.RegistersRead), NbToRead.ToString(), r.RegistersRead.Count);
|
|
|
|
return r;
|
|
}
|
|
|
|
public static ReadHoldingRegistersCommandFrame Parse(ArraySegment<Byte> data)
|
|
{
|
|
return new ReadHoldingRegistersCommandFrame(data);
|
|
}
|
|
} |