public static S7UserDataParameter TranslateFromMemory(Memory <byte> data) { var span = data.Span; var result = new S7UserDataParameter(); span.Slice(0, 3).CopyTo(result.ParamHeader); result.ParamDataLength = span[3]; result.ParameterType = span[4]; result.TypeAndGroup = span[5]; result.SubFunction = span[6]; result.SequenceNumber = span[7]; if (result.ParamDataLength == 0x08) { result.DataUnitReferenceNumber = span[8]; result.LastDataUnit = span[9]; result.ParamErrorCode = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(10, 2)); } return(result); }
public static Memory <byte> TranslateToMemory(S7UserDataParameter datagram, Memory <byte> memory) { var result = memory.IsEmpty ? new Memory <byte>(new byte[datagram.ParamDataLength]) : memory; // check if we could use ArrayBuffer var span = result.Span; datagram.ParamHeader.CopyTo(span.Slice(0, 3)); span[3] = datagram.ParamDataLength; span[4] = datagram.ParameterType; span[5] = datagram.TypeAndGroup; span[6] = datagram.SubFunction; span[7] = datagram.SequenceNumber; if (datagram.ParamDataLength == 0x08) { span[8] = datagram.DataUnitReferenceNumber; span[9] = datagram.LastDataUnit; BinaryPrimitives.WriteUInt16BigEndian(span.Slice(10, 2), datagram.ParamErrorCode); } return(result); }