public static Memory <byte> TranslateToMemory(S7HeaderDatagram datagram) { var length = datagram.GetMemorySize(); var result = new Memory <byte>(new byte[length]); // check if we could use ArrayBuffer var span = result.Span; span[0] = datagram.ProtocolId; span[1] = datagram.PduType; BinaryPrimitives.WriteUInt16BigEndian(span.Slice(2, 2), datagram.RedundancyIdentification); BinaryPrimitives.WriteUInt16BigEndian(span.Slice(4, 2), datagram.ProtocolDataUnitReference); BinaryPrimitives.WriteUInt16BigEndian(span.Slice(6, 2), datagram.ParamLength); BinaryPrimitives.WriteUInt16BigEndian(span.Slice(8, 2), datagram.DataLength); return(result); }
public static IMemoryOwner <byte> TranslateToMemory(S7HeaderDatagram datagram, int length, out int memoryLength) { memoryLength = length == -1 ? datagram.GetMemorySize() : length; var result = MemoryPool <byte> .Shared.Rent(memoryLength); var span = result.Memory.Slice(0, memoryLength).Span; span[0] = datagram.ProtocolId; span[1] = datagram.PduType; BinaryPrimitives.WriteUInt16BigEndian(span.Slice(2, 2), datagram.RedundancyIdentification); BinaryPrimitives.WriteUInt16BigEndian(span.Slice(4, 2), datagram.ProtocolDataUnitReference); BinaryPrimitives.WriteUInt16BigEndian(span.Slice(6, 2), datagram.ParamLength); BinaryPrimitives.WriteUInt16BigEndian(span.Slice(8, 2), datagram.DataLength); return(result); }