示例#1
0
        /// <summary>
        /// Creates a tag. If the CPU type is LGX, the port type and slot has to be specified.
        /// </summary>
        /// <param name="controller">Controller reference</param>
        /// <param name="name">The textual name of the tag to access. The name is anything allowed by the protocol.
        /// E.g. myDataStruct.rotationTimer.ACC, myDINTArray[42] etc.</param>
        /// <param name="size">The size of an element in bytes. The tag is assumed to be composed of elements of the same size.
        /// For structure tags, use the total size of the structure.</param>
        /// <param name="length">elements count: 1- single, n-array.</param>
        internal Tag(Controller controller, string name, int size, int length = 1)
        {
            Controller   = controller;
            Name         = name;
            Size         = size;
            Length       = length;
            ValueManager = new TagValueManager(this);
            TypeValue    = typeof(TType);

            var url = $"protocol=ab_eip&gateway={controller.IPAddress}";

            if (!string.IsNullOrEmpty(controller.Path))
            {
                url += $"&path={controller.Path}";
            }
            url += $"&cpu={controller.CPUType}&elem_size={Size}&elem_count={Length}&name={Name}";
            if (controller.DebugLevel > 0)
            {
                url += $"&debug={controller.DebugLevel}";
            }

            //create reference
            Handle = NativeMethod.plc_tag_create(url, controller.Timeout);

            Value = TagHelper.CreateObject <TType>(Length);
        }
示例#2
0
        /// <summary>
        /// Get size from object.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int GetSizeFromObject(object obj)
        {
            var size = 0;

            var type = obj.GetType();

            if (type.IsArray)
            {
                foreach (var el in TagValueManager.GetArray(obj))
                {
                    size += GetSizeFromObject(el);
                }
            }
            else
            {
                if (!NativeTypes.TryGetValue(type, out size))
                {
                    if (type.IsClass && !type.IsAbstract)
                    {
                        foreach (var property in TagValueManager.GetAccessableProperties(type))
                        {
                            size += GetSizeFromObject(property.GetValue(obj));
                        }
                    }
                }
            }

            return(size);
        }
示例#3
0
        /// <summary>
        /// Create Tag array.
        /// </summary>
        /// <param name="name">The textual name of the tag to access. The name is anything allowed by the protocol.
        /// E.g. myDataStruct.rotationTimer.ACC, myDINTArray[42] etc.</param>
        /// <param name="length">elements count: 1- single, n-array.</param>
        /// <typeparam name="TCustomType">Type to create</typeparam>
        /// <returns></returns>
        public Tag <TCustomType> CreateTagArray <TCustomType>(string name, int length)
        {
            var type = typeof(TCustomType);

            if (!type.IsArray)
            {
                throw new ArgumentException("Is not array!");
            }

            var obj = (Array)Activator.CreateInstance(type, length);

            TagValueManager.FixStringNullToEmpty(obj);

            return(CreateTagType <TCustomType>(name,
                                               TagSize.GetSizeFromObject(obj.GetValue(0)),
                                               length));
        }
示例#4
0
        internal static TType CreateObject <TType>(int length)
        {
            var obj       = default(TType);
            var typeTType = typeof(TType);

            if (typeTType == typeof(string))
            {
                obj = (TType)((object)"");
            }
            else if (typeTType.IsArray)
            {
                obj = (TType)Activator.CreateInstance(typeTType, length);
            }
            else
            {
                obj = (TType)Activator.CreateInstance(typeTType);
            }

            TagValueManager.FixStringNullToEmpty(obj);

            return(obj);
        }