コード例 #1
0
            internal TcAdsSymbolInfo CreateReferenceSymbol(TcAdsSymbolInfo parent, int subIndex)
            {
                if (parent == null)
                {
                    throw new ArgumentNullException("parent");
                }
                TcAdsSymbolInfo subSymbol = null;

                if (subIndex == 0)
                {
                    TcAdsDataType dataType       = (TcAdsDataType)parent.DataType;
                    string        referencedType = string.Empty;
                    if (DataTypeStringParser.TryParseReference(parent.DataType.Name, out referencedType))
                    {
                        TcAdsDataType typeByName = (TcAdsDataType)this._parser.GetTypeByName(referencedType);
                        if (typeByName != null)
                        {
                            subSymbol = new TcAdsSymbolInfo(this._parser, parent, subIndex, typeByName);
                            this.calcFieldAddress(parent, subSymbol, null, out subSymbol.indexGroup, out subSymbol.indexOffset);
                        }
                        else
                        {
                            object[] args = new object[] { referencedType };
                            Module.Trace.TraceWarning("Cannot create Reference Symbol. Dereferenced Type '{0}' not found!", args);
                        }
                    }
                }
                return(subSymbol);
            }
コード例 #2
0
            private void calcArrayElementIndexGroupIndexOffset(int subIndex, TcAdsSymbolInfo arrayInstance, int elementSize, out uint indexGroup, out uint indexOffset)
            {
                TcAdsDataType dataType  = (TcAdsDataType)arrayInstance.DataType;
                bool          isBitType = arrayInstance.IsBitType;

                if ((dataType.Flags & (AdsDataTypeFlags.None | AdsDataTypeFlags.PropItem)) == (AdsDataTypeFlags.None | AdsDataTypeFlags.PropItem))
                {
                    indexGroup  = 0xf017;
                    indexOffset = dataType.TypeHashValue;
                }
                else if (isBitType)
                {
                    this.calcBitAccessAddress(arrayInstance, 0, out indexGroup, out indexOffset);
                }
                else if (arrayInstance.IsReference || arrayInstance.IsDereferencedReference)
                {
                    indexGroup  = 0xf016;
                    indexOffset = 0;
                }
                else
                {
                    indexGroup  = (uint)arrayInstance.IndexGroup;
                    indexOffset = ((uint)arrayInstance.IndexOffset) + ((uint)(elementSize * subIndex));
                }
            }
コード例 #3
0
 internal TcAdsSymbolInfo CreatePointerSymbol(TcAdsSymbolInfo parent, TcAdsDataType referencedType)
 {
     if (parent == null)
     {
         throw new ArgumentNullException("parent");
     }
     if (referencedType == null)
     {
         throw new ArgumentNullException("referencedType");
     }
     return(new TcAdsSymbolInfo(this._parser, parent, 0)
     {
         indexGroup = !referencedType.IsBitType ? 0xf014 : 0xf01a,
         indexOffset = 0,
         size = (uint)referencedType.Size,
         typeName = referencedType.Name,
         dataTypeId = referencedType.DataTypeId,
         comment = string.Empty,
         instancePath = parent.instancePath + "^",
         shortName = parent.shortName + "^",
         typeEntryFlags = parent.typeEntryFlags,
         flags = parent.flags,
         arrayInfo = parent.arrayInfo
     });
 }
コード例 #4
0
 internal TcAdsSymbol(AdsSymbolEntry symbolEntry, TcAdsDataType typeEntry)
 {
     if (symbolEntry == null)
     {
         throw new ArgumentNullException("symbolEntry");
     }
     if (typeEntry == null)
     {
         object[] args = new object[] { symbolEntry.name };
         Module.Trace.TraceWarning("No data type found for AdsSymbolEntry '{0}'", args);
     }
     this._indexGroup      = symbolEntry.indexGroup;
     this._indexOffset     = symbolEntry.indexOffset;
     this._size            = symbolEntry.size;
     this._typeId          = (AdsDatatypeId)symbolEntry.dataType;
     this._typeEntryFlags  = typeEntry.Flags;
     this._flags           = symbolEntry.flags;
     this._name            = symbolEntry.name;
     this._typeName        = symbolEntry.type;
     this._comment         = symbolEntry.comment;
     this._arrayDimensions = symbolEntry.arrayDim;
     this._arrayInfos      = symbolEntry.array;
     this._attributeCount  = symbolEntry.attributeCount;
     this._attributes      = symbolEntry.attributes;
     this._dataType        = typeEntry;
 }
