Пример #1
0
 public void UpdateTag(UInt32 TagHandle, Object Value, TagQuality Quality)
 {
     if (!OPCSvr.UpdateTag(TagHandle, Value, Quality))
     {
         throw new OPCSvrException("<ERROR> Update Tag Failed!");
     }
 }
Пример #2
0
        internal void SetToSubscription(int MaxSubscriptionFrequency, int MaxDelayToFireEvent, Action <ITag> OnTagValueChangeAction = null)
        {
            try
            {
                if (ClientBound != null && !IsSubscribed && this.IsAvailable)
                {
                    //TODO
                    //_NotificationHandler = ClientBound.ClientUA.SubscribeDataChange(
                    //    ClientBound.Address,
                    //    this.Address,
                    //    MaxSubscriptionFrequency,
                    //    (sender, eventArgs) => GetSubscriptionValue(eventArgs));


                    _OnTagValueChange = OnTagValueChangeAction;
                    Subscription_MaxEventFrequency = MaxSubscriptionFrequency;
                    //Subscription_MaxDelayToFireEvent = MaxDelayToFireEvent;
                    IsSubscribed = true;
                }
            }
            catch (Exception ex)
            {
                Quality = TagQuality.ERROR;
                ClientBound.Logger?.LogException($"OPC - Tag Subscription error. Address: {this.Address}. Ex: {ex.Message}");
            }
        }
Пример #3
0
        internal bool UpdateValue(byte[] value, uint byteOffset = 0)
#endif
        {
            if (!Enabled)
            {
                return(false);
            }

            //The structure is always a 2 byte type code followed by the
            //actual data...
            ushort typeCode = BitConverter.ToUInt16(value, 0);

            if (_dataType == 0)
            {
                //Setting the type code for the first time...
                _dataType = typeCode;
            }
            else
            {
                if (_dataType != typeCode)
                {
                    _tagQuality      = TagQuality.Bad;
                    _lastError       = Resources.ErrorStrings.TypeMismatch + "0x" + typeCode.ToString("X2");
                    _lastErrorNumber = (int)LogixErrors.TypeMismatch;
                    throw new Exception(Resources.ErrorStrings.TypeMismatch + "0x" + typeCode.ToString("X2"));
                }
            }
            byte[] temp = new byte[value.Length - 2];

            Buffer.BlockCopy(value, 2, temp, 0, temp.Length);

            _timeStamp = DateTime.Now;

            return(OnDataUpdated(temp, byteOffset));
        }
Пример #4
0
 internal bool UpdateTag(ServiceResult result, object val = null)
 {
     if (StatusCode.IsGood(result.StatusCode))
     {
         if (val != null)
         {
             Value = val;
         }
         Quality    = TagQuality.GOOD;
         LastUpdate = DateTime.Now;
         return(true);
     }
     else if (StatusCode.IsUncertain(result.StatusCode))
     {
         Quality    = TagQuality.UNKNOWN;
         LastUpdate = DateTime.Now;
         return(false);
     }
     else
     {
         Quality    = TagQuality.NOT_AVAILABLE;
         LastUpdate = DateTime.Now;
         return(false);
     }
 }
