Beispiel #1
0
        public AdsErrorCode TryGetSymbol(string symbolPath, bool bLookup, out ITcAdsSymbol symbol)
        {
            if (string.IsNullOrEmpty(symbolPath))
            {
                throw new ArgumentOutOfRangeException("name");
            }
            symbol = null;
            SymbolInfoTable table = this;

            lock (table)
            {
                if (bLookup)
                {
                    TcAdsSymbol symbol2 = null;
                    if (this._symbolTable.TryGetValue(symbolPath, out symbol2))
                    {
                        symbol = symbol2;
                        return(AdsErrorCode.NoError);
                    }
                }
            }
            AdsErrorCode deviceSymbolNotFound = AdsErrorCode.DeviceSymbolNotFound;
            AdsStream    stream = new AdsStream(symbolPath.Length + 1);

            using (AdsBinaryWriter writer = new AdsBinaryWriter(stream))
            {
                writer.WritePlcAnsiString(symbolPath, symbolPath.Length + 1);
                AdsStream rdDataStream = new AdsStream(0xffff);
                int       readBytes    = 0;
                deviceSymbolNotFound = this._adsClient.TryReadWrite(0xf009, 0, rdDataStream, 0, (int)rdDataStream.Length, stream, 0, (int)stream.Length, out readBytes);
                if (deviceSymbolNotFound == AdsErrorCode.NoError)
                {
                    using (AdsBinaryReader reader = new AdsBinaryReader(rdDataStream))
                    {
                        AdsSymbolEntry symbolEntry = new AdsSymbolEntry(-1L, this._encoding, reader);
                        bool           flag2       = true;
                        flag2  = StringComparer.OrdinalIgnoreCase.Compare(symbolPath, symbolEntry.name) == 0;
                        symbol = new TcAdsSymbol(symbolEntry, (TcAdsDataType)this._datatypeTable.ResolveDataType(symbolEntry.type));
                        SymbolInfoTable table2 = this;
                        lock (table2)
                        {
                            this._symbolTable[symbolPath] = (TcAdsSymbol)symbol;
                            if (!flag2 && !this._symbolTable.ContainsKey(symbol.Name))
                            {
                                this._symbolTable[symbol.Name] = (TcAdsSymbol)symbol;
                                string message = $"InstancePath Ambiguity '{symbolPath}' and '{symbol.Name}'!";
                                TwinCAT.Ads.Module.Trace.TraceWarning(message);
                            }
                        }
                    }
                }
            }
            return(deviceSymbolNotFound);
        }
Beispiel #2
0
        private AdsErrorCode updateSymbolHandle(TcAdsSymbol adsSymbol)
        {
            AdsErrorCode noError = AdsErrorCode.NoError;

            if (adsSymbol.IndexGroup != 0xf005L)
            {
                uint num;
                noError = this._adsClient.RawInterface.ReadWrite(0xf003, 0, adsSymbol.Name, false, out num);
                if (noError == AdsErrorCode.NoError)
                {
                    adsSymbol.IndexGroup  = 0xf005L;
                    adsSymbol.IndexOffset = num;
                }
                else if (noError != AdsErrorCode.ClientSyncTimeOut)
                {
                }
            }
            return(noError);
        }
Beispiel #3
0
        public object ReadSymbol(string symbolPath, Type managedType, bool bReloadInfo)
        {
            AdsErrorCode code2;

            if (bReloadInfo)
            {
                this.Cleanup();
            }
            TcAdsSymbol adsSymbol = (TcAdsSymbol)this.GetSymbol(symbolPath, true);

            if (adsSymbol == null)
            {
                TcAdsDllWrapper.ThrowAdsException(AdsErrorCode.DeviceSymbolNotFound);
            }
            TcAdsDataType type = (TcAdsDataType)this._datatypeTable.ResolveDataType(adsSymbol.TypeName);

            if (type == null)
            {
                throw new NotSupportedException("Type of symbol not supported");
            }
            if (adsSymbol.IndexGroup != 0xf005L)
            {
                AdsErrorCode adsErrorCode = this.updateSymbolHandle(adsSymbol);
                if (adsErrorCode == AdsErrorCode.ClientSyncTimeOut)
                {
                    TcAdsDllWrapper.ThrowAdsException(adsErrorCode);
                }
            }
            AdsStream       stream = new AdsStream(adsSymbol.Size);
            bool            flag   = true;
            AdsBinaryReader reader = new AdsBinaryReader(stream);
            int             num2   = 0;

            goto TR_001C;
TR_000F:
            num2++;
TR_001C:
            while (true)
            {
                int num3;
                if (!((num2 < 2) & flag))
                {
                    object        obj2    = null;
                    int           num     = 0;
                    TcAdsDataType adsType = (TcAdsDataType)type.ResolveType(DataTypeResolveStrategy.AliasReference);
                    if (adsType.IsEnum)
                    {
                        num = this.InitializeEnum(adsType.Name, managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2);
                        return(obj2);
                    }
                    if (adsType.IsArray)
                    {
                        num = this.InitializeArray(managedType, adsType, reader, 0, -1, out obj2);
                        return(obj2);
                    }
                    if (adsType.IsStruct)
                    {
                        num = this.InitializeStruct(adsType.SubItems, managedType, reader, 0, out obj2);
                        return(obj2);
                    }
                    if (adsType.IsPointer)
                    {
                        num = this.InitializePointerType(managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2);
                        return(obj2);
                    }
                    if (adsType.IsPrimitive)
                    {
                        num = this.InitializePrimitiveType(adsType.Name, managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2);
                        return(obj2);
                    }
                    if (!adsType.IsPointer)
                    {
                        throw new NotSupportedException("Type of symbol not supported");
                    }
                    num = this.InitializePointerType(managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2);
                    return(obj2);
                }
                flag  = false;
                code2 = this._adsClient.RawInterface.Read((uint)adsSymbol.IndexGroup, (uint)adsSymbol.IndexOffset, 0, (int)stream.Length, stream.GetBuffer(), false, out num3);
                if (code2 > AdsErrorCode.DeviceInvalidOffset)
                {
                    if ((code2 != AdsErrorCode.DeviceSymbolNotFound) && (code2 != AdsErrorCode.DeviceSymbolVersionInvalid))
                    {
                        break;
                    }
                }
                else if (code2 == AdsErrorCode.NoError)
                {
                    goto TR_000F;
                }
                else if (code2 != AdsErrorCode.DeviceInvalidOffset)
                {
                    break;
                }
                if (adsSymbol.IndexGroup == 0xf005L)
                {
                    uint num4;
                    this._adsClient.RawInterface.Write(0xf006, 0, (uint)adsSymbol.IndexOffset, false);
                    if (this._adsClient.RawInterface.ReadWrite(0xf003, 0, symbolPath, false, out num4) == AdsErrorCode.NoError)
                    {
                        adsSymbol.IndexGroup  = 0xf005L;
                        adsSymbol.IndexOffset = num4;
                        flag = true;
                    }
                }
                goto TR_000F;
            }
            TcAdsDllWrapper.ThrowAdsException(code2);
            goto TR_000F;
        }
