public static S7UserDataDatagram BuildAlarmUpdateRequest(SiemensPlcProtocolContext context, ushort id, bool activate = true)
        {
            var data   = new byte[] { 0x86, 0x00, 0x61, 0x73, 0x6d, 0x65, 0x73, 0x73, 0x00, 0x00, activate ? (byte)0x09 : (byte)0x08, 0x00 };
            var result = new S7UserDataDatagram
            {
                Parameter = new S7UserDataParameter
                {
                    ParamDataLength = 4,
                    TypeAndGroup    = ((byte)UserDataFunctionType.Request << 4) | (byte)UserDataFunctionGroup.Cpu,
                    SubFunction     = (byte)UserDataSubFunctionCpu.Msgs,
                    SequenceNumber  = 0,
                    ParameterType   = (byte)UserDataParamTypeType.Request
                },
                Data = new S7UserData
                {
                    Data           = data,
                    UserDataLength = (ushort)data.Length,
                    ReturnCode     = data.Length > 0 ? (byte)ItemResponseRetValue.Success : (byte)ItemResponseRetValue.DataError,
                    TransportSize  = data.Length > 0 ? (byte)DataTransportSize.OctetString : (byte)DataTransportSize.Null
                }
            };

            result.Header.ProtocolDataUnitReference = id;
            result.Header.DataLength  = 16;
            result.Header.ParamLength = 8;

            return(result);
        }
        public static S7UserDataDatagram BuildBlockInfoRequest(SiemensPlcProtocolContext context, int id, PlcBlockType blockType, int blockNumber)
        {
            var result = new S7UserDataDatagram
            {
                Parameter = new S7UserDataParameter
                {
                    ParamDataLength = 4,
                    TypeAndGroup    = ((byte)UserDataFunctionType.Request << 4) | (byte)UserDataFunctionGroup.Block,
                    SubFunction     = (byte)UserDataSubFunctionBlock.BlockInfo,
                    SequenceNumber  = 0,
                    ParameterType   = (byte)UserDataParamTypeType.Request
                },
                Data = new S7UserData
                {
                    ReturnCode    = (byte)ItemResponseRetValue.Success,
                    TransportSize = (byte)DataTransportSize.OctetString,
                }
            };

            result.Header.ProtocolDataUnitReference = (ushort)id;
            result.Header.DataLength  = 12;
            result.Header.ParamLength = 8;

            result.Data.Data = new byte[] { 0x30, (byte)blockType, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41 };
            Encoding.Default.GetBytes(string.Format(CultureInfo.InvariantCulture, "{0:00000}", blockNumber)).AsSpan().CopyTo(result.Data.Data.Span.Slice(2, 5));
            result.Data.UserDataLength = (ushort)result.Data.Data.Length;

            return(result);
        }
        public static S7UserDataDatagram BuildPendingAlarmRequest(SiemensPlcProtocolContext context, ushort id, byte sequenceNumber)
        {
            var data   = sequenceNumber == 0 ? new byte[] { 0x00, 0x01, 0x12, 0x08, 0x1a, 0x00, 0x01, 0x34, 0x00, 0x00, 0x00, 0x04 } : Array.Empty <byte>();;
            var result = new S7UserDataDatagram
            {
                Parameter = new S7UserDataParameter
                {
                    ParamDataLength = sequenceNumber == 0 ? (byte)4 : (byte)8,
                    TypeAndGroup    = ((byte)UserDataFunctionType.Request << 4) | (byte)UserDataFunctionGroup.Cpu,
                    SubFunction     = (byte)UserDataSubFunctionCpu.AlarmInit,
                    SequenceNumber  = sequenceNumber,
                    ParameterType   = sequenceNumber == 0 ? (byte)UserDataParamTypeType.Request : (byte)UserDataParamTypeType.Response
                },
                Data = new S7UserData
                {
                    Data           = data,
                    UserDataLength = (ushort)data.Length,
                    ReturnCode     = data.Length > 0 ? (byte)ItemResponseRetValue.Success : (byte)ItemResponseRetValue.DataError,
                    TransportSize  = data.Length > 0 ? (byte)DataTransportSize.OctetString : (byte)DataTransportSize.Null
                }
            };

            result.Header.ProtocolDataUnitReference = id;
            result.Header.DataLength  = sequenceNumber == 0 ? (ushort)16 : (ushort)4;
            result.Header.ParamLength = sequenceNumber == 0 ? (ushort)8 : (ushort)12;

            return(result);
        }