Пример #5
0
        public bool WriteItem(object ValueToWrite)
        {
            if (ClientBound != null && ClientBound.IsConnected && ClientBound.IsAvailable && this.IsAvailable)
            {
                try
                {
                    var       MyValueCasted = Convert.ChangeType(ValueToWrite, TagType);
                    DataValue val           = new DataValue();
                    val.Value = MyValueCasted;


                    List <WriteValue> nodesToWrite = new List <WriteValue>();
                    nodesToWrite.Add(new WriteValue()
                    {
                        NodeId      = new NodeId(Address),
                        AttributeId = Attributes.Value,
                        Value       = val
                    });
                    WriteValueCollection     nodesToWriteCollection = new WriteValueCollection(nodesToWrite);
                    StatusCodeCollection     results;
                    DiagnosticInfoCollection diagnosticInfos;

                    RequestHeader requestHeader = new RequestHeader();
                    requestHeader.ReturnDiagnostics = 0;

                    ResponseHeader RS = ClientBound.ClientSession.Write(
                        requestHeader,
                        nodesToWriteCollection,
                        out results,
                        out diagnosticInfos);

                    StatusCode code = results.First();
                    if (StatusCode.IsGood(code))
                    {
                        Quality    = TagQuality.GOOD;
                        LastUpdate = DateTime.Now;
                        return(true);
                    }
                    else
                    {
                        Quality    = TagQuality.UNKNOWN;
                        LastUpdate = DateTime.Now;
                        ClientBound.Logger?.LogWarning($"OPC - Write Error on: {this.Name} @ {this.Address}. Reason: {code.ToString()}");
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    Quality = TagQuality.ERROR;
                    ClientBound.Logger?.LogWarning($"OPC - Write Error on: {this.Name} @ {this.Address}. Ex: {ex.Message}");
                    return(false);
                }
            }
            else
            {
                Quality = TagQuality.DISCONNECTED;
                return(false);
            }
        }
Пример #6
0
        public void AddTag(String TagName, object TagValue, String TagDescr, TagQuality TagInitialQuality, bool IsWritable)
        {
            UInt32 retval;

            retval = OPCSvr.CreateTag(TagName, TagValue, TagDescr, TagInitialQuality, IsWritable);
            if (retval <= 0)
            {
                throw new OPCSvrException("<ERROR> Add Tag Failed!");
            }
        }
Пример #7
0
 /// <summary>
 /// Sets value received from plc to tag.
 /// Uses scaling if it needs.
 /// Updates timestamps.
 /// </summary>
 /// <param name="value"></param>
 /// <param name="qty"></param>
 internal void SetValue(object value, TagQuality qty = TagQuality.GOOD)
 {
     Quality = qty;
     Last    = DateTime.Now.Ticks;
     if (qty == TagQuality.GOOD)
     {
         Value    = Scale?.ScaleDevToSrv(value) ?? value;
         LastGood = Last;
     }
 }
Пример #8
0
        public bool UpdateTag(UInt32 TagHandle, Object Value, TagQuality Quality)
        {
            bool flag = WtOPCInterface.UpdateTag(TagHandle, Value, (UInt16)Quality);

            //if (flag)
            //{
            //    this.tagList.FindTag(TagHandle).TagValue = Value;
            //}
            return(flag);
        }
Пример #9
0
        public bool UpdateTagByName(String Name, Object Value, TagQuality Quality)
        {
            bool flag = WtOPCInterface.UpdateTagByName(Name, Value, (UInt16)Quality);

            if (flag)
            {
                //为什么只更新TagValue?
                this.tagList.FindTag(Name).TagValue = Value;
            }
            return(flag);
        }
Пример #10
0
 /// <summary>
 /// Creates a new <see cref="LogixTag"/> with the specified TagAddress.
 /// </summary>
 /// <param name="TagAddress">Address (Tag Name) of the item on the Processor.</param>
 internal LogixTag(string TagAddress)
 {
     _enabled         = true;
     _parent          = null;
     _dataType        = 0x00;
     _address         = TagAddress;
     _lastError       = string.Empty;
     _lastErrorNumber = 0;
     _tagQuality      = TagQuality.Unknown;
     _timeStamp       = DateTime.MinValue;
 }
Пример #11
0
        /// <summary>
        /// Creates a new <see cref="LogixTag"/> with the specified TagAddress and adds it to the <see cref="T:LogixProcessor"/>.
        /// </summary>
        /// <param name="TagAddress">Address (Tag Name) of the item on the Processor.</param>
        /// <param name="Processor">Processor to add this <c>LogixTag</c> to.</param>
        /// <param name="ElementCount"></param>
        internal LogixTag(string TagAddress, LogixProcessor Processor, ushort ElementCount, object InitData = null)
#endif
        {
            _enabled         = true;
            _parent          = Processor;
            _dataType        = 0x00;
            _address         = TagAddress;
            _lastError       = string.Empty;
            _lastErrorNumber = 0;
            _tagQuality      = TagQuality.Unknown;
            _timeStamp       = DateTime.MinValue;
            _elements        = ElementCount;
            Initialize(InitData);
            //Processor.AddTag(this);
        }
Пример #12
0
        internal OPCTag(OPCClient refOPCClient, string name, string address, Type type, int?tagTypeSize = null, int elementN = 1)
        {
            //Reference to twincat client and to OPC client
            ClientBound = refOPCClient;

            _Quality = TagQuality.INIT;

            _Name        = string.IsNullOrWhiteSpace(name) ? _Address : name;
            _TagType     = type;
            _TagTypeSize = tagTypeSize;
            _ElementN    = elementN;

            _Address = address;

            CheckItem();
        }
Пример #13
0
        public UInt32 CreateTag(String TagName, object TagValue, String TagDescr, TagQuality TagInitialQuality, bool IsWritable)
        {
            UInt32 num = WtOPCInterface.CreateTag(TagName, TagValue, (UInt16)TagInitialQuality, IsWritable);

            if (num > 0)
            {
                TagInfo tagInfo = new TagInfo
                {
                    TagHandle  = num,
                    TagName    = TagName,
                    TagQuality = TagInitialQuality,
                    TagValue   = TagValue,
                    TagDescr   = TagDescr
                };
                this.tagList.Add(tagInfo);
            }
            return(num);
        }
Пример #14
0
 public bool ReadItem()
 {
     if (ClientBound != null && ClientBound.IsConnected && ClientBound.IsAvailable)
     {
         try
         {
             //var resss = ClientBound.ClientSession.ReadValue(new NodeId(Address), TagType);
             DataValue result = ClientBound.ClientSession.ReadValue(new NodeId(Address));
             if (StatusCode.IsGood(result.StatusCode))
             {
                 Value      = result.Value;
                 TagType    = result.Value.GetType();
                 Quality    = TagQuality.GOOD;
                 LastUpdate = result.SourceTimestamp;
                 return(true);
             }
             else if (StatusCode.IsUncertain(result.StatusCode))
             {
                 Quality    = TagQuality.UNKNOWN;
                 LastUpdate = result.SourceTimestamp;
                 return(false);
             }
             else
             {
                 Quality    = TagQuality.NOT_AVAILABLE;
                 LastUpdate = result.SourceTimestamp;
                 return(false);
             }
         }
         catch (Exception ex)
         {
             Quality = TagQuality.ERROR;
             ClientBound.Logger?.LogWarning($"OPC - Read Error on: {this.Name} @ {this.Address}. Ex: {ex.Message}");
             return(false);
         }
     }
     else
     {
         Quality = TagQuality.DISCONNECTED;
         return(false);
     }
 }
Пример #15
0
        internal void RemoveFromSubscription()
        {
            try
            {
                //TODO
                //if (ClientBound != null && IsSubscribed && this.IsAvailable)
                //{
                //    ClientBound.ClientUA.UnsubscribeMonitoredItem(_NotificationHandler);

                //    _NotificationHandler = 0;
                //    IsSubscribed = false;
                //    Subscription_MaxEventFrequency = null;
                //    Subscription_MaxDelayToFireEvent = null;
                //}
            }
            catch (Exception ex)
            {
                Quality = TagQuality.ERROR;
                ClientBound.Logger?.LogException($"OPC - Failed to remove Subscription. Address: {this.Address}. Ex: {ex.Message}");
            }
        }
Пример #16
0
        //private bool SetTagAddress(string address)
        //{
        //    //get Namespace
        //    string[] TagAddress = address.Split(';');
        //    if (TagAddress.Length < 2 || (address.Count(f => f == '=')!=2))
        //    {
        //        //WRONG ADDRESS TYPE
        //        IsAvailable = false;
        //        _Address = address;
        //        ClientBound.Logger?.LogException($"OPC - Tag creation error on: {this.Name} @ {this.Address}. Address must be in the format: ns=x, s=xxx");
        //        return false;
        //    }
        //    else
        //    {
        //        string[] NamespacePart = TagAddress[0].Split('=');
        //        if (NamespacePart.Length >= 2)
        //        {
        //            int NS = 0;
        //            if (Int32.TryParse(NamespacePart[1], out NS))
        //            {
        //                _NameSpaceIndex = (ushort)NS;
        //            }
        //        }
        //        string[] AddressPart = TagAddress[1].Split('=');
        //        if (AddressPart.Length >= 2)
        //        {
        //            _Address = AddressPart[1];
        //        }
        //        return true;
        //    }
        //}

        #endregion

        #region Tag Subscription

        internal void OnSubscriptionNotification(MonitoredItem item, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                foreach (var value in item.DequeueValues())
                {
                    //Update tag
                    if (StatusCode.IsGood(value.StatusCode))
                    {
                        Value      = value.Value;
                        LastUpdate = value.SourceTimestamp;
                        Quality    = TagQuality.GOOD;
                        //Execute external event
                        _OnTagValueChange?.Invoke(this);
                    }
                    else
                    {
                        if (StatusCode.IsUncertain(value.StatusCode))
                        {
                            Quality = TagQuality.UNKNOWN;
                        }
                        else if (StatusCode.IsBad(value.StatusCode))
                        {
                            Quality = TagQuality.ERROR;
                        }
                        else
                        {
                            Quality = TagQuality.UNKNOWN;
                        }
                        ClientBound.Logger?.LogWarning($"OPCTag {this.Name} quality's {this.Quality}");
                    }
                }
            }
            catch (Exception ex)
            {
                ClientBound.Logger?.LogException($"OPC - Tag Subscription Error. Address: {this.Address}. Ex: {ex.Message}");
            }
        }
