Exemple #1
0
        /// <summary>
        /// ADS Read State - Request<br/>
        /// Reads the ADS status and the device status of an ADS device.
        /// </summary>
        /// <param name="amsHeader"></param>
        /// <param name="binaryReader"></param>
        /// <exception cref="FormatException"/>
        internal AdsReadStateRequest(AmsHeader amsHeader, BinaryReader binaryReader)
        {
            if (!amsHeader.IsValid)
            {
                throw new FormatException("Parsed AmsHeader is not valid!");
            }

            if (amsHeader.Data_Length != EXPECTED_DATA_LEN)
            {
                throw new FormatException($"Parsed AmsHeader is not valid, Data_Length={amsHeader.Data_Length} not allowed.");
            }
            _PacketData = binaryReader.ReadBytes((int)amsHeader.Data_Length);
        }
Exemple #2
0
        /// <summary>
        /// ADS Write Control - Response<br/>
        /// Changes the ADS status and the device status of an ADS device. Additionally it is possible to send data to the ADS device to transfer further information.<br/>
        /// These data were not analysed from the current ADS devices (PLC, NC, ...)
        /// </summary>
        /// <param name="amsHeader"></param>
        /// <param name="binaryReader"></param>
        /// <exception cref="FormatException"/>
        internal AdsDeleteDeviceNotificationResponse(AmsHeader amsHeader, BinaryReader binaryReader)
        {
            if (!amsHeader.IsValid)
            {
                throw new FormatException("Parsed AmsHeader is not valid!");
            }

            if (amsHeader.Data_Length != EXPECTED_DATA_LEN)
            {
                throw new FormatException($"Parsed AmsHeader is not valid, Data_Length={amsHeader.Data_Length} not allowed.");
            }
            _PacketData = binaryReader.ReadBytes((int)amsHeader.Data_Length);

            ParsePacketData();
        }
Exemple #3
0
        /// <summary>
        /// ADS Write - Response<br/>
        /// With ADS Write data can be written to an ADS device. The data are addressed by the Index Group and the Index Offset
        /// </summary>
        /// <param name="amsHeader"></param>
        /// <param name="binaryReader"></param>
        /// <exception cref="FormatException"/>
        internal AdsWriteResponse(AmsHeader amsHeader, BinaryReader binaryReader)
        {
            if (!amsHeader.IsValid)
            {
                throw new FormatException("Parsed AmsHeader is not valid!");
            }

            // sometimes, Data_Length is larger then 4...
            if (amsHeader.Data_Length < EXPECTED_DATA_LEN)
            {
                throw new FormatException($"Parsed AmsHeader is not valid, Data_Length={amsHeader.Data_Length} not allowed.");
            }
            _PacketData = binaryReader.ReadBytes((int)amsHeader.Data_Length);

            ParsePacketData();
        }
        internal static IPayload GetAdsCommand(AmsHeader amsHeader, BinaryReader binaryReader)
        {
            UInt16 cId = BuildReqResFromAmsHeader(amsHeader);

            if (AdsCommandLookUp.TryGetValue(cId, out var adsCommandType))
            {
                var constructorInfo = adsCommandType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null,
                                                                    new Type[] { typeof(AmsHeader), typeof(BinaryReader) }, null);
                if (constructorInfo is not null)
                {
                    try
                    {
                        return((IPayload)(constructorInfo.Invoke(new object[] { amsHeader, binaryReader })));
                    }
                    catch (Exception ex)
                    {
                        throw ex.InnerException ?? ex;
                    }
                }
            }
            throw new Exception("Fatal Error, no valid constructor found.");
        }
 private static ushort BuildReqResFromAmsHeader(AmsHeader amsHeader)
 {
     return(amsHeader.IsResponse ? BuildResponseOfCommand(amsHeader.AmsCommandId) : BuildRequestOfCommand(amsHeader.AmsCommandId));
 }