Exemple #4
0
        public static S7UserDataDatagram BuildBlocksOfTypeRequest(SiemensPlcProtocolContext context, int id, PlcBlockType type, byte sequenceNumber)
        {
            var result = new S7UserDataDatagram
            {
                Parameter = new S7UserDataParameter
                {
                    ParamDataLength = sequenceNumber == 0x00 ? (byte)4 : (byte)8,
                    TypeAndGroup    = ((byte)UserDataFunctionType.Request << 4) | (byte)UserDataFunctionGroup.Block,
                    SubFunction     = (byte)UserDataSubFunctionBlock.ListType,
                    SequenceNumber  = sequenceNumber,
                    ParameterType   = sequenceNumber == 0x00 ? (byte)UserDataParamTypeType.Request : (byte)UserDataParamTypeType.Response
                },
                Data = new S7UserData
                {
                    ReturnCode    = (byte)ItemResponseRetValue.DataError,
                    TransportSize = (byte)DataTransportSize.Null
                }
            };

            result.Header.ProtocolDataUnitReference = (ushort)id;
            result.Header.DataLength  = sequenceNumber == 0x00 ? (ushort)6 : (ushort)4;
            result.Header.ParamLength = sequenceNumber == 0x00 ? (ushort)8 : (ushort)12;

            result.Data.Data           = sequenceNumber == 0 ? new byte[] { 0x30, (byte)type } : Array.Empty <byte>();
            result.Data.UserDataLength = (ushort)result.Data.Data.Length;

            return(result);
        }
Exemple #5
0
        public static S7UserDataDatagram BuildBlocksCountRequest(SiemensPlcProtocolContext context, int id)
        {
            var result = new S7UserDataDatagram
            {
                Parameter = new S7UserDataParameter
                {
                    ParamDataLength = 4,
                    TypeAndGroup    = ((byte)UserDataFunctionType.Request << 4) | (byte)UserDataFunctionGroup.Block,
                    SubFunction     = (byte)UserDataSubFunctionBlock.List,
                    SequenceNumber  = 0,
                    ParameterType   = (byte)UserDataParamTypeType.Request
                },
                Data = new S7UserData
                {
                    ReturnCode     = (byte)ItemResponseRetValue.DataError,
                    TransportSize  = (byte)DataTransportSize.Null,
                    Data           = Array.Empty <byte>(),
                    UserDataLength = 0
                }
            };

            result.Header.ProtocolDataUnitReference = (ushort)id;
            result.Header.DataLength  = 4;
            result.Header.ParamLength = 8;

            return(result);
        }
        public static S7PendingAlarmAckDatagram TranslateFromMemory(Memory <byte> data)
        {
            var current = new S7PendingAlarmAckDatagram
            {
                UserData = S7UserDataDatagram.TranslateFromMemory(data),
            };

            return(current);
        }
        public static S7AlarmIndicationDatagram TranslateFromMemory(Memory <byte> data)
        {
            var current = new S7AlarmIndicationDatagram
            {
                UserData = S7UserDataDatagram.TranslateFromMemory(data),
            };

            current.AlarmMessage = S7AlarmMessage.TranslateFromMemory(current.UserData.Data.Data, (AlarmMessageType)current.UserData.Parameter.SubFunction);

            return(current);
        }
        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);
        }