コード例 #5
0
        public IDataType ResolveType(DataTypeResolveStrategy type)
        {
            TcAdsDataType baseType = this;
            TcAdsDataType type3    = this;

            if (type == DataTypeResolveStrategy.Alias)
            {
                while (true)
                {
                    if ((baseType == null) || (baseType.Category != DataTypeCategory.Alias))
                    {
                        if ((baseType == null) && (this.Category == DataTypeCategory.Alias))
                        {
                            object[] args = new object[] { type3.Name };
                            Module.Trace.TraceWarning("Cannot resolve type '{0}", args);
                        }
                        break;
                    }
                    type3    = baseType;
                    baseType = (TcAdsDataType)baseType.BaseType;
                }
            }
            else if (type == DataTypeResolveStrategy.AliasReference)
            {
                while (true)
                {
                    if ((baseType == null) || ((baseType.Category != DataTypeCategory.Alias) && (baseType.Category != DataTypeCategory.Reference)))
                    {
                        if ((baseType == null) && ((this.Category == DataTypeCategory.Alias) || (this.Category == DataTypeCategory.Reference)))
                        {
                            object[] args = new object[] { type3.Name };
                            Module.Trace.TraceWarning("Cannot resolve type '{0}", args);
                        }
                        break;
                    }
                    if (baseType.Category == DataTypeCategory.Alias)
                    {
                        type3    = baseType;
                        baseType = (TcAdsDataType)baseType.BaseType;
                    }
                    else
                    {
                        string str;
                        if (!DataTypeStringParser.TryParseReference(baseType.Name, out str))
                        {
                            type3    = baseType;
                            baseType = null;
                        }
                        else
                        {
                            type3 = baseType;
                            IDataType type4 = null;
                            this._table.TryResolveType(str, out type4);
                            baseType = (TcAdsDataType)type4;
                        }
                    }
                }
            }
            return(baseType);
        }
