Innovenergy_trunk/csharp/Lib/Protocols/Modbus/Protocol/Frames/Replies/ReadCoilsResponseFrame.cs

45 lines
1.5 KiB
C#

using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors;
using InnovEnergy.Lib.Utils;
namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Replies;
public class ReadCoilsResponseFrame : ModbusFrame
{
private new const Int32 MinSize = 3;
private MbByte ByteCount => Data.ByteAt(2);
internal MbBits Coils => Data.BitsAt(3);
public ReadCoilsResponseFrame(Byte slave, IReadOnlyList<Boolean> inputs) : base (inputs.Count)
{
var nBytes = Math.Ceiling(inputs.Count / 8.0).ConvertTo<UInt16>();
SlaveAddress .Set(slave);
FunctionCode .Set(Protocol.FunctionCode.ReadCoils);
ByteCount .Set(nBytes);
Coils .Set(inputs);
}
private ReadCoilsResponseFrame(Byte[] data) : this(new ArraySegment<Byte>(data))
{ }
private ReadCoilsResponseFrame(ArraySegment<Byte> data) : base(data)
{
AssertFunctionCode(Protocol.FunctionCode.ReadCoils);
// 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 ReadCoilsResponseFrame Parse(Byte[] data) => new(data);
public static ReadCoilsResponseFrame Parse(ArraySegment<Byte> data) => new(data);
}