Exemple #9
0
        //public static Memory<byte> TranslateToMemory(S7PlcBlockInfoAckDatagram datagram)
        //{
        //    var result = S7UserDataDatagram.TranslateToMemory(datagram.UserData);

        //    S7UserDataParameter.TranslateToMemory(datagram.Parameter, result.Slice(datagram.Header.GetHeaderSize()));
        //    result.Span[offset++] = datagram.Data.ReturnCode;
        //    result.Span[offset++] = datagram.Data.TransportSize;
        //    BinaryPrimitives.WriteUInt16BigEndian(result.Slice(offset, 2).Span, datagram.Data.UserDataLength);
        //    datagram.Data.Data.CopyTo(result.Slice(offset + 2, datagram.Data.UserDataLength));
        //    return result;
        //}

        public static S7PlcBlockInfoAckDatagram TranslateFromMemory(Memory <byte> data)
        {
            var result = new S7PlcBlockInfoAckDatagram
            {
                UserData = S7UserDataDatagram.TranslateFromMemory(data)
            };

            if (result.UserData.Parameter.ParamErrorCode == 0)
            {
                if (result.UserData.Data.ReturnCode == 0xff)
                {
                    var offset = 0;
                    var span   = result.UserData.Data.Data.Span;
                    result.BlockType           = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(offset, 2)); offset += 2;
                    result.LengthOfInfo        = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(offset, 2)); offset += 2;
                    result.Unknown1            = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(offset, 2)); offset += 2;
                    result.Const1              = span[offset++];
                    result.Const2              = span[offset++];
                    result.Unknown2            = span[offset++];
                    result.BlockFlags          = (PlcBlockAttributes)span[offset++];
                    result.BlockLanguage       = (PlcBlockLanguage)span[offset++];
                    result.SubBlockType        = (PlcSubBlockType)span[offset++];
                    result.BlockNumber         = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(offset, 2)); offset += 2;
                    result.LengthLoadMemory    = BinaryPrimitives.ReadUInt32BigEndian(span.Slice(offset, 4)); offset += 4;
                    result.BlockSecurity       = BinaryPrimitives.ReadUInt32BigEndian(span.Slice(offset, 4)); offset += 4; // ????
                    result.LastCodeChange      = GetDt(span.Slice(offset, 6)); offset += 6;
                    result.LastInterfaceChange = GetDt(span.Slice(offset, 6)); offset += 6;
                    result.SSBLength           = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(offset, 2)); offset += 2;
                    result.ADDLength           = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(offset, 2)); offset += 2;
                    result.LocalDataSize       = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(offset, 2)); offset += 2;
                    result.CodeSize            = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(offset, 2)); offset += 2;
                    result.Author              = Encoding.ASCII.GetString(span.Slice(offset, 8).ToArray()); offset += 8;
                    result.Family              = Encoding.ASCII.GetString(span.Slice(offset, 8).ToArray()); offset += 8;
                    result.Name = Encoding.ASCII.GetString(span.Slice(offset, 8).ToArray()); offset += 8;
                    var version = span[offset++];
                    result.VersionHeaderMajor = (version & 0xF0) >> 4;
                    result.VersionHeaderMinor = (version & 0x0F);
                    result.Unknown2           = span[offset++];
                    result.Checksum           = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(offset, 2)); offset += 2;
                    result.Reserved1          = BinaryPrimitives.ReadUInt32BigEndian(span.Slice(offset, 4)); offset += 4;
                    result.Reserved2          = BinaryPrimitives.ReadUInt32BigEndian(span.Slice(offset, 4));
                }
            }
            return(result);
        }
Exemple #10
0
        public static S7AlarmUpdateAckDatagram TranslateFromMemory(Memory <byte> data)
        {
            var span    = data.Span;
            var current = new S7AlarmUpdateAckDatagram
            {
                UserData = S7UserDataDatagram.TranslateFromMemory(data),
            };
            var offset = 0;

            current.SubscribedEvents = span[offset++];
            current.Unknown          = span[offset++];
            data.Slice(offset, current.Username.Length).CopyTo(current.Username);
            offset           += current.Username.Length;
            current.AlarmType = span[offset++];
            current.FillByte  = span[offset++];

            return(current);
        }
        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);
        }