Esempio n. 1
0
        public void ReadTagSynchronously(Tag tag)
        {
            tag.LockValue();

            try
            {
                var logixTag = GetLogixTag(tag.FullName());

                _controlLogix.ReadTag(new[] { logixTag });

                // handle special DataTypes like UDTs
                object convertedValue;
                if (TryConvertToCustomValue(tag, logixTag, out convertedValue))
                {
                    tag.Value = convertedValue;
                }
                else
                {
                    tag.Value = logixTag.Value;
                }

                if (TagChanged != null)
                {
                    TagChanged(this, new TagChangedEventArgs(tag));
                }
            }
            finally
            {
                tag.ReleaseValue();
            }
        }
Esempio n. 2
0
        public Task WriteTag(Tag tag, object[] values)
        {
            if (tag.Value == null && values == null)
            {
                return(Task.FromResult(true));
            }

            tag.LockValue();

            try
            {
                throw new NotImplementedException();
            }
            finally
            {
                tag.ReleaseValue();
            }
        }
Esempio n. 3
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);
        }