コード例 #6
0
        internal TcAdsSymbolInfo GetSubSymbol(TcAdsSymbolInfo parent, int subIndex, bool dereference)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }
            TcAdsSymbolInfo info = null;

            try
            {
                ITcAdsDataType dataType = parent.DataType;
                ITcAdsDataType type2    = null;
                if (dataType != null)
                {
                    type2 = (ITcAdsDataType)dataType.ResolveType(DataTypeResolveStrategy.AliasReference);
                }
                if (type2 != null)
                {
                    int count = parent.SubSymbols.Count;
                    if (((type2 != null) && (type2.SubItems.Count > 0)) && (subIndex < type2.SubItems.Count))
                    {
                        info = this._subSymbolFactory.CreateSubSymbol(parent, subIndex);
                    }
                    else if ((type2 != null) && ((type2.Dimensions.Count > 0) || DataTypeStringParser.IsArray(type2.Name)))
                    {
                        info = this._subSymbolFactory.CreateArrayElement(parent, (TcAdsDataType)type2, subIndex);
                    }
                    else if ((type2 != null) && type2.IsReference)
                    {
                        this.SetPlatformPointerSize(type2.Size);
                    }
                    else if ((subIndex == 0) && type2.IsPointer)
                    {
                        this.SetPlatformPointerSize(type2.Size);
                        if (dereference)
                        {
                            string str;
                            DataTypeStringParser.TryParsePointer(type2.Name, out str);
                            TcAdsDataType referencedType = (TcAdsDataType)this.ResolveDataType(str);
                            if ((referencedType != null) & dereference)
                            {
                                bool flag = (referencedType.Flags & AdsDataTypeFlags.AnySizeArray) == AdsDataTypeFlags.AnySizeArray;
                                if ((referencedType.Size > 0) | flag)
                                {
                                    info = this._subSymbolFactory.CreatePointerSymbol(parent, referencedType);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Module.Trace.TraceError($"ParentSymbol: {parent.Name}", exception);
                throw;
            }
            return(info);
        }
コード例 #7
0
        internal void WriteEnumValue(string symbolPath, object value, TcAdsDataType type, AdsBinaryWriter writer, int writerOffset)
        {
            object obj2 = value;

            if (value is string)
            {
                obj2 = EnumValueConverter.ToValue(type, (string)value);
            }
            this.WritePrimitiveValue(symbolPath, obj2, type.ManagedType, type.DataTypeId, type.Size, writer, writerOffset);
        }
コード例 #8
0
 internal void AlignSubItemToType(TcAdsDataType subEntryType)
 {
     if (base._dataTypeId != subEntryType.DataTypeId)
     {
         base._dataTypeId = subEntryType.DataTypeId;
     }
     if ((base._arrayInfo == null) && (subEntryType.ArrayInfo != null))
     {
         base._arrayInfo = subEntryType.ArrayInfo;
     }
 }
コード例 #9
0
        public bool TryResolveType(string name, out IDataType ret)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("typeName");
            }
            ITcAdsDataType type = null;

            if ((this._dataTypes != null) && !this._dataTypes.TryGetDataType(name, out type))
            {
                int  length    = 0;
                bool isUnicode = false;
                AdsDatatypeArrayInfo[] dims = null;
                if (DataTypeStringParser.TryParseString(name, out length, out isUnicode))
                {
                    AdsDatatypeId dataType = isUnicode ? AdsDatatypeId.ADST_WSTRING : AdsDatatypeId.ADST_STRING;
                    type = new TcAdsDataType(name, dataType, isUnicode ? ((uint)((length + 1) * 2)) : ((uint)(length + 1)), AdsDataTypeFlags.DataType, DataTypeCategory.String, typeof(string), this);
                }
                else
                {
                    string str;
                    if (DataTypeStringParser.TryParsePointer(name, out str))
                    {
                        ITcAdsDataType type2 = (ITcAdsDataType)this.ResolveDataType(str);
                        type = new TcAdsDataType(name, AdsDatatypeId.ADST_BIGTYPE, (uint)this.PlatformPointerSize, AdsDataTypeFlags.DataType, DataTypeCategory.Pointer, null, this);
                    }
                    else if (DataTypeStringParser.TryParseReference(name, out str))
                    {
                        ITcAdsDataType type3 = (ITcAdsDataType)this.ResolveDataType(str);
                        type = new TcAdsDataType(name, AdsDatatypeId.ADST_BIGTYPE, (uint)this.PlatformPointerSize, AdsDataTypeFlags.DataType, DataTypeCategory.Reference, null, this);
                    }
                    else if (DataTypeStringParser.TryParseArray(name, out dims, out str))
                    {
                        ITcAdsDataType type4 = (ITcAdsDataType)this.ResolveDataType(str);
                        if (type4 != null)
                        {
                            type = new TcAdsDataType(name, str, (uint)type4.Size, dims, this);
                        }
                    }
                }
                if (type == null)
                {
                    this.OnTypeResolveError(name);
                }
                else
                {
                    this._dataTypes.Add(type);
                    this.OnTypeCreated(type);
                }
            }
            ret = type;
            return(ret != null);
        }
コード例 #10
0
        private TcAdsDataTypeCollection collectDataTypes(int count, uint[] dataTypeOffsets, AdsBinaryReader dataTypeReader)
        {
            TcAdsDataTypeCollection types = new TcAdsDataTypeCollection();

            types.AddRange(this._defaultTypes);
            AdsDataTypeEntry[] entryArray = new AdsDataTypeEntry[count];
            for (int i = 0; i < count; i++)
            {
                entryArray[i] = this.GetDataTypeEntry(i, dataTypeOffsets, dataTypeReader);
            }
            for (int j = 0; j < count; j++)
            {
                if (!(this._streamIncludesBuildInTypes && this._defaultTypes.Contains(entryArray[j].entryName)))
                {
                    TcAdsDataType item = new TcAdsDataType(entryArray[j], this);
                    if (item.IsPointer || item.IsReference)
                    {
                        this.SetPlatformPointerSize(item.Size);
                        Type managedType = typeof(ulong);
                        if (item.Size == 4)
                        {
                            managedType = typeof(uint);
                        }
                        for (int k = 0; k < types.Count; k++)
                        {
                            ITcAdsDataType dataType = types[k];
                            if (dataType.Size <= 0)
                            {
                                if (dataType.Category == DataTypeCategory.Alias)
                                {
                                    ITcAdsDataType type4 = types.ResolveType(dataType, DataTypeResolveStrategy.Alias);
                                    if ((type4 != null) && (type4.Category == DataTypeCategory.Pointer))
                                    {
                                        ((TcAdsDataType)dataType).SetSize(item.Size, managedType);
                                    }
                                }
                                if (dataType.Category == DataTypeCategory.Pointer)
                                {
                                    ((TcAdsDataType)dataType).SetSize(item.Size, managedType);
                                }
                            }
                        }
                    }
                    types.Add(item);
                }
            }
            return(types);
        }
コード例 #11
0
            internal TcAdsSymbolInfo CreateSubSymbol(TcAdsSymbolInfo parent, int subIndex)
            {
                if (parent == null)
                {
                    throw new ArgumentNullException("parent");
                }
                ITcAdsDataType dataType = parent.DataType;

                if (dataType == null)
                {
                    throw new ArgumentException("DataType not specified", "parent");
                }
                if (((subIndex < 0) || (dataType.SubItems == null)) || (subIndex >= dataType.SubItems.Count))
                {
                    throw new ArgumentOutOfRangeException("subIndex");
                }
                TcAdsSymbolInfo subSymbol = null;

                if (parent.IsPointer && (subIndex > 0))
                {
                    throw new ArgumentOutOfRangeException("subIndex");
                }
                TcAdsSubItem  typeSubEntry = (TcAdsSubItem)dataType.SubItems[subIndex];
                TcAdsDataType typeByName   = (TcAdsDataType)this._parser.GetTypeByName(typeSubEntry.Name);

                if (typeSubEntry != null)
                {
                    if (typeByName != null)
                    {
                        typeSubEntry.AlignSubItemToType(typeByName);
                    }
                    subSymbol = new TcAdsSymbolInfo(this._parser, parent, subIndex)
                    {
                        size           = (uint)typeSubEntry.Size,
                        dataTypeId     = typeSubEntry.DataTypeId,
                        typeEntryFlags = this.createTypeEntryFlags(parent, typeSubEntry),
                        flags          = this.createSubSymbolFlags(parent, typeSubEntry),
                        shortName      = typeSubEntry.SubItemName
                    };
                    subSymbol.instancePath = $"{parent.instancePath}.{subSymbol.shortName}";
                    subSymbol.typeName     = typeSubEntry.Name;
                    subSymbol.comment      = typeSubEntry.Comment;
                    subSymbol.arrayInfo    = typeSubEntry.ArrayInfo;
                    subSymbol.attributes   = typeSubEntry.Attributes;
                    this.calcFieldAddress(parent, subSymbol, typeSubEntry, out subSymbol.indexGroup, out subSymbol.indexOffset);
                }
                return(subSymbol);
            }
コード例 #12
0
 static DataTypeInfoTable()
 {
     TcAdsDataType[] typeArray1 = new TcAdsDataType[0x2a];
     typeArray1[0]    = new TcAdsDataType("VOID", AdsDatatypeId.ADST_VOID, 0, AdsDataTypeFlags.DataType, DataTypeCategory.Primitive, null, null);
     typeArray1[1]    = new TcAdsDataType("POINTER TO VOID", AdsDatatypeId.ADST_VOID, 0, AdsDataTypeFlags.DataType, DataTypeCategory.Pointer, null, null);
     typeArray1[2]    = new TcAdsDataType("PVOID", AdsDatatypeId.ADST_BIGTYPE, 0, AdsDataTypeFlags.DataType, DataTypeCategory.Alias, "POINTER TO VOID", null, null);
     typeArray1[3]    = new TcAdsDataType("BIT", AdsDatatypeId.ADST_BIT, 1, AdsDataTypeFlags.BitValues | AdsDataTypeFlags.DataType, DataTypeCategory.Primitive, typeof(bool), null);
     typeArray1[4]    = new TcAdsDataType("BIT2", AdsDatatypeId.ADST_BIT, 2, AdsDataTypeFlags.BitValues | AdsDataTypeFlags.DataType, DataTypeCategory.Primitive, typeof(byte), null);
     typeArray1[5]    = new TcAdsDataType("BIT3", AdsDatatypeId.ADST_BIT, 3, AdsDataTypeFlags.BitValues | AdsDataTypeFlags.DataType, DataTypeCategory.Primitive, typeof(byte), null);
     typeArray1[6]    = new TcAdsDataType("BIT4", AdsDatatypeId.ADST_BIT, 4, AdsDataTypeFlags.BitValues | AdsDataTypeFlags.DataType, DataTypeCategory.Primitive, typeof(byte), null);
     typeArray1[7]    = new TcAdsDataType("BIT5", AdsDatatypeId.ADST_BIT, 5, AdsDataTypeFlags.BitValues | AdsDataTypeFlags.DataType, DataTypeCategory.Primitive, typeof(byte), null);
     typeArray1[8]    = new TcAdsDataType("BIT6", AdsDatatypeId.ADST_BIT, 6, AdsDataTypeFlags.BitValues | AdsDataTypeFlags.DataType, DataTypeCategory.Primitive, typeof(byte), null);
     typeArray1[9]    = new TcAdsDataType("BIT7", AdsDatatypeId.ADST_BIT, 7, AdsDataTypeFlags.BitValues | AdsDataTypeFlags.DataType, DataTypeCategory.Primitive, typeof(byte), null);
     typeArray1[10]   = new TcAdsDataType("BIT8", AdsDatatypeId.ADST_BIT, 8, AdsDataTypeFlags.BitValues | AdsDataTypeFlags.DataType, DataTypeCategory.Primitive, typeof(byte), null);
     typeArray1[11]   = new TcAdsDataType("SINT", AdsDatatypeId.ADST_INT8, 1, DataTypeCategory.Primitive, typeof(sbyte));
     typeArray1[12]   = new TcAdsDataType("USINT", AdsDatatypeId.ADST_UINT8, 1, DataTypeCategory.Primitive, typeof(byte));
     typeArray1[13]   = new TcAdsDataType("BYTE", AdsDatatypeId.ADST_UINT8, 1, DataTypeCategory.Primitive, typeof(byte));
     typeArray1[14]   = new TcAdsDataType("UINT8", AdsDatatypeId.ADST_UINT8, 1, DataTypeCategory.Primitive, typeof(byte));
     typeArray1[15]   = new TcAdsDataType("INT", AdsDatatypeId.ADST_INT16, 2, DataTypeCategory.Primitive, typeof(short));
     typeArray1[0x10] = new TcAdsDataType("INT16", AdsDatatypeId.ADST_INT16, 2, DataTypeCategory.Primitive, typeof(short));
     typeArray1[0x11] = new TcAdsDataType("UINT", AdsDatatypeId.ADST_UINT16, 2, DataTypeCategory.Primitive, typeof(ushort));
     typeArray1[0x12] = new TcAdsDataType("WORD", AdsDatatypeId.ADST_UINT16, 2, DataTypeCategory.Primitive, typeof(ushort));
     typeArray1[0x13] = new TcAdsDataType("UINT16", AdsDatatypeId.ADST_UINT16, 2, DataTypeCategory.Primitive, typeof(ushort));
     typeArray1[20]   = new TcAdsDataType("DINT", AdsDatatypeId.ADST_INT32, 4, DataTypeCategory.Primitive, typeof(int));
     typeArray1[0x15] = new TcAdsDataType("INT32", AdsDatatypeId.ADST_INT32, 4, DataTypeCategory.Primitive, typeof(int));
     typeArray1[0x16] = new TcAdsDataType("UDINT", AdsDatatypeId.ADST_UINT32, 4, DataTypeCategory.Primitive, typeof(uint));
     typeArray1[0x17] = new TcAdsDataType("UINT32", AdsDatatypeId.ADST_UINT32, 4, DataTypeCategory.Primitive, typeof(int));
     typeArray1[0x18] = new TcAdsDataType("UXINT", AdsDatatypeId.ADST_BIGTYPE, 0, AdsDataTypeFlags.DataType, DataTypeCategory.Alias, "PVOID", null, null);
     typeArray1[0x19] = new TcAdsDataType("DWORD", AdsDatatypeId.ADST_UINT32, 4, DataTypeCategory.Primitive, typeof(uint));
     typeArray1[0x1a] = new TcAdsDataType("REAL", AdsDatatypeId.ADST_REAL32, 4, DataTypeCategory.Primitive, typeof(float));
     typeArray1[0x1b] = new TcAdsDataType("FLOAT", AdsDatatypeId.ADST_REAL32, 4, DataTypeCategory.Primitive, typeof(float));
     typeArray1[0x1c] = new TcAdsDataType("LREAL", AdsDatatypeId.ADST_REAL64, 8, DataTypeCategory.Primitive, typeof(double));
     typeArray1[0x1d] = new TcAdsDataType("DOUBLE", AdsDatatypeId.ADST_REAL64, 8, DataTypeCategory.Primitive, typeof(double));
     typeArray1[30]   = new TcAdsDataType("BOOL", AdsDatatypeId.ADST_BIT, 1, DataTypeCategory.Primitive, typeof(bool));
     typeArray1[0x1f] = new TcAdsDataType("TIME", AdsDatatypeId.ADST_BIGTYPE, 4, DataTypeCategory.Primitive, typeof(TIME));
     typeArray1[0x20] = new TcAdsDataType("TOD", AdsDatatypeId.ADST_BIGTYPE, 4, DataTypeCategory.Primitive, typeof(TOD));
     typeArray1[0x21] = new TcAdsDataType("DATE", AdsDatatypeId.ADST_BIGTYPE, 4, DataTypeCategory.Primitive, typeof(DATE));
     typeArray1[0x22] = new TcAdsDataType("DT", AdsDatatypeId.ADST_BIGTYPE, 4, DataTypeCategory.Primitive, typeof(DT));
     typeArray1[0x23] = new TcAdsDataType("LTIME", AdsDatatypeId.ADST_BIGTYPE, 8, DataTypeCategory.Primitive, typeof(LTIME));
     typeArray1[0x24] = new TcAdsDataType("DATE_AND_TIME", AdsDatatypeId.ADST_BIGTYPE, 4, DataTypeCategory.Alias, "DT", typeof(DT));
     typeArray1[0x25] = new TcAdsDataType("TIME_OF_DAY", AdsDatatypeId.ADST_BIGTYPE, 4, DataTypeCategory.Alias, "TOD", typeof(TOD));
     typeArray1[0x26] = new TcAdsDataType("LINT", AdsDatatypeId.ADST_INT64, 8, DataTypeCategory.Primitive, typeof(long));
     typeArray1[0x27] = new TcAdsDataType("ULINT", AdsDatatypeId.ADST_UINT64, 8, DataTypeCategory.Primitive, typeof(ulong));
     typeArray1[40]   = new TcAdsDataType("LWORD", AdsDatatypeId.ADST_UINT64, 8, DataTypeCategory.Primitive, typeof(ulong));
     typeArray1[0x29] = new TcAdsDataType("OTCID", AdsDatatypeId.ADST_BIGTYPE, 4, DataTypeCategory.Alias, "UDINT", typeof(uint));
     _defaultTypes    = typeArray1;
 }
コード例 #13
0
            private int calcElementBaseSize(TcAdsSymbolInfo parentArrayInstance)
            {
                TcAdsDataType type      = (TcAdsDataType)parentArrayInstance.DataType.ResolveType(DataTypeResolveStrategy.AliasReference);
                int           size      = type.Size;
                bool          isBitType = parentArrayInstance.IsBitType;
                ReadOnlyDimensionCollection dimensions = type.Dimensions;

                for (int i = type.Dimensions.Count - 1; i >= 0; i--)
                {
                    if (isBitType)
                    {
                        size = 1;
                    }
                    else if (dimensions[i].ElementCount > 0)
                    {
                        size /= dimensions[i].ElementCount;
                    }
                }
                return(size);
            }
コード例 #14
0
        public AdsErrorCode TryLoadType(string name, bool lookup, out ITcAdsDataType type)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentOutOfRangeException("name");
            }
            if (lookup)
            {
                DataTypeInfoTable table = this;
                lock (table)
                {
                    if (this._dataTypes.TryGetDataType(name, out type))
                    {
                        return(AdsErrorCode.NoError);
                    }
                }
            }
            int  length    = 0;
            bool isUnicode = false;

            if (DataTypeStringParser.TryParseString(name, out length, out isUnicode))
            {
                int           byteCount = 0;
                AdsDatatypeId dataType  = AdsDatatypeId.ADST_VOID;
                if (isUnicode)
                {
                    char[] chars = new char[] { 'a' };
                    byteCount = Encoding.Unicode.GetByteCount(chars);
                    dataType  = AdsDatatypeId.ADST_WSTRING;
                }
                else
                {
                    char[] chars = new char[] { 'a' };
                    byteCount = Encoding.ASCII.GetByteCount(chars);
                    dataType  = AdsDatatypeId.ADST_STRING;
                }
                type = new TcAdsDataType(name, dataType, (uint)((length + 1) * byteCount), DataTypeCategory.String, typeof(string));
                DataTypeInfoTable table2 = this;
                lock (table2)
                {
                    this._dataTypes.Add(type);
                }
                this.OnTypeGenerated(type);
                return(AdsErrorCode.NoError);
            }
            AdsStream stream = new AdsStream(name.Length + 1);

            using (AdsBinaryWriter writer = new AdsBinaryWriter(stream))
            {
                writer.WritePlcString(name, name.Length + 1, this._encoding);
                AdsStream rdDataStream = new AdsStream(0xffff);
                try
                {
                    int num3 = this._adsClient.ReadWrite(0xf011, 0, rdDataStream, stream);
                }
                catch (AdsErrorException exception1)
                {
                    if (exception1.ErrorCode != AdsErrorCode.DeviceSymbolNotFound)
                    {
                        throw;
                    }
                    type = null;
                }
                using (AdsBinaryReader reader = new AdsBinaryReader(rdDataStream))
                {
                    AdsDataTypeEntry entry = new AdsDataTypeEntry(true, this._encoding, reader);
                    type = new TcAdsDataType(entry, this);
                    DataTypeInfoTable table3 = this;
                    lock (table3)
                    {
                        this._dataTypes.Add(type);
                    }
                }
            }
            if (type != null)
            {
                return(AdsErrorCode.NoError);
            }
            this.OnResolveError(name);
            return(AdsErrorCode.DeviceSymbolNotFound);
        }
