Esempio n. 1
0
        public static Memory <byte> TranslateToMemory(S7CommSetupDatagram datagram)
        {
            var result = S7HeaderDatagram.TranslateToMemory(datagram.Header);

            S7CommSetupParameterDatagram.TranslateToMemory(datagram.Parameter, result.Slice(datagram.Header.GetHeaderSize()));
            return(result);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        public static Memory <byte> TranslateToMemory(S7AckDataDatagram datagram)
        {
            var result = S7HeaderDatagram.TranslateToMemory(datagram.Header);

            S7HeaderErrorCodesDatagram.TranslateToMemory(datagram.Error, result.Slice(datagram.Header.GetHeaderSize()));
            return(result);
        }
Esempio n. 5
0
        public static IMemoryOwner <byte> TranslateToMemory(S7CommSetupDatagram datagram, out int memoryLength)
        {
            var result = S7HeaderDatagram.TranslateToMemory(datagram.Header, out memoryLength);
            var take   = memoryLength - datagram.Header.GetHeaderSize();

            S7CommSetupParameterDatagram.TranslateToMemory(datagram.Parameter, result.Memory.Slice(datagram.Header.GetHeaderSize(), take));
            return(result);
        }
Esempio n. 6
0
        public static IMemoryOwner <byte> TranslateToMemory(S7AckDataDatagram datagram, out int memoryLength)
        {
            var result = S7HeaderDatagram.TranslateToMemory(datagram.Header, datagram.Header.GetHeaderSize() + datagram.Error.GetSize() + datagram.Header.ParamLength + datagram.Header.DataLength, out memoryLength);
            var take   = memoryLength - datagram.Header.GetHeaderSize();

            S7HeaderErrorCodesDatagram.TranslateToMemory(datagram.Error, result.Memory.Slice(datagram.Header.GetHeaderSize(), take));
            return(result);
        }
Esempio n. 7
0
        public static S7CommSetupDatagram TranslateFromMemory(Memory <byte> data)
        {
            var result = new S7CommSetupDatagram
            {
                Header = S7HeaderDatagram.TranslateFromMemory(data)
            };

            result.Parameter = S7CommSetupParameterDatagram.TranslateFromMemory(data.Slice(result.Header.GetHeaderSize()));
            return(result);
        }
Esempio n. 8
0
        public static S7AckDataDatagram TranslateFromMemory(Memory <byte> data)
        {
            var result = new S7AckDataDatagram
            {
                Header = S7HeaderDatagram.TranslateFromMemory(data)
            };

            result.Error = S7HeaderErrorCodesDatagram.TranslateFromMemory(data.Slice(result.Header.GetHeaderSize()));

            return(result);
        }
Esempio n. 9
0
        public static IMemoryOwner <byte> TranslateToMemory(S7UserDataDatagram datagram, out int memoryLength)
        {
            var offset   = datagram.Header.GetHeaderSize() + datagram.Parameter.GetParamSize();
            var dataSize = offset + datagram.Data.GetUserDataLength();
            var result   = S7HeaderDatagram.TranslateToMemory(datagram.Header, dataSize, out memoryLength);
            var mem      = result.Memory.Slice(0, memoryLength);

            S7UserDataParameter.TranslateToMemory(datagram.Parameter, mem.Slice(datagram.Header.GetHeaderSize()));
            mem.Span[offset++] = datagram.Data.ReturnCode;
            mem.Span[offset++] = datagram.Data.TransportSize;
            BinaryPrimitives.WriteUInt16BigEndian(mem.Slice(offset, 2).Span, datagram.Data.UserDataLength);
            datagram.Data.Data.CopyTo(mem.Slice(offset + 2, datagram.Data.UserDataLength));
            return(result);
        }
Esempio n. 10
0
        public static S7HeaderDatagram TranslateFromMemory(Memory <byte> data)
        {
            var span   = data.Span;
            var result = new S7HeaderDatagram
            {
                ProtocolId = span[0],
                PduType    = span[1],
                RedundancyIdentification  = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(2, 2)),
                ProtocolDataUnitReference = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(4, 2)),
                ParamLength = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(6, 2)),
                DataLength  = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(8, 2)),
            };

            return(result);
        }
Esempio n. 11
0
        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);
        }
Esempio n. 12
0
        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);
        }
Esempio n. 13
0
        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);
        }
Esempio n. 14
0
        public static S7UserDataDatagram TranslateFromMemory(Memory <byte> data)
        {
            var span   = data.Span;
            var result = new S7UserDataDatagram
            {
                Header = S7HeaderDatagram.TranslateFromMemory(data),
                Data   = new S7UserData()
            };

            result.Parameter = S7UserDataParameter.TranslateFromMemory(data.Slice(result.Header.GetHeaderSize()));
            var offset = result.Header.GetHeaderSize() + result.Parameter.GetParamSize();

            result.Data.ReturnCode     = span[offset++];
            result.Data.TransportSize  = span[offset++];
            result.Data.UserDataLength = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(offset, 2));
            offset          += 2;
            result.Data.Data = new byte[result.Data.UserDataLength];
            data.Slice(offset, result.Data.UserDataLength).CopyTo(result.Data.Data);
            return(result);
        }
Esempio n. 15
0
 public static IMemoryOwner <byte> TranslateToMemory(S7HeaderDatagram datagram, out int memoryLength)
 => TranslateToMemory(datagram, -1, out memoryLength);