Welcome Guest ( Login | Register )



All times are UTC - 7 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: How to Get Tag Value using string to Tag Function ?
PostPosted: Fri Jan 15, 2016 4:31 am 

Joined: Fri Jan 15, 2016 4:00 am
Posts: 3

Offline
Hello,

I am using string to tag function given below and wanted to show tag value in message box on button click event. "value1" is my Tag Name.
When i click on button it shows error in the log file as
Object reference not set to an instance of an object.
Below is my Code

Code:
void Button1_Click(System.Object sender, System.EventArgs e)
      {
       MessageBox.Show(StringToTag("value1").Value);
      
         
      }
      // returns null if tag not found
      public GlobalDataItem StringToTag(string tagName)
      {
         string typeName = (new GlobalDataItem()).GetType().Name;
         PropertyInfo[] props = Globals.Tags.GetType().GetProperties();
         foreach (PropertyInfo prop in props) {
            if (prop.PropertyType.Name.Equals(typeName)) {
               if (prop.Name.Equals(tagName, StringComparison.CurrentCultureIgnoreCase)) {
                  return (GlobalDataItem)prop.GetValue(Globals.Tags, null);
               }               
            }
         }
         return null;
      }


 Profile  
 
 Post subject: Re: How to Get Tag Value using string to Tag Function ?
PostPosted: Tue Mar 08, 2016 1:19 am 

Joined: Mon Apr 08, 2013 11:50 am
Posts: 13

Offline
The problem is that the StringToTag don't work!
It will always return null.

You should never do:
Code:
MessageBox.Show(StringToTag("value1").Value);

When you have a function that can return null.

You need to split it into several lines

Code:
GlobalDataItem item = StringToTag("value1");
if(item!=null)
{
  // here you might want to check if the tag has any value...
  MessageBox.Show(item.Value);
}


But at the moment this will not work since the StringToTag function always returns null...

// Anders


 Profile  
 
 Post subject: Re: How to Get Tag Value using string to Tag Function ?
PostPosted: Tue Mar 08, 2016 3:08 pm 

Joined: Mon Jun 11, 2012 2:10 pm
Posts: 83

Offline
After doing a lot of digging I discovered iX 2.20 no longer creates tags as properties. They are now created as fields (public variables). This seems to have been to reduce baggage created by defining tags as properties. I also found iX has two types of tags it can create: GlobalDataItem and LightweightTag. Although these classes are similar, they cannot be recast to the other type. To deal with this, I created a generic StringToTag function

In the Tags script, add the following static function
Code:
public static T StringToTag<T>(string tagName) where T : class
{
   Type type = typeof(T);
   FieldInfo[] props = Globals.Tags.GetType().GetFields();
   foreach (FieldInfo prop in props) {
      if (prop.FieldType.Equals(type)) {
         if (prop.Name.Equals(tagName, StringComparison.CurrentCultureIgnoreCase)) {
            return (T)prop.GetValue(Globals.Tags);
         }               
      }
   }
   return null;
}

Do not forget to add "using System.Reflection;" to head of Tags script.

As an example of how to use the function, I created a sample screen named Screen1 with a single button, script for screen shown below
Code:
//--------------------------------------------------------------
// Press F1 to get help about using script.
// To access an object that is not located in the current class, start the call with Globals.
// When using events and timers be cautious not to generate memoryleaks,
// please see the help for more information.
//---------------------------------------------------------------

namespace Neo.ApplicationFramework.Generated
{
    using System.Windows.Forms;
    using System;
    using System.Drawing;
    using Neo.ApplicationFramework.Tools;
    using Neo.ApplicationFramework.Common.Graphics.Logic;
    using Neo.ApplicationFramework.Controls;
    using Neo.ApplicationFramework.Interfaces;
   using GlobalDataItem = Neo.ApplicationFramework.Tools.OpcClient.GlobalDataItem;
   using LightweightTag = Neo.ApplicationFramework.Tools.OpcClient.LightweightTag;
   
    public partial class Screen1
    {
      
      void Button_Click(System.Object sender, System.EventArgs e)
      {
         GlobalDataItem tag = Tags.StringToTag<GlobalDataItem>("Tag1");
         if (tag == null)
         {
            LightweightTag tag2 = Tags.StringToTag<LightweightTag>("Tag1");
            if (tag2 == null)
            {
               MessageBox.Show("Error");
            }
            else
            {
               MessageBox.Show(tag2.Value.ToString());
            }
         }
         else
         {
            MessageBox.Show(tag.Value.ToString());
         }
      }
    }
}


If you only want to use the tag's "Value" property, you could also add the following to the Tags script to reduce coding elsewhere:
Code:
public VariantValue this[string tagName]
{
   get
   {
      GlobalDataItem GDI = Tags.StringToTag<GlobalDataItem>(tagName);
      if (GDI == null)
      {
         LightweightTag LWT = Tags.StringToTag<LightweightTag>(tagName);
         if (LWT == null)
            throw new NotSupportedException(tagName);
         else
            return LWT.Value;
      }
      else
         return GDI.Value;
   }
   set
   {
      GlobalDataItem GDI = Tags.StringToTag<GlobalDataItem>(tagName);
      if (GDI == null)
      {
         LightweightTag LWT = Tags.StringToTag<LightweightTag>(tagName);
         if (LWT == null)
            throw new NotSupportedException(tagName);
         else
            LWT.Value = value;
      }
      else
         GDI.Value = value;
   }
}


This will enable you to use the code "Globals.Tags["Tag1"]" to read/write to Tag1. You may want to add a catch block just in case the function throws a "NotSupportedException". (Note: you will need to add the using remarks for GlobalDataItem and LightweightTag as in the Screen1 example in the Tags script.)

Hope this helps.

_________________
Adam M.
Controls Engineer
FlexEnergy


 Profile  
 
 Post subject: Re: How to Get Tag Value using string to Tag Function ?
PostPosted: Wed Mar 09, 2016 1:16 am 

Joined: Mon Apr 08, 2013 11:50 am
Posts: 13

Offline
Wow! That must have taken a while to figure out!

Thank you very much!

// Anders


 Profile  
 
 Post subject: Re: How to Get Tag Value using string to Tag Function ?
PostPosted: Thu Jun 01, 2017 3:48 am 

Joined: Tue Nov 13, 2012 12:25 pm
Posts: 15

Offline
Good morning,
I tried to use the ValueChange event of a LightweightTag in a script but it does not seem to work, someone has already had some experience on it?
Thank you.


 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 7 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: