iX Talk
http://beijerinc.com/support/ix/forum/

How to Get Tag Value using string to Tag Function ?
http://beijerinc.com/support/ix/forum/viewtopic.php?f=6&t=2029
Page 1 of 1

Author:  ParthDesai [ Fri Jan 15, 2016 4:31 am ]
Post subject:  How to Get Tag Value using string to Tag Function ?

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;
      }

Author:  andis59 [ Tue Mar 08, 2016 1:19 am ]
Post subject:  Re: How to Get Tag Value using string to Tag Function ?

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

Author:  AMitchneck [ Tue Mar 08, 2016 3:08 pm ]
Post subject:  Re: How to Get Tag Value using string to Tag Function ?

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.

Author:  andis59 [ Wed Mar 09, 2016 1:16 am ]
Post subject:  Re: How to Get Tag Value using string to Tag Function ?

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

Thank you very much!

// Anders

Author:  ladin79 [ Thu Jun 01, 2017 3:48 am ]
Post subject:  Re: How to Get Tag Value using string to Tag Function ?

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.

Page 1 of 1 All times are UTC - 7 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/