2023-04-20 13:03:13 +00:00
|
|
|
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors;
|
|
|
|
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Replies;
|
|
|
|
using InnovEnergy.Lib.Utils;
|
2023-05-04 07:32:35 +00:00
|
|
|
using static InnovEnergy.Lib.Protocols.Modbus.Protocol.FunctionCode;
|
2023-04-20 13:03:13 +00:00
|
|
|
|
|
|
|
namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Commands;
|
|
|
|
|
|
|
|
internal class ReadCoilsCommandFrame : ModbusFrame
|
|
|
|
{
|
|
|
|
private const Int32 Size = 6;
|
|
|
|
|
2023-05-04 07:32:35 +00:00
|
|
|
public MbWord ReadAddress => Data.WordAt(2);
|
|
|
|
public MbWord NumberOfCoils => Data.WordAt(4);
|
2023-04-20 13:03:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
public ReadCoilsCommandFrame(Byte slave, UInt16 readAddress, UInt16 nBits) : base(Size)
|
|
|
|
{
|
|
|
|
if (nBits > Constants.MaxCoils)
|
|
|
|
throw new ArgumentOutOfRangeException($"Maximum number of registers ({Constants.MaxCoils}) exceeeded!", nameof(nBits));
|
|
|
|
|
2023-05-04 07:32:35 +00:00
|
|
|
SlaveAddress .Set(slave);
|
|
|
|
FunctionCode .Set(ReadCoils);
|
|
|
|
ReadAddress .Set(readAddress);
|
|
|
|
NumberOfCoils.Set(nBits);
|
2023-04-20 13:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ReadCoilsCommandFrame(ArraySegment<Byte> data) : base(data)
|
|
|
|
{
|
|
|
|
if (data.Count != Size)
|
|
|
|
throw new ArgumentException($"Expecting an array of size {Size}", nameof(data));
|
|
|
|
|
2023-05-04 07:32:35 +00:00
|
|
|
AssertFunctionCode(ReadCoils);
|
2023-04-20 13:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ReadCoilsResponseFrame VerifyResponse(ReadCoilsResponseFrame response)
|
|
|
|
{
|
|
|
|
if (response.SlaveAddress != SlaveAddress)
|
|
|
|
throw new UnexpectedResponseFieldException(nameof(response.SlaveAddress), SlaveAddress.ToString(), response.SlaveAddress);
|
|
|
|
|
2023-05-04 07:32:35 +00:00
|
|
|
if (response.Coils.Count != Math.Ceiling(NumberOfCoils / 8.0).ConvertTo<Int32>() * 8)
|
|
|
|
throw new UnexpectedResponseFieldException(nameof(response.Coils), NumberOfCoils.ToString(), response.Coils.Count);
|
2023-04-20 13:03:13 +00:00
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ReadCoilsCommandFrame Parse(ArraySegment<Byte> data)
|
|
|
|
{
|
|
|
|
return new ReadCoilsCommandFrame(data);
|
|
|
|
}
|
|
|
|
}
|