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

40 lines
1.4 KiB
C#

using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors;
using static InnovEnergy.Lib.Protocols.Modbus.Protocol.FunctionCode;
namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Replies;
public class WriteCoilsResponseFrame : ModbusFrame
{
private const Int32 Size = 6;
public MbAddress WriteAddress => Data.AddressAt(2);
public MbWord NbWritten => Data.WordAt(4);
internal static Int32 ExpectedSize() => Size;
public WriteCoilsResponseFrame(Byte slave, UInt16 writeAddress, UInt16 nbWritten) : base(Size)
{
SlaveAddress .Set(slave);
FunctionCode .Set(WriteMultipleCoils);
WriteAddress .Set(writeAddress);
NbWritten .Set(nbWritten);
}
private WriteCoilsResponseFrame(Byte[] data) : this (new ArraySegment<Byte>(data) )
{ }
private WriteCoilsResponseFrame(ArraySegment<Byte> data) : base(data)
{
if (data.Count != Size)
throw new ArgumentException($"Expecting an array of size {Size}", nameof(data));
AssertFunctionCode(WriteMultipleCoils);
}
public static WriteCoilsResponseFrame Parse(ArraySegment<Byte> data) => new WriteCoilsResponseFrame(data);
public static WriteCoilsResponseFrame Parse(Byte[] data) => new WriteCoilsResponseFrame(data);
public static WriteCoilsResponseFrame Parse(ModbusFrame frame) => new WriteCoilsResponseFrame(frame.Data);
}