Пример #1
0
        internal static SymbolUploadInfo readSymbolUploadInfo(IAdsConnection connection)
        {
            SymbolUploadInfo uploadInfo   = null;
            AdsErrorCode     adsErrorCode = AdsSymbolLoader.loadUploadInfo(connection, TimeSpan.FromMilliseconds((double)connection.Timeout), out uploadInfo);

            if (adsErrorCode != AdsErrorCode.NoError)
            {
                AdsErrorException ex = AdsErrorException.Create(adsErrorCode);
                Module.Trace.TraceWarning("Could not load Symbol Upload info", ex);
                uploadInfo = new SymbolUploadInfo();
            }
            return(uploadInfo);
        }
Пример #2
0
        public ITcAdsSymbol GetSymbol(string symbolPath, bool bLookup)
        {
            if (string.IsNullOrEmpty(symbolPath))
            {
                throw new ArgumentOutOfRangeException("name");
            }
            ITcAdsSymbol symbol       = null;
            AdsErrorCode adsErrorCode = this.TryGetSymbol(symbolPath, bLookup, out symbol);

            if ((adsErrorCode != AdsErrorCode.NoError) && (adsErrorCode != AdsErrorCode.DeviceSymbolNotFound))
            {
                throw AdsErrorException.Create(adsErrorCode);
            }
            return(symbol);
        }
Пример #3
0
        public void StartListening()
        {
            List <Tag> tags       = _notificationHandlesByTag.Select(t => t.Item1).ToList();
            string     failedTags = string.Empty;

            try
            {
                _twinCatClient.Connect(_adsAddress, _adsPort);
                _twinCatClient.Timeout = Timeout;
            }
            catch (AdsErrorException)
            {
                throw new PlcCommunicationException("Can't start listening. Connection to PLC failed.",
                                                    _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture));
            }

            AdsErrorException lastInnerException = null;

            _notificationHandlesByTag.Clear();
            foreach (Tag tag in tags)
            {
                try
                {
                    int handle = _twinCatClient.AddDeviceNotification(tag.FullName(), new AdsStream(tag.BitSize == 0 ? 81 : tag.BitSize / 8), AdsTransMode.OnChange, (int)RefreshRate, 0, tag);
                    _notificationHandlesByTag.Add(new Tuple <Tag, int>(tag, handle));
                }
                catch (AdsErrorException e)
                {
                    failedTags        += tag.FullName() + "\n";
                    lastInnerException = e;
                }
            }

            if (!string.IsNullOrEmpty(failedTags))
            {
                throw new PlcCommunicationException("Can't start listening. Some tags were not found:\n" + failedTags,
                                                    _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture), failedTags, lastInnerException);
            }
        }
Пример #4
0
        public object ReadValue(ISymbol symbol, out DateTime utcReadTime)
        {
            object       obj2 = null;
            AdsErrorCode code = (AdsErrorCode)this.TryReadValue(symbol, out obj2, out utcReadTime);

            if (code == AdsErrorCode.NoError)
            {
                return(obj2);
            }
            SymbolException ex = null;

            ex = new SymbolException($"Could not read Symbol '{symbol}'! Error: {code}", symbol, AdsErrorException.Create(code));
            Module.Trace.TraceError(ex);
            throw ex;
        }
