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

55 lines
1.8 KiB
C#
Raw 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 ReadDiscreteInputResponseFrame : ModbusFrame
{
private new const Int32 MinSize = 3;
private MbByte ByteCount => Data.ByteAt(2);
public MbBits Inputs => Data.BitsAt(3);
public ReadDiscreteInputResponseFrame(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 ReadDiscreteInputResponseFrame(Byte[] data) : this(new ArraySegment<Byte>(data))
{ }
private ReadDiscreteInputResponseFrame(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 ReadDiscreteInputResponseFrame Parse(ModbusFrame rawFrame)
{
return new ReadDiscreteInputResponseFrame(rawFrame.Data);
}
public static ReadDiscreteInputResponseFrame Parse(ArraySegment<Byte> data)
{
return new ReadDiscreteInputResponseFrame(data);
}
}