LightweightTag indexing

Post Reply
Aurimas94
Posts: 6
Joined: Mon Nov 18, 2019 4:12 am

LightweightTag indexing

Post by Aurimas94 »

Hello iX developers!
Spent almost 3 weeks on this so nearly gave up.. Problem is that I want to read and write array tags like:
tag1[0] = 10;
I have Tags numbered like:
Tags.PNG
Tags.PNG (13 KiB) Viewed 13895 times
So I need to have lets say 10 tags with array size of 3 (type of INT). Then I need to loop through each of Tag and Tag index like for example:

Code: Select all

Globals.Tags.tagName[indx] = 10;
To explain more, I was able to get FieldInfo of all Tag names which contains a string "Tag" at the beginning. Then By using LightweightTag and method defined in Tags script window "StringToTag<>"

Code: Select all

LightweightTag tag2 = Tags.StringToTag<LightweightTag>(tagField[i].Name.ToString());

where:
tagField.Name.ToString()
sets the available Tag name.

So if I try to set the value like:

Code: Select all

tag2[0].Value = 10;
it gives me an error:
error.PNG
error.PNG (6.49 KiB) Viewed 13895 times
I attached my project.
Any Help will be appreciated!

Aurimas
Attachments
testProj.zip
(30.19 KiB) Downloaded 777 times

User avatar
Russ C.
Posts: 213
Joined: Thu Nov 16, 2017 3:32 pm
Contact:

Re: LightweightTag indexing

Post by Russ C. »

You can access array tags index, by using this format:

Code: Select all

Globals.Tags.Tag1.Values[0].Value = 1;
Best regards,

Russ
(801) 708-6690
Technical Support
Contact Us

Beijer Electronics AB
http://www.beijerelectronics.us

Aurimas94
Posts: 6
Joined: Mon Nov 18, 2019 4:12 am

Re: LightweightTag indexing

Post by Aurimas94 »

Thanks for the reply.
I have tried this format earlier, but it gives compilation error like below:
error1.PNG
error1.PNG (92.89 KiB) Viewed 13871 times
So it seems that "LightweightTags" cannot be used like that. However when using GlobalDataItem like:

Code: Select all

FieldInfo[] tagField = typeof(Neo.ApplicationFramework.Generated.Tags).GetFields(BindingFlags.Public|BindingFlags.Instance);
				for(int i = 0; i < tagField.Length; i++)
				{
					if(tagField[i].Name.Contains("Tag"))
					{
						GlobalDataItem tag = Tags.StringToTag<GlobalDataItem>(tagField[i].Name.ToString());
						tag.Values[0].Value = 10;
						if (tag == null)
						{
						/*	
							LightweightTag tag2 = Tags.StringToTag<LightweightTag>(tagField[i].Name.ToString());
							
							tag2.Values[0].Value=10;
							//Globals.Tags.Tag1.Values[0].Value = 10;
							if (tag2 == null)
							{
								MessageBox.Show("Error");
							}
							else
							{	
								MessageBox.Show(tag2.Value.ToString());
							}
						*/	
						}
						else
						{
							MessageBox.Show(tag.Value.ToString());
						}
						
					}
					else 
						return;
				}
It compiles and even shows one of the Tag values yet there is 3 of them...
I don't really understand difference of LightweightTag and "normal" tag... How can I make all of them the same, lets say not the LightweightTag?
Thanks.

User avatar
Russ C.
Posts: 213
Joined: Thu Nov 16, 2017 3:32 pm
Contact:

Re: LightweightTag indexing

Post by Russ C. »

The "LightWeight" tag means that tag isn't doesn't have any additional properties for being used in special cases such as: Non-Volatile, has a Gain or Offset, Data Exchange, Array, etc...

So your issue is your forcing it to always be a LightWeightTag which doesn't have a "Values" property because LightWeightTags can't be an ArrayTag.

Code: Select all

LightweightTag tag2 = Tags.StringToTag<LightweightTag>(tagField[i].Name.ToString());
Why are you using GlobalDataItem on the lines above and LightWeightTag on this one?

Have you already tried using it like:

Code: Select all

GlobalDataItem tag2 = Tags.StringToTag<GlobalDataItem>(tagField[i].Name.ToString());
Best regards,

Russ
(801) 708-6690
Technical Support
Contact Us

Beijer Electronics AB
http://www.beijerelectronics.us

Aurimas94
Posts: 6
Joined: Mon Nov 18, 2019 4:12 am

Re: LightweightTag indexing

Post by Aurimas94 »

Last time I tried I couldn't get array values while using:

Code: Select all

GlobalDataItem tag2 = Tags.StringToTag<GlobalDataItem>(tagField[i].Name.ToString());
Today I noticed really stupid mistake...
Instead of passing in MessageBox the same array index as I am writing to I was passing nothing, so resulting value always equals to 0... :roll:

All in all, that was my mistake and I was sooo close :lol:
I really appreciate your help, Russ C. You are the man!

I attached project files if someone come across the same issue.
Tag_Name_Index_Example.zip
(29.19 KiB) Downloaded 876 times
And here is the results:
Capture.PNG
Capture.PNG (5.99 KiB) Viewed 13863 times
cheers! :D

User avatar
Russ C.
Posts: 213
Joined: Thu Nov 16, 2017 3:32 pm
Contact:

Re: LightweightTag indexing

Post by Russ C. »

Awesome!! Glad you were able to get it up and running!! :D :D
Best regards,

Russ
(801) 708-6690
Technical Support
Contact Us

Beijer Electronics AB
http://www.beijerelectronics.us

Post Reply