public void AddTag(Tag tag)
        {
            if (_tags.Contains(tag))
            {
                return;
            }

            var logixTag = new Logix.Tag(_controlLogix)
            {
                Name     = tag.FullName(),
                DataType = IngearHelper.ParseNetLogixDataType(tag.DataType)
            };

            logixTag.Changed += TagDataChanged;

            tag.ValueChanged += TagValueChanged;

            _tags.Add(tag);
            _tagGroup.AddTag(logixTag);
        }
Exemple #2
0
        public Task WriteTag(Tag tag, object value)
        {
            if (tag.Value == null && value == null)
            {
                return(Task.FromResult(true));
            }

            var task = _writeTaskScheduler.Schedule(() =>
            {
                tag.LockValue();

                try
                {
                    tag.DetectDataTypeForValue(value);

                    if (!IsConnected)
                    {
                        throw new PlcCommunicationException(
                            "IngearTagController can't write tag because there is no connection established!",
                            _controlLogix.IPAddress, string.Empty);
                    }

                    var logixTag = new Logix.Tag(_controlLogix)
                    {
                        Name     = tag.FullName(),
                        DataType = IngearHelper.ParseNetLogixDataType(tag.DataType)
                    };

                    // for some reason we must read the tag at least once
                    if (logixTag.ErrorCode != 0)
                    {
                        _controlLogix.ReadTag(logixTag);
                    }

                    // handle special DataTypes like UDTs
                    ConvertToLogixTagValueFunction convertFunction;
                    if (_tagValueConverters != null &&
                        _tagValueConverters.TryGetValue(tag.DataType, out convertFunction))
                    {
                        logixTag.Value = convertFunction(value);
                    }
                    else
                    {
                        if (tag.DataType.Equals(IEC61131_3_DataTypes.Boolean))
                        {
                            if ((bool)value)
                            {
                                logixTag.Value = "True";
                            }
                            else
                            {
                                logixTag.Value = "False";
                            }
                        }
                        else
                        {
                            logixTag.Value = value;
                        }
                    }

                    var result = _controlLogix.WriteTag(logixTag);

                    if (result != ResultCode.E_SUCCESS)
                    {
                        throw new PlcCommunicationException("Can't write value to ControlLogix.",
                                                            _controlLogix.IPAddress,
                                                            _controlLogix.Path, logixTag.ErrorString);
                    }
                }
                finally
                {
                    tag.ReleaseValue();
                }
            });

            return(task);
        }