Writing tag values to a file
-
- Posts: 26
- Joined: Fri Jan 20, 2012 12:55 pm
Writing tag values to a file
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?
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?
-
- Posts: 824
- Joined: Tue Mar 13, 2012 9:53 am
Re: Writing tag values to a file
Check out this forum post on how to dynamically reference tags using C#.
Best Regards,
Mark Monroe
Beijer Electronics, Inc. | Applications Engineer
Mark Monroe
Beijer Electronics, Inc. | Applications Engineer
-
- Posts: 26
- Joined: Fri Jan 20, 2012 12:55 pm
Re: Writing tag values to a file
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?
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?
-
- Posts: 824
- Joined: Tue Mar 13, 2012 9:53 am
Re: Writing tag values to a file
Best Regards,
Mark Monroe
Beijer Electronics, Inc. | Applications Engineer
Mark Monroe
Beijer Electronics, Inc. | Applications Engineer
-
- Posts: 26
- Joined: Fri Jan 20, 2012 12:55 pm
Re: Writing tag values to a file
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?.
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?.
-
- Posts: 26
- Joined: Fri Jan 20, 2012 12:55 pm
Re: Writing tag values to a file
" 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.
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.
-
- Posts: 824
- Joined: Tue Mar 13, 2012 9:53 am
Re: Writing tag values to a file
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
Mark Monroe
Beijer Electronics, Inc. | Applications Engineer
-
- Posts: 26
- Joined: Fri Jan 20, 2012 12:55 pm
Re: Writing tag values to a file
Thanks Mark,
I found out how to do it!
I found out how to do it!
-
- Posts: 22
- Joined: Fri May 25, 2012 7:44 am
- Location: Charlotte, NC. USA
Re: Writing tag values to a file
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.
GetTags Method...
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.
Obtaining the Value of each tag in the sequence can be done by using gdi.Value (keep in mind it is of type VariantValue)
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;
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);
}
}
}
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));
}
Best Regards,
Patrick Hall
Patrick Hall
-
- Posts: 26
- Joined: Fri Jan 20, 2012 12:55 pm
Re: Writing tag values to a file
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.
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.