コード例 #15
0
        internal int InitializeArray(Type managedType, TcAdsDataType adsType, AdsBinaryReader reader, int readerOffset, int jagLevel, out object value)
        {
            int num2;

            if (managedType == null)
            {
                throw new ArgumentNullException("managedType");
            }
            if (adsType == null)
            {
                throw new ArgumentNullException("adsType");
            }
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            int num = readerOffset;

            if (!managedType.IsArray)
            {
                throw new ArgumentException($"Cannot convert datatype of symbol to this type. Expected an array, actual type is {managedType.ToString()}", "type");
            }
            Type elementType = managedType.GetElementType();

            AdsDatatypeArrayInfo[] arrayInfo = adsType.ArrayInfo;
            Type baseElementType             = null;
            bool flag = PrimitiveTypeConverter.TryJaggedArray(managedType, out num2, out baseElementType);
            int  arrayElementCount = AdsArrayDimensionsInfo.GetArrayElementCount(arrayInfo);
            int  byteLength        = adsType.ByteSize / arrayElementCount;

            if (flag)
            {
                if (arrayInfo.Length != 1)
                {
                    throw new ArgumentException($"Cannot convert datatype of symbol to this type. Expected an array rank of {arrayInfo.Length}, actual rank is {num2}", "type");
                }
            }
            else
            {
                int length = arrayInfo.Length;
                if (length != managedType.GetArrayRank())
                {
                    throw new ArgumentException($"Cannot convert datatype of symbol to this type. Expected an array rank of {length}, actual rank is {managedType.GetArrayRank()}", "type");
                }
            }
            AdsArrayDimensionsInfo info = new AdsArrayDimensionsInfo(arrayInfo);
            Array array = null;

            if (flag)
            {
                jagLevel++;
                array = Array.CreateInstance(elementType, info.DimensionElements[jagLevel]);
                int[] lowerBounds = new int[] { info.LowerBounds[jagLevel] };
                int[] upperBounds = new int[] { info.UpperBounds[jagLevel] };
                foreach (int[] numArray in new ArrayIndexIterator(lowerBounds, upperBounds, true))
                {
                    object obj2 = null;
                    readerOffset += this.InitializeArray(elementType, (TcAdsDataType)adsType.BaseType, reader, readerOffset, jagLevel, out obj2);
                    array.SetValue(obj2, numArray);
                }
            }
            else
            {
                DataTypeCategory category = adsType.BaseType.Category;
                array = Array.CreateInstance(elementType, info.DimensionElements);
                foreach (int[] numArray2 in new ArrayIndexIterator(info.LowerBounds, info.UpperBounds, true))
                {
                    object obj3 = null;
                    if (category == DataTypeCategory.Primitive)
                    {
                        if (elementType != ((TcAdsDataType)adsType.BaseType).ManagedType)
                        {
                            throw new ArgumentException("Cannot convert data type of symbol to this type.", "type");
                        }
                        readerOffset += this.InitializePrimitiveType(adsType.BaseType.Name, elementType, adsType.DataTypeId, byteLength, reader, readerOffset, out obj3);
                        array.SetValue(obj3, numArray2);
                        continue;
                    }
                    if (category == DataTypeCategory.Enum)
                    {
                        if (elementType != ((TcAdsDataType)adsType.BaseType).ManagedType)
                        {
                            throw new ArgumentException("Cannot convert data type of symbol to this type.", "type");
                        }
                        readerOffset += this.InitializeEnum(adsType.BaseType.Name, elementType, adsType.DataTypeId, byteLength, reader, readerOffset, out obj3);
                        array.SetValue(obj3, numArray2);
                        continue;
                    }
                    if (adsType.BaseType.Category == DataTypeCategory.Struct)
                    {
                        readerOffset += this.InitializeStruct(adsType.BaseType.SubItems, elementType, reader, readerOffset, out obj3);
                        array.SetValue(obj3, numArray2);
                        continue;
                    }
                    if (adsType.BaseType.Category == DataTypeCategory.Array)
                    {
                        readerOffset += this.InitializeArray(elementType, (TcAdsDataType)adsType.BaseType, reader, readerOffset, jagLevel, out obj3);
                        array.SetValue(obj3, numArray2);
                    }
                }
            }
            value = array;
            return(readerOffset - num);
        }