Пример #17
0
 internal void CheckItem()
 {
     try
     {
         //Try to check if Tag is available
         if ((ClientBound.ClientSession != null) && ClientBound.IsConnected)
         {
             //Try to read item to check if is available
             IsAvailable = this.ReadItem();
         }
         else
         {
             //Tag always available if online
             IsAvailable = true;
         }
     }
     catch (Exception ex)
     {
         Quality = TagQuality.NOT_AVAILABLE;
         ClientBound.Logger?.LogException($"OPC - Tag creation error on: {this.Name} @ {this.Address}. Ex: {ex.Message}");
         IsAvailable = false;
     }
 }
Пример #18
0
 private void timer_Elapsed(object sender, ElapsedEventArgs e)
 {
     foreach (MoxaIPNode node in this.nodes)
     {
         TagQuality lastQuality = TagQuality.Bad;
         if (node.IsAlive)
         {
             lastQuality = TagQuality.Good;
         }
         else
         {
             if (this.nodeIsNotResponding != null)
             {
                 this.nodeIsNotResponding(this, new NodeEventArgs {
                     Node = node
                 });
             }
         }
         foreach (MoxaTag tag in node.Tags)
         {
             tag.Quality = lastQuality;
         }
     }
 }
Пример #19
0
        internal void SetTagError(byte[] value)
        {
            TagQuality oldQuality = _tagQuality;

            switch (value[0])
            {
            case 0x04:
                _lastError       = Resources.ErrorStrings.MalformedIOI;
                _lastErrorNumber = (int)LogixErrors.MalformedIOI;
                _tagQuality      = TagQuality.Bad;
                break;

            case 0x05:
                _lastError       = Resources.ErrorStrings.ItemNotFound;
                _lastErrorNumber = (int)LogixErrors.ItemNotFound;
                _tagQuality      = TagQuality.Bad;
                break;

            case 0x0A:
                _lastError       = Resources.ErrorStrings.AttributeError;
                _lastErrorNumber = (int)LogixErrors.AttributeError;
                _tagQuality      = TagQuality.Bad;
                break;

            case 0x13:
                _lastError       = Resources.ErrorStrings.NotEnoughData;
                _lastErrorNumber = (int)LogixErrors.NotEnoughData;
                _tagQuality      = TagQuality.Bad;
                break;

            case 0x1C:
                _lastError       = Resources.ErrorStrings.InsufficientAttributes;
                _lastErrorNumber = (int)LogixErrors.InsufficientAttributes;
                _tagQuality      = TagQuality.Bad;
                break;

            case 0x26:
                _lastError       = Resources.ErrorStrings.InvalidIOILength;
                _lastErrorNumber = (int)LogixErrors.InvalidIOILength;
                _tagQuality      = TagQuality.Bad;
                break;

            case 0xFF:
                if (value.Length != 4)
                {
                    _lastError       = Resources.ErrorStrings.Unknown;
                    _lastErrorNumber = (int)LogixErrors.Unknown;
                    _tagQuality      = TagQuality.Bad;
                }
                else
                {
                    switch (value[2])
                    {
                    case 0x05:
                        _lastError       = Resources.ErrorStrings.AccessBeyondObject;
                        _lastErrorNumber = (int)LogixErrors.AccessBeyondObject;
                        _tagQuality      = TagQuality.Bad;
                        break;

                    case 0x07:
                        _lastError       = Resources.ErrorStrings.AbbreviatedTypeError;
                        _lastErrorNumber = (int)LogixErrors.AbbreviatedTypeError;
                        _tagQuality      = TagQuality.Bad;
                        break;

                    case 0x04:
                        _lastError       = Resources.ErrorStrings.BeginOffsetError;
                        _lastErrorNumber = (int)LogixErrors.BeginOffsetError;
                        _tagQuality      = TagQuality.Bad;
                        break;

                    default:
                        _lastError       = Resources.ErrorStrings.Unknown;
                        _lastErrorNumber = (int)LogixErrors.Unknown;
                        _tagQuality      = TagQuality.Bad;
                        break;
                    }
                }
                break;

            case 0x06:
            case 0x00:
                _lastErrorNumber = 0x00;
                _lastError       = string.Empty;
                _tagQuality      = TagQuality.Good;
                break;

            default:
                _lastError       = Resources.ErrorStrings.Unknown;
                _lastErrorNumber = (int)LogixErrors.Unknown;
                _tagQuality      = TagQuality.Bad;
                break;
            }

            if (_tagQuality != oldQuality)
            {
                RaiseTagQualityChanged(this);
            }
        }
