private void ImportChildTagsRecursively(Tag parentTag, TcAdsSymbolInfo symbol, int adsPort)
        {
            while (symbol != null)
            {
                var tag = new Tag();

                try
                {
                    tag        = BeckhoffHelper.ConvertSymbolToTag(symbol, adsPort);
                    tag.Parent = parentTag;
                    parentTag.Childs.Add(tag);
                }
                catch (TagException e)
                {
                    Console.WriteLine("Cannot import tags online (will skip it): " + e);
                }

                if (symbol.SubSymbolCount > 0 && !symbol.IsPointer && !symbol.IsReference)
                {
                    ImportChildTagsRecursively(tag, symbol.FirstSubSymbol, adsPort);
                }

                symbol = symbol.NextSymbol;
            }
        }
        private Tag ImportTagWithChilds(TcAdsSymbolInfo symbol, int adsPort, HashSet <string> tagHashSet)
        {
            Tag tag = null;

            if (symbol == null)
            {
                return(tag);
            }

            try
            {
                tag = BeckhoffHelper.ConvertSymbolToTag(symbol, adsPort);
            }
            catch (TagException e)
            {
                _logger.Warn("Cannot import tags online (will skip it): " + e);
                return(tag);
            }

            // prevent circular reads (endless loop) by checking if the hashset already contains the tag
            if ((symbol.IsReference || symbol.IsPointer) && (!tag.MetaData.ReadPointer || tagHashSet.Contains(tag.GetPointerlessFullName())))
            {
                return(tag);
            }

            if (tag.MetaData.ReadPointer)
            {
                tagHashSet.Add(tag.GetPointerlessFullName());
            }

            tag.FullName();

            TcAdsSymbolInfo subSymbol = symbol.FirstSubSymbol;

            while (subSymbol != null)
            {
                Tag subTag = ImportTagWithChilds(subSymbol, adsPort, tagHashSet);
                if (subTag != null)
                {
                    tag.Childs.Add(subTag);
                    subTag.Parent = tag;
                }
                subSymbol = subSymbol.NextSymbol;
            }
            return(tag);
        }
        public Tag ImportTag(string name, int adsPort)
        {
            _logger.Debug(string.Format("Importing tag '{0}' on port '{1}", name, adsPort));
            if (_twinCatClient == null)
            {
                return(null);
            }

            var symbol = _twinCatClient.ReadSymbolInfo(name);

            if (symbol == null)
            {
                return(null);
            }

            Tag result = BeckhoffHelper.ConvertSymbolToTag(symbol, adsPort);

            return(result);
        }
 private object ReadNextDataType(string dataType, int bitSize, AdsBinaryReader binaryReader)
 {
     return(BeckhoffHelper.ReadDataType(dataType, bitSize, binaryReader));
 }
Ejemplo n.º 5
0
        private bool WriteToStream(AdsStream dataStream, Tag tag, object value)
        {
            var binaryWriter = new AdsBinaryWriter(dataStream);

            switch (tag.DataType)
            {
            case IEC61131_3_DataTypes.Boolean:
                if (value is string)
                {
                    value = Boolean.Parse((string)value);
                }

                binaryWriter.Write((bool)value);
                break;

            case IEC61131_3_DataTypes.Byte:
                binaryWriter.Write((byte)value);
                break;

            case IEC61131_3_DataTypes.SInt:
                binaryWriter.Write(Convert.ToSByte(value));
                break;

            case IEC61131_3_DataTypes.USInt:
                binaryWriter.Write(Convert.ToByte(value));
                break;

            case IEC61131_3_DataTypes.UInt:
                binaryWriter.Write(Convert.ToUInt16(value));
                break;

            case IEC61131_3_DataTypes.Int:
                binaryWriter.Write(Convert.ToInt16(value));
                break;

            case IEC61131_3_DataTypes.DInt:
                binaryWriter.Write((int)value);
                break;

            case IEC61131_3_DataTypes.LInt:
                binaryWriter.Write(Convert.ToInt64(value));
                break;

            case IEC61131_3_DataTypes.Real:
                binaryWriter.Write((float)value);
                break;

            case IEC61131_3_DataTypes.LReal:
                binaryWriter.Write((double)value);
                break;

            case IEC61131_3_DataTypes.Word:
                binaryWriter.Write(Convert.ToUInt16(value));
                break;

            case IEC61131_3_DataTypes.DWord:
                binaryWriter.Write(Convert.ToUInt32(value));
                break;

            case IEC61131_3_DataTypes.LWord:
                binaryWriter.Write(Convert.ToUInt64(value));
                break;

            case IEC61131_3_DataTypes.ULInt:
                binaryWriter.Write(Convert.ToUInt64(value));
                break;

            case IEC61131_3_DataTypes.UDInt:
                binaryWriter.Write(Convert.ToUInt32(value));
                break;

            default:
                // handle STRINT DataType
                if (StructuredTextSyntaxRegexHelper.StringDataTypeRegex.IsMatch(tag.DataType))
                {
                    var tmpString = (string)value;
                    var length    = tag.BitSize / 8;

                    if (tmpString.Length > length)
                    {
                        throw new TagException("string is exceeds max defined lenght of " + length, tag);
                    }

                    binaryWriter.WritePlcString((string)value, (int)dataStream.Length);
                    break;
                }

                if (BeckhoffHelper.IsEnum(tag.DataType, tag.BitSize))
                {
                    binaryWriter.Write(Convert.ToInt16(value));
                    break;
                }

                switch (tag.BitSize)
                {
                case 1:
                    if (value is string)
                    {
                        value = Boolean.Parse((string)value);
                    }

                    binaryWriter.Write((bool)value);
                    break;

                case 8:
                    binaryWriter.Write(Convert.ToByte(value));
                    break;

                case 16:
                    binaryWriter.Write(Convert.ToUInt16(value));
                    break;

                case 32:
                    binaryWriter.Write(Convert.ToUInt32(value));
                    break;

                case 64:
                    binaryWriter.Write(Convert.ToUInt64(value));
                    break;

                default:
                    return(false);
                }
                break;
            }

            return(true);
        }