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

45 lines
1.5 KiB
C#
Raw Permalink Normal View History

2023-02-16 12:57:06 +00:00
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors;
using InnovEnergy.Lib.Utils;
namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Replies;
public class ReadCoilsResponseFrame : ModbusFrame
2023-02-16 12:57:06 +00:00
{
private new const Int32 MinSize = 3;
2023-05-04 07:32:35 +00:00
private MbByte ByteCount => Data.ByteAt(2);
internal MbBits Coils => Data.BitsAt(3);
2023-02-16 12:57:06 +00:00
public ReadCoilsResponseFrame(Byte slave, IReadOnlyList<Boolean> 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(Protocol.FunctionCode.ReadCoils);
2023-02-16 12:57:06 +00:00
ByteCount .Set(nBytes);
2023-05-04 07:32:35 +00:00
Coils .Set(inputs);
2023-02-16 12:57:06 +00:00
}
private ReadCoilsResponseFrame(Byte[] data) : this(new ArraySegment<Byte>(data))
2023-02-16 12:57:06 +00:00
{ }
private ReadCoilsResponseFrame(ArraySegment<Byte> data) : base(data)
2023-02-16 12:57:06 +00:00
{
AssertFunctionCode(Protocol.FunctionCode.ReadCoils);
2023-02-16 12:57:06 +00:00
// 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 ReadCoilsResponseFrame Parse(Byte[] data) => new(data);
2023-02-16 12:57:06 +00:00
2023-05-04 07:32:35 +00:00
public static ReadCoilsResponseFrame Parse(ArraySegment<Byte> data) => new(data);
2023-02-16 12:57:06 +00:00
}