Пример #20
0
        internal bool UpdateValue(byte[] value, uint byteOffset = 0)
#endif
		{
            if (!Enabled)
                return false;

            //The structure is always a 2 byte type code followed by the
            //actual data...
            ushort typeCode = BitConverter.ToUInt16(value, 0);

            if (_dataType == 0)
            {
                //Setting the type code for the first time...
                _dataType = typeCode;
            }
            else
            {
                if (_dataType != typeCode)
                {
                    _tagQuality = TagQuality.Bad;
                    _lastError = Resources.ErrorStrings.TypeMismatch + "0x" + typeCode.ToString("X2");
                    _lastErrorNumber = (int)LogixErrors.TypeMismatch;
                    throw new Exception(Resources.ErrorStrings.TypeMismatch + "0x" + typeCode.ToString("X2"));
                }
            }
            byte[] temp = new byte[value.Length - 2];

            Buffer.BlockCopy(value, 2, temp, 0, temp.Length);

            _timeStamp = DateTime.Now;

            return OnDataUpdated(temp, byteOffset);
        }
Пример #21
0
        /// <summary>
        /// Creates a new <see cref="LogixTag"/> with the specified TagAddress and adds it to the <see cref="T:LogixProcessor"/>.
        /// </summary>
        /// <param name="TagAddress">Address (Tag Name) of the item on the Processor.</param>
        /// <param name="Processor">Processor to add this <c>LogixTag</c> to.</param>
        /// <param name="ElementCount"></param>
        internal LogixTag(string TagAddress, LogixProcessor Processor, ushort ElementCount, object InitData = null)
