Welcome Guest ( Login | Register )



All times are UTC - 7 hours [ DST ]



Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Writing tag values to a file
PostPosted: Fri Nov 09, 2012 6:24 pm 

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

Offline
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?


 Profile  
 
 Post subject: Re: Writing tag values to a file
PostPosted: Mon Nov 12, 2012 9:45 am 

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

Offline
Check out this forum post on how to dynamically reference tags using C#.

_________________
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer


 Profile  
 
 Post subject: Re: Writing tag values to a file
PostPosted: Mon Nov 12, 2012 12:30 pm 

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

Offline
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?


 Profile  
 
 Post subject: Re: Writing tag values to a file
PostPosted: Mon Nov 12, 2012 1:22 pm 

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

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


Code:
Globals.Tags.MyTag.Read();

_________________
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer


 Profile  
 
 Post subject: Re: Writing tag values to a file
PostPosted: Mon Nov 12, 2012 2:11 pm 

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

Offline
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?.


 Profile  
 
 Post subject: Re: Writing tag values to a file
PostPosted: Mon Nov 12, 2012 2:23 pm 

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

Offline
" 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.


 Profile  
 
 Post subject: Re: Writing tag values to a file
PostPosted: Tue Nov 13, 2012 8:54 am 

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

Offline
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


 Profile  
 
 Post subject: Re: Writing tag values to a file
PostPosted: Tue Nov 13, 2012 2:24 pm 

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

Offline
Thanks Mark,
I found out how to do it!


 Profile  
 
 Post subject: Re: Writing tag values to a file
PostPosted: Wed Nov 14, 2012 2:26 pm 

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

Offline
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:
   // Additional Usings
   using System.Collections.Generic;
   using System.Reflection;
   using Neo.ApplicationFramework.Tools.OpcClient;


GetTags Method...

Code:
      /// <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:
         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)

Attachment:
DynamicTagList.png
DynamicTagList.png [ 20.67 KiB | Viewed 12749 times ]

_________________
Best Regards,
Patrick Hall


 Profile  
 
 Post subject: Re: Writing tag values to a file
PostPosted: Sat Nov 17, 2012 4:23 pm 

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

Offline
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.


 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next

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: