示例#1
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);

                    var item = new Item(tag.FullName(), 1);

                    Result result;
                    _device.Write(item, value, out result);

                    if (!result.IsOK)
                    {
                        throw new PlcCommunicationException("Can't write value to ControlLogix.",
                                                            _device.RoutePath, result.EvArgs.Message);
                    }
                }
                finally
                {
                    tag.ReleaseValue();
                }
            });

            return(task);
        }
示例#2
0
        protected virtual void TryStartNextJob()
        {
            RemoveJobsScheduler.Schedule(RemoveCompletedAndInvalidJobs);

            var job = GetUnstartedJobs().FirstOrDefault();

            if (job == null)
            {
                return;
            }

            EventAggregator.Publish(new TryStartJobEvent(job.JobId));

            Logger.DebugFormat("Job counts: unstarted={0}, ongoing={1}, completed={2}",
                               GetUnstartedJobs().Count(), GetJobsInProduction().Count(), GetCompletedJobs().Count());

            StartJobScheduler.Schedule(TryStartNextJob);
        }
 public void RecalculateRoute()
 {
     Scheduler.Schedule(DoRecalculateRoute);
 }
        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(
                            "BeckhoffTagController can't write tag because there is no connection established!",
                            _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture), string.Empty);
                    }

                    var address    = GetPlcAddress(tag);
                    var dataStream = new AdsStream(tag.BitSize == 0 ? 81 : tag.BitSize / 8);

                    try
                    {
                        if (address == null)
                        {
                            int handle       = _twinCatClient.CreateVariableHandle(tag.FullName());
                            var isDataStream = WriteToStream(dataStream, tag, value);

                            try
                            {
                                if (isDataStream)
                                {
                                    _twinCatClient.Write(handle, dataStream);
                                }
                                else
                                {
                                    _twinCatClient.WriteAny(handle, value);
                                }
                            }
                            finally
                            {
                                _twinCatClient.DeleteVariableHandle(handle);
                            }
                        }
                        else
                        {
                            var isDataStream = WriteToStream(dataStream, tag, value);
                            if (isDataStream)
                            {
                                _twinCatClient.Write(address.IndexGroup, address.IndexOffset, dataStream);
                            }
                            else
                            {
                                _twinCatClient.WriteAny(address.IndexGroup, address.IndexOffset, value);
                            }
                        }
                    }
                    catch (AdsErrorException ex)
                    {
                        if (_logger != null)
                        {
                            _logger.Warn("Couldn't write " + tag.FullName() + " because " + ex.ErrorCode.ToString() + " will try again...");
                        }

                        try
                        {
                            // try again
                            if (address == null)
                            {
                                int handle       = _twinCatClient.CreateVariableHandle(tag.FullName());
                                var isDataStream = WriteToStream(dataStream, tag, value);

                                try
                                {
                                    if (isDataStream)
                                    {
                                        _twinCatClient.Write(handle, dataStream);
                                    }
                                    else
                                    {
                                        _twinCatClient.WriteAny(handle, value);
                                    }
                                }
                                finally
                                {
                                    _twinCatClient.DeleteVariableHandle(handle);
                                }
                            }
                            else
                            {
                                var isDataStream = WriteToStream(dataStream, tag, value);
                                if (isDataStream)
                                {
                                    _twinCatClient.Write(address.IndexGroup, address.IndexOffset, dataStream);
                                }
                                else
                                {
                                    _twinCatClient.WriteAny(address.IndexGroup, address.IndexOffset, value);
                                }
                            }
                        }
                        catch (AdsErrorException e)
                        {
                            throw new PlcCommunicationException("Can't write tag " + tag.FullName() + " on port " + _adsPort.ToString(CultureInfo.InvariantCulture),
                                                                _adsPort.ToString(CultureInfo.InvariantCulture),
                                                                _adsPort.ToString(CultureInfo.InvariantCulture), string.Empty, e);
                        }
                    }
                }
                finally
                {
                    tag.ReleaseValue();
                }

                // at this point we assume the value was written successfully to the PLC
                Type type;
                if (IEC61131_3_DataTypes.NetDataTypes.TryGetValue(tag.DataType, out type))
                {
                    tag.Value = Convert.ChangeType(value, type);
                }
                else
                {
                    tag.Value = value;
                }
            });

            return(task);
        }
示例#5
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);
        }