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

55 lines
1.7 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;
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
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);
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);
}
private ReadDiscreteInputsResponseFrame(Byte[] data) : this(new ArraySegment<Byte>(data))
2023-02-16 12:57:06 +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
public static ReadDiscreteInputsResponseFrame Parse(ArraySegment<Byte> data)
2023-02-16 12:57:06 +00:00
{
return new ReadDiscreteInputsResponseFrame(data);
2023-02-16 12:57:06 +00:00
}
}