2023-02-16 12:57:06 +00:00
|
|
|
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors;
|
|
|
|
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Replies;
|
|
|
|
using InnovEnergy.Lib.Utils;
|
|
|
|
using static InnovEnergy.Lib.Protocols.Modbus.Protocol.FunctionCode;
|
|
|
|
using static InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Constants;
|
|
|
|
|
|
|
|
|
|
|
|
namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Commands;
|
|
|
|
|
|
|
|
using MbFc = MbByte<FunctionCode>;
|
|
|
|
|
|
|
|
internal class ReadDiscreteInputsCommandFrame : ModbusFrame
|
|
|
|
{
|
|
|
|
private const Int32 Size = 6;
|
|
|
|
|
2023-03-10 12:46:46 +00:00
|
|
|
private MbWord ReadAddress => Data.WordAt(2);
|
|
|
|
private MbWord QuantityOfInputs => Data.WordAt(4);
|
2023-02-16 12:57:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
public ReadDiscreteInputsCommandFrame(Byte slave, UInt16 readAddress, UInt16 nBits) : base(Size)
|
|
|
|
{
|
|
|
|
if (nBits > MaxDiscreteInputs)
|
|
|
|
throw new ArgumentOutOfRangeException($"Maximum number of registers ({MaxDiscreteInputs}) exceeeded!", nameof(nBits));
|
|
|
|
|
|
|
|
SlaveAddress .Set(slave);
|
|
|
|
FunctionCode .Set(ReadDiscreteInputs);
|
|
|
|
ReadAddress .Set(readAddress);
|
|
|
|
QuantityOfInputs.Set(nBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ReadDiscreteInputsCommandFrame(ArraySegment<Byte> data) : base(data)
|
|
|
|
{
|
|
|
|
if (data.Count != Size)
|
|
|
|
throw new ArgumentException($"Expecting an array of size {Size}", nameof(data));
|
|
|
|
|
|
|
|
AssertFunctionCode(ReadDiscreteInputs);
|
|
|
|
}
|
|
|
|
|
2023-04-20 13:03:13 +00:00
|
|
|
public ReadDiscreteInputsResponseFrame VerifyResponse(ReadDiscreteInputsResponseFrame response)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
|
|
|
if (response.SlaveAddress != SlaveAddress)
|
|
|
|
throw new UnexpectedResponseFieldException(nameof(response.SlaveAddress), SlaveAddress.ToString(), response.SlaveAddress);
|
|
|
|
|
|
|
|
if (response.Inputs.Count != Math.Ceiling(QuantityOfInputs / 8.0).ConvertTo<Int32>() * 8)
|
|
|
|
throw new UnexpectedResponseFieldException(nameof(response.Inputs), QuantityOfInputs.ToString(), response.Inputs.Count);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ReadDiscreteInputsCommandFrame Parse(ArraySegment<Byte> data)
|
|
|
|
{
|
|
|
|
return new ReadDiscreteInputsCommandFrame(data);
|
|
|
|
}
|
|
|
|
}
|