Beispiel #4
0
        public void WriteSymbol(string name, object value, bool bReloadInfo)
        {
            AdsErrorCode code2;

            if (bReloadInfo)
            {
                this.Cleanup();
            }
            TcAdsSymbol adsSymbol = (TcAdsSymbol)this.GetSymbol(name, true);

            if (adsSymbol == null)
            {
                TcAdsDllWrapper.ThrowAdsException(AdsErrorCode.DeviceSymbolNotFound);
            }
            TcAdsDataType type = (TcAdsDataType)((TcAdsDataType)this._datatypeTable.ResolveDataType(adsSymbol.TypeName)).ResolveType(DataTypeResolveStrategy.AliasReference);

            if (type == null)
            {
                throw new NotSupportedException("Type of symbol not supported");
            }
            if (adsSymbol.IndexGroup != 0xf005L)
            {
                AdsErrorCode adsErrorCode = this.updateSymbolHandle(adsSymbol);
                if (adsErrorCode == AdsErrorCode.ClientSyncTimeOut)
                {
                    TcAdsDllWrapper.ThrowAdsException(adsErrorCode);
                }
            }
            AdsStream       stream = new AdsStream(adsSymbol.Size);
            AdsBinaryWriter writer = new AdsBinaryWriter(stream);

            if (type.IsEnum)
            {
                this.WriteEnumValue(adsSymbol.Name, value, type, writer, 0);
            }
            else if (type.IsArray)
            {
                this.WriteArray(value, type, writer, 0);
            }
            else if (type.IsStruct)
            {
                this.WriteStruct(value, type.SubItems, writer, 0);
            }
            else if (type.IsPointer)
            {
                this.WritePointerValue(adsSymbol.Name, value, adsSymbol.DataTypeId, adsSymbol.Size, writer, 0);
            }
            else
            {
                if (!type.IsPrimitive)
                {
                    throw new NotSupportedException("Type of symbol not supported");
                }
                this.WritePrimitiveValue(adsSymbol.Name, value, type.ManagedType, adsSymbol.DataTypeId, adsSymbol.Size, writer, 0);
            }
            bool flag = true;
            int  num  = 0;

            goto TR_000F;
TR_0002:
            num++;
TR_000F:
            while (true)
            {
                if (!((num < 2) & flag))
                {
                    return;
                }
                flag  = false;
                code2 = this._adsClient.RawInterface.Write((uint)adsSymbol.IndexGroup, (uint)adsSymbol.IndexOffset, 0, (int)stream.Length, stream.GetBuffer(), false);
                if (code2 > AdsErrorCode.DeviceInvalidOffset)
                {
                    if ((code2 != AdsErrorCode.DeviceSymbolNotFound) && (code2 != AdsErrorCode.DeviceSymbolVersionInvalid))
                    {
                        break;
                    }
                }
                else if (code2 == AdsErrorCode.NoError)
                {
                    goto TR_0002;
                }
                else if (code2 != AdsErrorCode.DeviceInvalidOffset)
                {
                    break;
                }
                if (adsSymbol.IndexGroup == 0xf005L)
                {
                    uint num2;
                    this._adsClient.RawInterface.Write(0xf006, 0, (uint)adsSymbol.IndexOffset, false);
                    if (this._adsClient.RawInterface.ReadWrite(0xf003, 0, name, false, out num2) == AdsErrorCode.NoError)
                    {
                        adsSymbol.IndexGroup  = 0xf005L;
                        adsSymbol.IndexOffset = num2;
                        flag = true;
                    }
                }
                goto TR_0002;
            }
            TcAdsDllWrapper.ThrowAdsException(code2);
            goto TR_0002;
        }