Пример #5
0
        internal void WritePrimitiveValue(string symbolPath, object value, Type managedType, AdsDatatypeId dataType, int byteSize, AdsBinaryWriter writer, int writerOffset)
        {
            if (string.IsNullOrEmpty(symbolPath))
            {
                throw new ArgumentNullException("symbolPath");
            }
            if (managedType == null)
            {
                throw new ArgumentNullException("managedType");
            }
            if (byteSize < 0)
            {
                throw new ArgumentOutOfRangeException("byteSize");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writerOffset < 0)
            {
                throw new ArgumentOutOfRangeException("writerOffset");
            }
            Type type  = value.GetType();
            bool flag2 = PrimitiveTypeConverter.IsPlcOpenType(value.GetType());

            if (PrimitiveTypeConverter.IsPlcOpenType(managedType))
            {
                Type tp = null;
                if (PrimitiveTypeConverter.TryGetManagedType(dataType, out tp))
                {
                    managedType = tp;
                }
                else if (managedType == typeof(LTIME))
                {
                    managedType = typeof(ulong);
                    dataType    = AdsDatatypeId.ADST_UINT64;
                }
            }
            if (managedType != type)
            {
                try
                {
                    object obj1 = PrimitiveTypeConverter.Convert(value, managedType);
                    value = obj1;
                }
                catch (MarshalException exception)
                {
                    throw new ArgumentException($"Cannot convert value type '{value.GetType()}' to symbol type '{managedType}'!", "value", exception);
                }
            }
            writer.BaseStream.Position = writerOffset;
            switch (dataType)
            {
            case AdsDatatypeId.ADST_INT16:
                writer.Write((short)value);
                return;

            case AdsDatatypeId.ADST_INT32:
                writer.Write((int)value);
                return;

            case AdsDatatypeId.ADST_REAL32:
                writer.Write((float)value);
                return;

            case AdsDatatypeId.ADST_REAL64:
                writer.Write((double)value);
                return;
            }
            switch (dataType)
            {
            case AdsDatatypeId.ADST_INT8:
                writer.Write((sbyte)value);
                return;

            case AdsDatatypeId.ADST_UINT8:
                writer.Write((byte)value);
                return;

            case AdsDatatypeId.ADST_UINT16:
                writer.Write((ushort)value);
                return;

            case AdsDatatypeId.ADST_UINT32:
                writer.Write((uint)value);
                return;

            case AdsDatatypeId.ADST_INT64:
                writer.Write((long)value);
                return;

            case AdsDatatypeId.ADST_UINT64:
                writer.Write((ulong)value);
                return;
            }
            switch (dataType)
            {
            case AdsDatatypeId.ADST_STRING:
            {
                int  byteCount = Encoding.Default.GetByteCount("a");
                long position  = writer.BaseStream.Position;
                int  length    = ((string)value).Length;
                if (((length + 1) * byteCount) > byteSize)
                {
                    throw AdsErrorException.Create(AdsErrorCode.DeviceInvalidSize);
                }
                writer.WritePlcAnsiString((string)value, length);
                writer.BaseStream.Position = position + byteSize;
                return;
            }

            case AdsDatatypeId.ADST_WSTRING:
            {
                int  byteCount = Encoding.Unicode.GetByteCount("a");
                long position  = writer.BaseStream.Position;
                int  length    = ((string)value).Length;
                if (((length + 1) * byteCount) > byteSize)
                {
                    throw AdsErrorException.Create(AdsErrorCode.DeviceInvalidSize);
                }
                writer.WritePlcUnicodeString((string)value, length);
                writer.BaseStream.Position = position + byteSize;
                return;
            }

            case AdsDatatypeId.ADST_BIT:
                if ((bool)value)
                {
                    writer.Write((byte)1);
                    return;
                }
                writer.Write((byte)0);
                return;
            }
            throw new ArgumentException("Unexpected datatype. Cannot convert datatype of symbol to this type.", "type");
        }
Пример #6
0
        public override void WriteValue(ISymbol symbol, object value, out DateTime utcWriteTime)
        {
            if (symbol == null)
            {
                throw new ArgumentNullException("symbol");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            int             num      = 0;
            ISymbol         symbol2  = symbol;
            IDataType       dataType = symbol2.DataType;
            IResolvableType type2    = dataType as IResolvableType;

            if (type2 != null)
            {
                dataType = type2.ResolveType(DataTypeResolveStrategy.AliasReference);
            }
            utcWriteTime = DateTime.MinValue;
            if (dataType.IsBitType)
            {
                num = this.tryWriteValue(symbol2, BitTypeConverter.Marshal(dataType.BitSize, value), 0, out utcWriteTime);
            }
            else if (!symbol2.IsPrimitiveType)
            {
                DynamicValue value2 = (DynamicValue)value;
                num = this.TryWriteValue(value2, out utcWriteTime);
            }
            else if (((dataType.Category == DataTypeCategory.Primitive) || (dataType.Category == DataTypeCategory.SubRange)) || (dataType.Category == DataTypeCategory.Pointer))
            {
                num = this.tryWriteValue(symbol2, PrimitiveTypeConverter.Default.Marshal(symbol2.DataType, value), 0, out utcWriteTime);
            }
            else if (dataType.Category == DataTypeCategory.String)
            {
                IStringType type3   = (IStringType)symbol2.DataType;
                byte[]      buffer3 = new PrimitiveTypeConverter(type3.Encoding).Marshal(symbol2.DataType, value);
                if (buffer3.Length >= type3.Size)
                {
                    Exception ex = new AdsErrorException($"String is too large for symbol '{symbol2.InstancePath}' (Type: {symbol2.TypeName}).", AdsErrorCode.DeviceInvalidSize);
                    Module.Trace.TraceError(ex);
                    throw ex;
                }
                num = this.tryWriteValue(symbol2, buffer3, 0, out utcWriteTime);
            }
            else if (dataType.Category == DataTypeCategory.Array)
            {
                IArrayType type4 = (IArrayType)symbol2.DataType;
                num = this.tryWriteValue(symbol2, PrimitiveTypeConverter.Default.Marshal(symbol2.DataType, value), 0, out utcWriteTime);
            }
            else
            {
                if (dataType.Category != DataTypeCategory.Enum)
                {
                    Exception ex = new SymbolException($"Could not write Symbol '{symbol2.InstancePath}' (Type: {symbol2.TypeName}). Category mismatch!", symbol2);
                    Module.Trace.TraceError(ex);
                    throw ex;
                }
                IEnumType type5 = (IEnumType)symbol2.DataType;
                num = this.tryWriteValue(symbol2, PrimitiveTypeConverter.Default.Marshal(symbol2.DataType, value), 0, out utcWriteTime);
            }
            if (num != 0)
            {
                Exception ex = new SymbolException($"Could not write Symbol '{symbol2.InstancePath}' (Type: {symbol2.TypeName})! Error: {num}", symbol2);
                Module.Trace.TraceError(ex);
                throw ex;
            }
        }