Writing tag values to a file

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
hans gerritsen
Posts: 26
Joined: Fri Jan 20, 2012 12:55 pm

Re: Writing tag values to a file

Post by hans gerritsen »

While generating a list of the Global.Tags, is there a possibility to find out if the particular tag is marked as a Bit, Bool, string etc.?
Is there also a way to find out if the tag is marked as Read, Write or Readwrite?

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: Writing tag values to a file

Post by mark.monroe »

There is no way that I know of to access the data type or tag access type through scripting.
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

hans gerritsen
Posts: 26
Joined: Fri Jan 20, 2012 12:55 pm

Re: Writing tag values to a file

Post by hans gerritsen »

Ok. Thanks anyway for your help on this subject.

Patrick Hall
Posts: 22
Joined: Fri May 25, 2012 7:44 am
Location: Charlotte, NC. USA

Re: Writing tag values to a file

Post by Patrick Hall »

hans gerritsen wrote:Hi Patrick,

I have already a working version but your solution would much much better!!
But I only need those tags which are ReadWrite or Write and I have to know what kind of type it is (Bool, INT16, etc). I can't find how to do that.

Thanks in advance.
Let us reference back to my previous post [HERE] near the bottom you will find this code snippet...

Code: Select all

         ListBox1.Items.Clear();         
         
         foreach(GlobalDataItem gdi in Globals.Tags.GetTags<GlobalDataItem>()){
            ListBox1.Items.Add(string.Format("{0}={1}",gdi.Name, gdi.Value));
         }
As you are quite aware the line ListBox1.Items.Add(string.Format("{0}={1}",gdi.Name, gdi.Value)); is simply using string format to generate a string from the various properties on a GlobalDataItem [bolded] and in this case Name and Value (of type VariantValue). This is simply calling the properties .ToString() method implicitly.

Other properties of interest are .AccessRight, .DataType, and .GlobalDataType.

To determine if a tag is Read, Write or ReadWrite you can access the AccessRight property which returns an enum of type Neo.ApplicationFramework.Interfaces.AccessRights ... [gdi.AccessRight]
Simply checking if it is AccessRights.Write, or AccessRights.ReadWrite should indicate if the value can be changed as a result of a "load from CSV" (Assuming you get the VariantValue conversion correct).

GlobalDataType and DataType are both enums of type Neo.ApplicationFramework.Interop.DataSource.BEDATATYPE and pertain to columns A and C respectively. Column B is determined by the afformentioned .AccessRight property. see next image.
DynamicTagList_PropCoor.png
DynamicTagList_PropCoor.png (43.11 KiB) Viewed 24564 times
and using this updated bit of code...

Code: Select all

			ListBox1.Items.Clear();			
			
			foreach(GlobalDataItem gdi in Globals.Tags.GetTags<GlobalDataItem>()){
				ListBox1.Items.Add(string.Format("{0,5}={1,15} [{2,12}, {3,10}, {4,12}]",
					gdi.Name,
					gdi.Value,
					gdi.GlobalDataType,
					gdi.AccessRight,
					gdi.DataType));
			}
we see this...
DynamicTagList_v2.png
DynamicTagList_v2.png (15.24 KiB) Viewed 24564 times
as you can see DT_REAL4 is a Float (I would guess DT_REAL8 to be a Double) DT_INTEGER2 is a "INT16" so a DT_INTEGER4 should be an "INT32" etc.

I'm sure with a little trial and error you should be able to figure out the other enumeration options (i.e. DT_* enum values for other data types).
Best Regards,
Patrick Hall

hans gerritsen
Posts: 26
Joined: Fri Jan 20, 2012 12:55 pm

Re: Writing tag values to a file

Post by hans gerritsen »

Perfect!
Thanks a lot Patrick

bjornidar
Posts: 49
Joined: Thu Nov 17, 2011 2:10 am
Location: Norway
Contact:

Re: Writing tag values to a file

Post by bjornidar »

Really helpfull, Patrick :)

hans gerritsen
Posts: 26
Joined: Fri Jan 20, 2012 12:55 pm

Re: Writing tag values to a file

Post by hans gerritsen »

I can read the GlobalDataType, AccessRight etc as described in previous topics.
How can I read if the tag is an array and the size of this array?
For example: tag 'Test' is of DataType=Bool[8]. The array size =8.

rinomasaya
Posts: 1
Joined: Thu Apr 13, 2017 12:07 am

Re: Writing tag values to a file

Post by rinomasaya »

At runtime, the Reflection mechanism uses the Portable Executable file to read information about the assembly and it is possible to uncover the methods, properties, and events of a type, and to invoke them dynamically. Reflection generally begins with a call to a method present on every object in the .NET framework, GetType(). The GetType() is a member of the System.Object class, and the method returns an instance of System.Type.

Type type = refCls.GetType();

The classes in the System.Reflection namespace, together with Type, enable you to get information about loaded assemblies and the types defined within them, such as classes, interfaces, and value types.

Source: .Net Reflection

Rino

monikavd
Posts: 2
Joined: Wed Oct 04, 2017 3:24 am

Re: Writing tag values to a file

Post by monikavd »

Hello!!
What is the path you use to write on the panel? I tried using "C: /" but I can not then access that file via FTP
Can anybody help me?
Thank you!


[quote="hans gerritsen"]

public void SaveSettings(string path)
{
try {
using (StreamWriter Saving = new StreamWriter(path))
{
Saving.WriteLine(Globals.Tags.<tagname>.FullName.ToString()); Saving.WriteLine(Globals.Tags.<tagname>.Value.ToString());
}
}
catch{
MessageBox.Show("Error while saving");
}
}

dthomas
Posts: 14
Joined: Tue Jun 11, 2019 6:06 pm

Re: Writing tag values to a file

Post by dthomas »

I know this is an older thread, however I am running into an issue with trying to use this code - specifically

Code: Select all

public IEnumerable<T> GetTags<T>() where T:GlobalDataItem {

  // Gets PropertyInfo array using reflection
  PropertyInfo[] props = this.GetType().GetProperties();
         
  // Process all PropertyInfo objects found
  foreach (PropertyInfo prop in props) {            
    if (prop.PropertyType == typeof(T)) {
      yield return (T)prop.GetValue(this, null);                  
    }            
  }
}         
I am testing this code exactly as described - yet no tags are added to the listbox. Through some debugging I have found out that
prop.PropertyType is Neo.ApplicationFramework.Tools.OpClient.GlobalDataItems
Wereas
typeof(T) is Neo.ApplicationFramework.Tools.OpClient.GlobalDataItem
When did this change? If i try to change the functions or any of the GlobalDataItem instances to GlobalDataItems the code will not build.

Post Reply