Page 1 of 2

Get Tag Array Elements by String

Posted: Fri Mar 01, 2013 12:13 pm
by matto
I have many tag arrays:
arrTag_0[0...13]
arrTag_1[0...13]
...
arrTag_n[0...13]

At a certain point, I process each of them as arrTag_j (j is a loop counter from 0 to n) in a loop by using:

Code: Select all

ushort[] arrEvent=new ushort[14];
for (int k=0;k<14;k++) {
	GlobalDataItem eventTag = GetGlobalDataItem("arrTag_" + (j).ToString());
	arrEvent[k]=eventTag[k].value;
}
where the GetGlobalDataItem was a function posted to this forum a long time ago to get a tag from a string.

Except I can't get that to work. I tried eventTag[k].value, eventTag.values[k], evenTag.value[k].Ushort, and I also tried to modify the GetGlobalDataItem function to return the Kth item of a tag.

I need to parse individual registers within that tag array. Any ideas on how to do this? Thank you!

Re: Get Tag Array Elements by String

Posted: Fri Mar 01, 2013 1:29 pm
by mark.monroe
Does the below code work for you? If so, then you have an issue with the way you are creating array names.

Code: Select all

GlobalDataItem eventTag = GetGlobalDataItem("arrTag_0");
Unless you post the error you are getting, there is not much help we can give you.

Regards,
Mark

Re: Get Tag Array Elements by String

Posted: Fri Mar 01, 2013 4:09 pm
by matto
Actually, even better:

Code: Select all

for (int j=0;j<max;j++) {
	GlobalDataItem eventTag = GetGlobalDataItem("arrTag_" + (j).ToString());
}
has worked for me in multiple places in my code. But that GlobalDataItem is an array, and I want each array element of that. eventTag[0], for example.

I guess I am not sure which error to post, so I will post a couple.

Code: Select all

arrEvent[k]=eventTag[k].value;

//'Neo.ApplicationFramework.Interfaces.IGlobalDataSubItem' does not contain a definition for 'value' and no extension method 'value' accepting a first argument of type....

Code: Select all

GlobalDataItem eventTag = GetGlobalDataItem("arrTags_" + (j).ToString());
for (int k=0;k<14;k++) {
	arrEvent[k]=eventTag.Values[k];
}
if (j==0) {
	Globals.Tags.Event2.SetAnalog(arrEvent[0]);
	Globals.Tags.Event3.SetAnalog(arrEvent[1]);
}


//event 2 and 3 always show a value of 0. (tested with a label on a screen), but if I map an additional tag to the first register that the arrTags_0 array also maps to, that additional tag does not show 0 (so I know the arrTags_0[0] is not zero).

Re: Get Tag Array Elements by String

Posted: Mon Mar 04, 2013 10:53 am
by mark.monroe
Value needs to be capitalized, which is why the below code will not work.

Code: Select all

//This will not work
arrEvent[k]=eventTag[k].value;
//This should work
arrEvent[k]=eventTag[k].Value;
Try this:

Code: Select all

