Exemplo n.º 1
0
        private void DataChannelStateTagValueChanged(Tag dataChannelStateTag, TagValueChangedEventArgs args)
        {
            if ((short)dataChannelStateTag.Value == (short)DataStateEnum.DataWritten)
            {
                try
                {
                    _tagListener.ReadTagSynchronously(_channelToLineTag);
                }
                catch (PlcCommunicationException e)
                {
                    RaiseCommunicationProblem(e);
                }

                if (_channelToLineTag.Value == null)
                {
                    Thread.Sleep(50);

                    try
                    {
                        // try to read UDT again
                        _tagListener.ReadTagSynchronously(_channelToLineTag);
                    }
                    catch (PlcCommunicationException e)
                    {
                        RaiseCommunicationProblem(e);
                    }

                    if (_channelToLineTag.Value == null)
                    {
                        RaiseCommunicationProblem(new PlcCommunicationException(
                                                      "Can't listen to DataChannel " + _channelToLineTag.Name +
                                                      " because value is null for DataType " +
                                                      _channelToLineTag.DataType + " (tried it two times)",
                                                      _tagListener.AddressAndPath, ""));
                    }
                }

                if (DataReceived != null && _channelToLineTag.Value != null)
                {
                    DataReceived(this, (TDataType)_channelToLineTag.Value);
                }

                try
                {
                    _tagController.WriteTag(dataChannelStateTag, (short)DataStateEnum.DataChannelFree);
                }
                catch (PlcCommunicationException e)
                {
                    RaiseCommunicationProblem(e);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Callback from the Beckhoff Listener that the Datachannel changed
        /// </summary>
        /// <param name="dataChannelStateTag"></param>
        /// <param name="args"></param>
        private void DataChannelStateTagValueChanged(Tag dataChannelStateTag, TagValueChangedEventArgs args)
        {
            if ((short)dataChannelStateTag.Value == (short)DataStateEnum.DataWritten)
            {
                if (DataReceived != null)
                {
                    int iIdx = 0;
                    try
                    {
                        _channelToLineTag.ClearValue();
                        _tagListener.ReadTagSynchronously(_channelToLineTag);
                        iIdx++;
                    }
                    catch (PlcCommunicationException e)
                    {
                        RaiseCommunicationProblem(e);
                    }

                    if (DataReceived != null)
                    {
                        if (_channelToLineTag.Value == null)
                        {
                            RaiseCommunicationProblem(new PlcCommunicationException("Can't listen to DataChannel " + _channelToLineTag.Name + " because value is null for DataType " + _channelToLineTag.DataType, _tagListener.AddressAndPath, ""));
                        }

                        DataReceived(this, (TDataType)_channelToLineTag.Value);
                    }
                }

                try
                {
                    _tagController.WriteTag(dataChannelStateTag, (short)DataStateEnum.DataChannelFree);
                }
                catch (PlcCommunicationException e)
                {
                    RaiseCommunicationProblem(e);
                }
            }
        }