コード例 #16
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;
        }
コード例 #17
0
            internal TcAdsSymbolInfo CreateArrayElement(TcAdsSymbolInfo parentArrayInstance, TcAdsDataType parentArrayType, int subIndex)
            {
                if (parentArrayInstance == null)
                {
                    throw new ArgumentNullException("arrayInstance");
                }
                if (parentArrayType == null)
                {
                    throw new ArgumentNullException("parentArrayType");
                }
                TcAdsSymbolInfo info     = null;
                ITcAdsDataType  baseType = parentArrayType.BaseType;

                if (baseType == null)
                {
                    DataTypeException innerException = new DataTypeException("Base type of Array '{0}' not defined!", parentArrayType);
                    throw new AdsSymbolException("Cannot create array element!", parentArrayInstance, innerException);
                }
                bool isBitType = parentArrayInstance.IsBitType;
                ReadOnlyDimensionCollection dimensions = parentArrayType.Dimensions;
                int elementSize = this.calcElementBaseSize(parentArrayInstance);

                if (subIndex < parentArrayType.Dimensions.ElementCount)
                {
                    info = new TcAdsSymbolInfo(this._parser, parentArrayInstance, subIndex)
                    {
                        size           = (uint)elementSize,
                        typeEntryFlags = this.createElementTypeEntryFlags(parentArrayInstance, (TcAdsDataType)baseType),
                        flags          = this.createElementSymbolFlags(parentArrayInstance, (TcAdsDataType)baseType),
                        dataTypeId     = (baseType == null) ? AdsDatatypeId.ADST_BIGTYPE : baseType.DataTypeId
                    };
                    if (baseType != null)
                    {
                        info.typeName  = baseType.Name;
                        info.arrayInfo = baseType.Dimensions.ToArray();
                    }
                    else
                    {
                        info.typeName  = parentArrayType.BaseTypeName;
                        info.arrayInfo = null;
                    }
                    info.comment = parentArrayInstance.Comment;
                    this.calcArrayElementIndexGroupIndexOffset(subIndex, parentArrayInstance, elementSize, out info.indexGroup, out info.indexOffset);
                    string str = ArrayIndexConverter.SubIndexToIndexString(parentArrayType.Dimensions.LowerBounds, parentArrayType.Dimensions.UpperBounds, subIndex);
                    info.instancePath = parentArrayInstance.instancePath + str;
                    info.shortName    = parentArrayInstance.shortName + str;
                    info.attributes   = parentArrayInstance.attributes;
                }
                if ((subIndex == parentArrayType.Dimensions.ElementCount) && ((parentArrayType.Flags & (AdsDataTypeFlags.None | AdsDataTypeFlags.Oversample)) != AdsDataTypeFlags.None))
                {
                    info = new TcAdsSymbolInfo(this._parser, parentArrayInstance, subIndex)
                    {
                        size           = (uint)elementSize,
                        typeEntryFlags = this.createElementTypeEntryFlags(parentArrayInstance, (TcAdsDataType)baseType),
                        flags          = this.createElementSymbolFlags(parentArrayInstance, (TcAdsDataType)baseType)
                    };
                    if (info.typeEntryFlags.HasFlag(AdsDataTypeFlags.None | AdsDataTypeFlags.Static))
                    {
                        info.indexGroup  = 0xf019;
                        info.indexOffset = 0;
                    }
                    else
                    {
                        info.indexGroup  = parentArrayInstance.indexGroup;
                        info.indexOffset = parentArrayInstance.indexOffset;
                    }
                    if (baseType != null)
                    {
                        info.dataTypeId = baseType.DataTypeId;
                        info.typeName   = baseType.Name;
                    }
                    else
                    {
                        info.dataTypeId = AdsDatatypeId.ADST_BIGTYPE;
                        info.typeName   = parentArrayType.BaseTypeName;
                    }
                    info.comment = parentArrayInstance.Comment;
                    string str2 = ArrayIndexConverter.OversamplingSubElementToString(subIndex);
                    info.instancePath = parentArrayInstance.instancePath + str2;
                    info.shortName    = parentArrayInstance.shortName + str2;
                    if ((parentArrayType.Flags & (AdsDataTypeFlags.None | AdsDataTypeFlags.PropItem)) == (AdsDataTypeFlags.None | AdsDataTypeFlags.PropItem))
                    {
                        info.indexGroup  = 0xf017;
                        info.indexOffset = parentArrayType.TypeHashValue;
                    }
                    info.attributes = parentArrayInstance.attributes;
                }
                return(info);
            }
