Innovenergy_trunk/csharp/Lib/Protocols/Modbus/Protocol/Frames/Replies/ReadDiscreteInputsResponseF...

55 lines
1.7 KiB
C#

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>;
public class ReadDiscreteInputsResponseFrame : ModbusFrame
{
private new const Int32 MinSize = 3;
private MbByte ByteCount => Data.ByteAt(2);
public MbBits Inputs => Data.BitsAt(3);
public ReadDiscreteInputsResponseFrame(Byte slave, Booleans inputs) : base (inputs.Count)
{
var nBytes = Math.Ceiling(inputs.Count / 8.0).ConvertTo<UInt16>();
SlaveAddress .Set(slave);
FunctionCode .Set(ReadDiscreteInputs);
ByteCount .Set(nBytes);
Inputs .Set(inputs);
}
private ReadDiscreteInputsResponseFrame(Byte[] data) : this(new ArraySegment<Byte>(data))
{ }
private ReadDiscreteInputsResponseFrame(ArraySegment<Byte> data) : base(data)
{
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));
}
public static ReadDiscreteInputsResponseFrame Parse(Byte[] data)
{
return new ReadDiscreteInputsResponseFrame(data);
}
public static ReadDiscreteInputsResponseFrame Parse(ArraySegment<Byte> data)
{
return new ReadDiscreteInputsResponseFrame(data);
}
}