#endif
		{
            _enabled = true;
            _parent = Processor;
            _dataType = 0x00;
            _address = TagAddress;
            _lastError = string.Empty;
            _lastErrorNumber = 0;
            _tagQuality = TagQuality.Unknown;
            _timeStamp = DateTime.MinValue;
            _elements = ElementCount;
            Initialize(InitData);
            //Processor.AddTag(this);
        }
Пример #22
0
        internal void SetTagError(byte[] value)
        {
            TagQuality oldQuality = _tagQuality;

            switch (value[0])
            {
                case 0x04:
                    _lastError = Resources.ErrorStrings.MalformedIOI;
                    _lastErrorNumber = (int)LogixErrors.MalformedIOI;
                    _tagQuality = TagQuality.Bad;
                    break;
                case 0x05:
                    _lastError = Resources.ErrorStrings.ItemNotFound;
                    _lastErrorNumber = (int)LogixErrors.ItemNotFound;
                    _tagQuality = TagQuality.Bad;
                    break;
                case 0x0A:
                    _lastError = Resources.ErrorStrings.AttributeError;
                    _lastErrorNumber = (int)LogixErrors.AttributeError;
                    _tagQuality = TagQuality.Bad;
                    break;
                case 0x13:
                    _lastError = Resources.ErrorStrings.NotEnoughData;
                    _lastErrorNumber = (int)LogixErrors.NotEnoughData;
                    _tagQuality = TagQuality.Bad;
                    break;
                case 0x1C:
                    _lastError = Resources.ErrorStrings.InsufficientAttributes;
                    _lastErrorNumber = (int)LogixErrors.InsufficientAttributes;
                    _tagQuality = TagQuality.Bad;
                    break;
                case 0x26:
                    _lastError = Resources.ErrorStrings.InvalidIOILength;
                    _lastErrorNumber = (int)LogixErrors.InvalidIOILength;
                    _tagQuality = TagQuality.Bad;
                    break;
                case 0xFF:
                    if (value.Length != 4)
                    {
                        _lastError = Resources.ErrorStrings.Unknown;
                        _lastErrorNumber = (int)LogixErrors.Unknown;
                        _tagQuality = TagQuality.Bad;
                    }
                    else
                    {
                        switch (value[2])
                        {
                            case 0x05:
                                _lastError = Resources.ErrorStrings.AccessBeyondObject;
                                _lastErrorNumber = (int)LogixErrors.AccessBeyondObject;
                                _tagQuality = TagQuality.Bad;
                                break;
                            case 0x07:
                                _lastError = Resources.ErrorStrings.AbbreviatedTypeError;
                                _lastErrorNumber = (int)LogixErrors.AbbreviatedTypeError;
                                _tagQuality = TagQuality.Bad;
                                break;
                            case 0x04:
                                _lastError = Resources.ErrorStrings.BeginOffsetError;
                                _lastErrorNumber = (int)LogixErrors.BeginOffsetError;
                                _tagQuality = TagQuality.Bad;
                                break;
                            default:
                                _lastError = Resources.ErrorStrings.Unknown;
                                _lastErrorNumber = (int)LogixErrors.Unknown;
                                _tagQuality = TagQuality.Bad;
                                break;
                        }
                    }
                    break;
                case 0x06:
                case 0x00:
                    _lastErrorNumber = 0x00;
                    _lastError = string.Empty;
                    _tagQuality = TagQuality.Good;
                    break;
                default:
                    _lastError = Resources.ErrorStrings.Unknown;
                    _lastErrorNumber = (int)LogixErrors.Unknown;
                    _tagQuality = TagQuality.Bad;
                    break;
            }

            if (_tagQuality != oldQuality)
                RaiseTagQualityChanged(this);
        }
Пример #23
0
        public void UpdateTag(string tagName, object value, TagQuality quanlity)
        {
            TagInfo info = TagList.FindTag(tagName);

            UpdateTag(info.TagHandle, value, quanlity);
        }
Пример #24
0
 /// <summary>
 /// Creates a new <see cref="LogixTag"/> with the specified TagAddress.
 /// </summary>
 /// <param name="TagAddress">Address (Tag Name) of the item on the Processor.</param>
 internal LogixTag(string TagAddress)
 {
     _enabled = true;
     _parent = null;
     _dataType = 0x00;
     _address = TagAddress;
     _lastError = string.Empty;
     _lastErrorNumber = 0;
     _tagQuality = TagQuality.Unknown;
     _timeStamp = DateTime.MinValue;
 }