コード例 #18
0
            private AdsSymbolFlags createElementSymbolFlags(TcAdsSymbolInfo arrayInstance, TcAdsDataType elementType)
            {
                AdsSymbolFlags none = AdsSymbolFlags.None;

                if (elementType != null)
                {
                    none = DataTypeFlagConverter.Convert(elementType.Flags);
                }
                none = (none & (AdsSymbolFlags.Attributes | AdsSymbolFlags.BitValue | AdsSymbolFlags.ExtendedFlags | AdsSymbolFlags.InitOnReset | AdsSymbolFlags.ItfMethodAccess | AdsSymbolFlags.MethodDeref | AdsSymbolFlags.Persistent | AdsSymbolFlags.ReadOnly | AdsSymbolFlags.ReferenceTo | AdsSymbolFlags.Static | AdsSymbolFlags.TComInterfacePtr | AdsSymbolFlags.TypeGuid)) | (AdsSymbolFlags.ContextMask & arrayInstance.flags);
                if (arrayInstance.IsBitType)
                {
                    none |= AdsSymbolFlags.BitValue;
                }
                if (arrayInstance.IsPersistent)
                {
                    none |= AdsSymbolFlags.None | AdsSymbolFlags.Persistent;
                }
                return(none);
            }
コード例 #19
0
 private AdsDataTypeFlags createElementTypeEntryFlags(TcAdsSymbolInfo parentArrayInstance, TcAdsDataType elementType) =>
 ((elementType == null) ? AdsDataTypeFlags.None : elementType.Flags);
コード例 #20
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;
        }