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

Writing tag values to a file

Post by hans gerritsen »

The panel application is being used in several plants. Each plant has it's own settings. I want to make a backup/restore script for all actual settings which are being saved to harddisk or usb of the T21C-panel. (Recipes are unfortunately limited to 256 variable and I need a lot more.)
With the following script I can save settings to a file:

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");
}
}
The problem is that I have to write one scriptline for each tag
Is there a possibility to make a loop which uses an tag[index] to write this info to a file? f.eks. "for (int i=0; i < ....Tags.Count....;i++)"

Or are there easier ways to make a .csv fil with all tagnames with values which can be read into the panel again?

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

Re: Writing tag values to a file

Post by mark.monroe »

Check out this forum post on how to dynamically reference tags using C#.
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 »

Thanks for the links with several solutions.
As I understand the Globals.Tags object isn't a 'list' so it is not possible to loop through them. The other solutions given are based on a tag names which must have a kind of sequence in their names. Unfortunately this is not the case in my situation.

I think I have to create a kind manual taglist myself.
If I write the values of the tags to a file I have to be sure they contain the actual values in the plc. What can I do to make sure the values have been read from the plc before I write them to a file?

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

Re: Writing tag values to a file

Post by mark.monroe »

You can call this function, which will force a tag read.

Code: Select all

Globals.Tags.MyTag.Read();
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 »

Thanks!
Another question:
I can use the script for "converting a string to a tag" (see below) to read a list of tag names from a file and write their actual values to a settings file.

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

I want to be able to read such a settings file back into the panel.
How can I assign a value to a tag via the same method? So instead of using GetValue, can I use SetValue?.

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

Re: Writing tag values to a file

Post by hans gerritsen »

" Globals.Tags.MyTag.Read(); "
Is it possible to use a script with a tag-string here instead of the specific tag name? Something like the script "String to Tag". Else I still have to write a scriptline for every tag name.

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

Re: Writing tag values to a file

Post by mark.monroe »

Yes, it is possible to use a viable as a tag name and then call the Read() function. You need to look at the other forum post I mentioned above and read up on C# reflection. The SetValue is a C# function that is used to set the value of an an object.
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 »

Thanks Mark,
I found out how to do it!

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 »

If you are still interested in iterating or generating a list from the Globals.Tags class at runtime. Here is a Generic method I wrote for a recent project that uses Reflection to generate an IEnumerable<T> that can be used with foreach directly. You can convert it to a list if you like.

You can place the additional usings and paste the member function into the Tags Script window so that GetTags<T>() is a member of the Globals.Tags class.

Code: Select all

	// Additional Usings
	using System.Collections.Generic;
	using System.Reflection;
	using Neo.ApplicationFramework.Tools.OpcClient;
GetTags Method...

Code: Select all

		/// <summary>
		/// Gets an IEnumerable{T} collection of defined tags.
		/// </summary>
		/// <example>
		/// // An iterable collection of GlobalDataItem Tags.
		/// IEnumerable{GlobalDataItem} gTags = Globals.Tags.GetTags{GlobalDataItem}();
		/// 
		/// // An iterable collection of SystemDataItem Tags.
		/// IEnumerable{SystemDataItem} sTags = Globals.Tags.GetTags{SystemDataItem}();
		/// </example>		
		/// <returns>IEnumerable{T}</returns>
		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);					    
				}				
			}			
		}
Code docs show examples of how to get either User created tags, or System Specific Tags (if you actually added any).

If you had a ListBox added to a screen (named ListBox1) you could use the following code to populate the ListBox with the names of all tags found.

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));
			}
Obtaining the Value of each tag in the sequence can be done by using gdi.Value (keep in mind it is of type VariantValue)
DynamicTagList.png
DynamicTagList.png (20.67 KiB) Viewed 36651 times
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 »

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.

Post Reply