//Not this
for (int k=0;k<14;k++) {
   arrEvent[k]=eventTag.Values[k];
//But this
for (int k=0;k<14;k++) {
   arrEvent[k]=eventTag[k].Value;
}

Re: Get Tag Array Elements by String

Posted: Mon Mar 04, 2013 11:01 am
by matto
Thank you for responding. However, its still not working for me:

Code: Select all

			for (int j=0;j<40;j++) {
				ushort[] arrEvent=new ushort[14];
				GlobalDataItem eventTag = GetGlobalDataItem("arr_EventRegisterGroup_" + (j).ToString());
				for (int k=0;k<14;k++) {
					arrEvent[k]=eventTag[k].Values;
				}

//'Neo.ApplicationFramework.Interfaces.IGlobalDataSubItem' does not contain a definition for 'Values' and no extension method 'Values accepting a first argument of type 'Neo.ApplicationFramework.Interfaces.IGlobalDataSubItem' could be found (are you missing a using directive or an assembly reference?)
arrEvent[k]=eventTag[k].Value
compiles. I will see how it behaves

Re: Get Tag Array Elements by String

Posted: Mon Mar 04, 2013 11:18 am
by mark.monroe
Try this:
arrEvent[k]=eventTag[k].Value;

There is no Values.

Re: Get Tag Array Elements by String

Posted: Mon Mar 04, 2013 1:15 pm
by matto
I still am not getting it to work. Let me back up and explain a bit more.

I have multiple tags set up (name @ modbus address):
  • arr_EventRegisterGroup_0 @ 40400...40413
    arr_EventRegisterGroup_1 @ 40414...40427 (irrelevant for this test)
    Event0 @ 40411
    Event1 @ 40412
    Event2 (internal)
    Event3 (internal)
    ...
    Event14 (internal)
    Event15 (internal)
    testTag (internal)
Notice that Event0 and Event1 are the same addresses as 2 of the addresses near the end of arr_EventRegisterGroup_0.

I display all of the Event0 ... Event15 tags and the testTag tag on labels on a screen. I also have a button which triggers the following code:

Code: Select all

			for (int j=0;j<40;j++) {
				ushort[] arrEvent=new ushort[14];
				GlobalDataItem eventTag = GetGlobalDataItem("arr_EventRegisterGroup_" + j.ToString());
				for (int k=0;k<14;k++) {
					arrEvent[k]=eventTag[k].Value;
				}
				if (j==0) {         // test the array values by displaying them
					Globals.Tags.Event2.SetAnalog(arrEvent[0]);
					Globals.Tags.Event3.SetAnalog(arrEvent[1]);
					Globals.Tags.Event4.SetAnalog(arrEvent[2]);
					Globals.Tags.Event5.SetAnalog(arrEvent[3]);
					Globals.Tags.Event6.SetAnalog(arrEvent[4]);
					Globals.Tags.Event7.SetAnalog(arrEvent[5]);
					Globals.Tags.Event8.SetAnalog(arrEvent[6]);
					Globals.Tags.Event9.SetAnalog(arrEvent[7]);
					Globals.Tags.Event10.SetAnalog(arrEvent[8]);
					Globals.Tags.Event11.SetAnalog(arrEvent[9]);
					Globals.Tags.Event12.SetAnalog(arrEvent[10]);
					Globals.Tags.Event13.SetAnalog(arrEvent[11]);
					Globals.Tags.Event14.SetAnalog(arrEvent[12]);
					Globals.Tags.Event15.SetAnalog(arrEvent[13]);
					Globals.Tags.testTag.IncrementAnalog(1);
				}
			}
The label showing Event1 (@ 40412) is a nonzero value (always).
However, all labels showing Event2...Event15 are always zero even when the refresh is clicked. The testTag label is incremented, so I know the event occurs. I would expect that at least Event14 should be non zero as it should reflect the same value as Event1.

Re: Get Tag Array Elements by String

Posted: Mon Mar 04, 2013 2:44 pm
by mark.monroe
I would try a very simple example, without any for loops. Just try and set 1 event's value.

Code: Select all

ushort[] arrEvent=new ushort[14];
GlobalDataItem eventTag = GetGlobalDataItem("arr_EventRegisterGroup_0");
arrEvent[0]=eventTag[0].Value;

Globals.Tags.Event2.SetAnalog(arrEvent[0]);
Now what happens if you use an Analog Numeric Box to monitor the Event2 tag?

Re: Get Tag Array Elements by String

Posted: Mon Mar 04, 2013 3:54 pm
by matto
I tried what you suggested, Mark, except that I used Event14. The new analog numeric on the screen also displays Event14 as zero at all times. My Event0 and Event1 tags now point to the first and second addresses of the arr_EventRegisterGroup_0 array also, and they are both non-zero.

Code: Select all

			ushort[] arrEvent=new ushort[14];
			GlobalDataItem eventTag = GetGlobalDataItem("arr_EventRegisterGroup_0");
			arrEvent[0]=eventTag[0].Value;
			Globals.Tags.Event14.SetAnalog(arrEvent[0]);
Maybe more clearly,
  • AnalogNumeric 1 (Tags.Event0 @ 40400) = 40995
    AnalogNumeric 2 (GetGlobalDataItem("arr_EventRegisterGroup_0")[0]) = 0
    **Tags.arr_EventRegisterGroup_0 is a 14 register array starting at 40400

Re: Get Tag Array Elements by String

Posted: Mon Mar 04, 2013 4:43 pm
by matto
To simplify further, I tried the following:

Code: Select all

//remove the local array variable AND change to uint instead of ushort
			uint arrEvent;
			GlobalDataItem eventTag = GetGlobalDataItem("arr_EventRegisterGroup_0");
			arrEvent=eventTag[0].Value;
			Globals.Tags.Event14.SetAnalog(arrEvent);
and just to be thorough,

Code: Select all

//remove the local array variable AND change to uint instead of ushort
//ALSO, try .Values[0] instead of [0].Value
			uint arrEvent;
			GlobalDataItem eventTag = GetGlobalDataItem("arr_EventRegisterGroup_0");
			arrEvent=eventTag.Values[0];
			Globals.Tags.Event14.SetAnalog(arrEvent);
And finally,

Code: Select all

			//use ONLY the GlobalDataItem and its 0th indexed value. no local variable at all
			GlobalDataItem eventTag = GetGlobalDataItem("arr_EventRegisterGroup_0");
			Globals.Tags.Event14.SetAnalog(eventTag[0].Value);
All have the same zeroed effect.