2023-02-16 12:57:06 +00:00
|
|
|
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors;
|
|
|
|
using InnovEnergy.Lib.Utils;
|
|
|
|
using static InnovEnergy.Lib.Protocols.Modbus.Protocol.FunctionCode;
|
|
|
|
|
|
|
|
namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Replies;
|
|
|
|
|
|
|
|
using Booleans = IReadOnlyList<Boolean>;
|
|
|
|
using MbFc = MbByte<FunctionCode>;
|
|
|
|
|
2023-04-20 13:03:13 +00:00
|
|
|
public class ReadDiscreteInputsResponseFrame : ModbusFrame
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
|
|
|
private new const Int32 MinSize = 3;
|
|
|
|
|
|
|
|
private MbByte ByteCount => Data.ByteAt(2);
|
|
|
|
public MbBits Inputs => Data.BitsAt(3);
|
|
|
|
|
2023-04-20 13:03:13 +00:00
|
|
|
public ReadDiscreteInputsResponseFrame(Byte slave, Booleans inputs) : base (inputs.Count)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
|
|
|
var nBytes = Math.Ceiling(inputs.Count / 8.0).ConvertTo<UInt16>();
|
|
|
|
|
|
|
|
SlaveAddress .Set(slave);
|
|
|
|
FunctionCode .Set(ReadDiscreteInputs);
|
|
|
|
ByteCount .Set(nBytes);
|
|
|
|
Inputs .Set(inputs);
|
|
|
|
}
|
|
|
|
|
2023-04-20 13:03:13 +00:00
|
|
|
private ReadDiscreteInputsResponseFrame(Byte[] data) : this(new ArraySegment<Byte>(data))
|
2023-02-16 12:57:06 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
2023-04-20 13:03:13 +00:00
|
|
|
private ReadDiscreteInputsResponseFrame(ArraySegment<Byte> data) : base(data)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
AssertFunctionCode(ReadDiscreteInputs);
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// var expectedSize = ByteCount + MinSize;
|
|
|
|
// if (data.Count != expectedSize)
|
|
|
|
// throw new ArgumentException($"Expecting an array of size {expectedSize}", nameof(data));
|
|
|
|
// if (data.Count < MinSize)
|
|
|
|
// throw new ArgumentException($"Expecting an array of size {MinSize} or more", nameof(data));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-04 07:32:35 +00:00
|
|
|
public static ReadDiscreteInputsResponseFrame Parse(Byte[] data)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-05-04 07:32:35 +00:00
|
|
|
return new ReadDiscreteInputsResponseFrame(data);
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
2023-05-04 07:32:35 +00:00
|
|
|
|
2023-04-20 13:03:13 +00:00
|
|
|
public static ReadDiscreteInputsResponseFrame Parse(ArraySegment<Byte> data)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-04-20 13:03:13 +00:00
|
|
|
return new ReadDiscreteInputsResponseFrame(data);
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
}
|