Make MbWords writable

This commit is contained in:
ig 2023-03-10 10:11:59 +01:00
parent 366e0af986
commit 2d242f25b7
2 changed files with 15 additions and 11 deletions

View File

@ -8,10 +8,7 @@ public static class Accessors
public static MbByte ByteAt (this ArraySegment<Byte> data, Byte i) => new MbByte(data, i);
public static MbWord WordAt (this ArraySegment<Byte> data, Byte i) => new MbWord(data, i);
public static MbAddress AddressAt(this ArraySegment<Byte> data, Byte i) => new MbAddress(data, i);
public static MbRegisters RegistersAt(this ArraySegment<Byte> data, Byte i) => new MbRegisters(data, i);
public static MbBits BitsAt (this ArraySegment<Byte> data, Byte i) => new MbBits(data, i);
public static MbWords WordsAt (this ArraySegment<Byte> data, Byte i) => new MbWords(data, i);
public static MbBits BitsAt (this ArraySegment<Byte> data, Byte i) => new MbBits(data, i);
public static MbByte<T> ByteAt<T>(this ArraySegment<Byte> data,Byte i) where T : struct, IConvertible => new MbByte<T>(data, i);
}

View File

@ -3,20 +3,18 @@ using InnovEnergy.Lib.Utils;
namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors;
public readonly struct MbRegisters : IReadOnlyList<UInt16>
public readonly struct MbWords : IReadOnlyList<UInt16>
{
private readonly ArraySegment<Byte> _Data;
internal MbRegisters(ArraySegment<Byte> data, Byte startIndex) : this(data, startIndex, CountWords(data, startIndex))
internal MbWords(ArraySegment<Byte> data, Byte startIndex) : this(data, startIndex, CountWords(data, startIndex))
{ }
internal MbRegisters(ArraySegment<Byte> data, Byte startIndex, UInt16 wordCount) : this(data.Array!, startIndex, wordCount)
internal MbWords(ArraySegment<Byte> data, Byte startIndex, UInt16 wordCount) : this(data.Array!, startIndex, wordCount)
{
}
internal MbRegisters(Byte[] data, Byte startIndex, UInt16 wordCount)
internal MbWords(Byte[] data, Byte startIndex, UInt16 wordCount)
{
_Data = new ArraySegment<Byte>(data, startIndex, wordCount * 2);
}
@ -62,6 +60,8 @@ public readonly struct MbRegisters : IReadOnlyList<UInt16>
public UInt16 this[Int32 index]
{
// a single register is always big endian (according to standard)
get
{
var i = index * 2;
@ -71,5 +71,12 @@ public readonly struct MbRegisters : IReadOnlyList<UInt16>
return (UInt16) (hi | lo);
}
set
{
var i = index * 2;
_Data[i + 0] = (Byte)(value >> 8);
_Data[i + 1] = (Byte)(value & 0xFF);
}
}
}