public static IMemoryOwner <byte> TranslateToMemory(S7WriteJobDatagram datagram, out int memoryLength) { var result = S7HeaderDatagram.TranslateToMemory(datagram.Header, out memoryLength); var mem = result.Memory.Slice(0, memoryLength); var span = mem.Span; var offset = datagram.Header.GetHeaderSize(); span[offset++] = datagram.Function; span[offset++] = datagram.ItemCount; foreach (var item in datagram.Items) { S7AddressItemSpecificationDatagram.TranslateToMemory(item, mem.Slice(offset)); offset += item.GetSpecificationLength(); } foreach (var item in datagram.Data) { S7DataItemSpecification.TranslateToMemory(item, mem.Slice(offset)); offset += item.GetSpecificationLength(); if (offset % 2 != 0) { offset++; } } return(result); }
public static S7WriteJobDatagram TranslateFromMemory(Memory <byte> data) { var span = data.Span; var result = new S7WriteJobDatagram { Header = S7HeaderDatagram.TranslateFromMemory(data), }; var offset = result.Header.GetHeaderSize(); result.Function = span[offset++]; result.ItemCount = span[offset++]; for (var i = 0; i < result.ItemCount; i++) { var res = S7AddressItemSpecificationDatagram.TranslateFromMemory(data.Slice(offset)); result.Items.Add(res); offset += res.GetSpecificationLength(); } for (var i = 0; i < result.ItemCount; i++) { var res = S7DataItemSpecification.TranslateFromMemory(data.Slice(offset)); result.Data.Add(res); offset += res.GetSpecificationLength(); } return(result); }
public static Memory <byte> TranslateToMemory(S7AddressItemSpecificationDatagram datagram, Memory <byte> memory) { var result = memory.IsEmpty ? new Memory <byte>(new byte[12]) : memory; // check if we could use ArrayBuffer var span = result.Span; span[0] = datagram.VariableSpecification; span[1] = datagram.LengthOfAddressSpecification; span[2] = datagram.SyntaxId; span[3] = datagram.TransportSize; BinaryPrimitives.WriteUInt16BigEndian(span.Slice(4, 2), datagram.ItemSpecLength); BinaryPrimitives.WriteUInt16BigEndian(span.Slice(6, 2), datagram.DbNumber); span[8] = datagram.Area; datagram.Address.CopyTo(result.Slice(9)); return(result); }
public static S7AddressItemSpecificationDatagram TranslateFromMemory(Memory <byte> data) { var span = data.Span; var result = new S7AddressItemSpecificationDatagram { VariableSpecification = span[0], LengthOfAddressSpecification = span[1], SyntaxId = span[2], TransportSize = span[3], ItemSpecLength = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(4, 2)), DbNumber = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(6, 2)), Area = span[8] }; data.Slice(9, 3).CopyTo(result.Address); return(result); }
public static Memory <byte> TranslateToMemory(S7ReadJobDatagram datagram) { var result = S7HeaderDatagram.TranslateToMemory(datagram.Header); var span = result.Span; var offset = datagram.Header.GetHeaderSize(); span[offset++] = datagram.Function; span[offset++] = datagram.ItemCount; foreach (var item in datagram.Items) { S7AddressItemSpecificationDatagram.TranslateToMemory(item, result.Slice(offset)); offset += item.GetSpecificationLength(); } return(result); }
public static S7AddressItemSpecificationDatagram TranslateFromMemory(Memory <byte> data) { var span = data.Span; var result = new S7AddressItemSpecificationDatagram { VariableSpecification = span[0], LengthOfAddressSpecification = span[1], SyntaxId = span[2], TransportSize = span[3], ItemSpecLength = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(4, 2)), DbNumber = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(6, 2)), Area = span[8] }; result.Address = new byte[3]; data.Slice(9, 3).CopyTo(result.Address); result.Offset = S7AddressItemSpecificationDatagram.CalcOffsetFromAddress(result.Address, result.TransportSize == (byte)ItemDataTransportSize.Bit ? typeof(bool) : typeof(byte)); return(result); }
public static S7WriteJobDatagram BuildWrite(SiemensPlcProtocolContext context, int id, IEnumerable <WriteItem> vars) { var numberOfItems = 0; var result = new S7WriteJobDatagram(); result.Header.ProtocolDataUnitReference = (ushort)id; if (vars != null) { foreach (var item in vars) { numberOfItems++; result.Items.Add(new S7AddressItemSpecificationDatagram { TransportSize = S7AddressItemSpecificationDatagram.GetTransportSize(item.Area, item.VarType), ItemSpecLength = item.NumberOfItems, DbNumber = item.DbNumber, Area = (byte)item.Area, Address = S7AddressItemSpecificationDatagram.GetAddress(item.Offset, item.VarType) }); } foreach (var item in vars) { numberOfItems--; result.Data.Add(new S7DataItemSpecification { ReturnCode = 0x00, TransportSize = S7DataItemSpecification.GetTransportSize(item.Area, item.VarType), Length = item.NumberOfItems, Data = item.Data, FillByte = numberOfItems == 0 || item.NumberOfItems % 2 == 0 ? Array.Empty <byte>() : new byte[1], ElementSize = item.ElementSize }); } } result.Header.ParamLength = (ushort)(2 + result.Items.Count * 12); result.Header.DataLength = (ushort)(S7DataItemSpecification.GetDataLength(vars) + result.Items.Count * 4); result.ItemCount = (byte)result.Items.Count; return(result); }
public static S7ReadJobDatagram BuildRead(SiemensPlcProtocolContext context, int id, IEnumerable <ReadItem> vars) { var result = new S7ReadJobDatagram(); result.Header.ProtocolDataUnitReference = (ushort)id; if (vars != null) { foreach (var item in vars) { result.Items.Add(new S7AddressItemSpecificationDatagram { TransportSize = S7AddressItemSpecificationDatagram.GetTransportSize(item.Area, item.VarType), ItemSpecLength = item.Length, DbNumber = item.DbNumber, Area = (byte)item.Area, Address = S7AddressItemSpecificationDatagram.GetAddress(item.Offset, item.VarType) }); } } result.Header.ParamLength = (ushort)(2 + result.Items.Count * 12); result.ItemCount = (byte)result.Items.Count; return(result); }