how can i make lightweighttag to get read() function

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
artistmonkey
Posts: 7
Joined: Sun May 27, 2018 9:11 pm

how can i make lightweighttag to get read() function

Post by artistmonkey »

I can get value of tag using dynamic tag name script. But the problem is tag value was not updated. I need to use tag.read() function and I don't know how to do that.
Anyone can help me!

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

Re: how can i make lightweighttag to get read() function

Post by Russ C. »

I may need a bit more context on what you're trying to do, but it sounds like you should just be able to read the tag value using the Globals.Tags.<YourTagName>.Value
Best regards,

Russ
(801) 708-6690
Technical Support
Contact Us

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

Nightf1y
Posts: 3
Joined: Thu Dec 03, 2015 2:48 pm

Re: how can i make lightweighttag to get read() function

Post by Nightf1y »

You need to do the following...

add...

using Interfaces.Tag;

and then access the tag as below.

((IBasicTag)Globals.Tags.Auto_BatchEndOkToSaveData).Read();


Regards

artistmonkey
Posts: 7
Joined: Sun May 27, 2018 9:11 pm

Re: how can i make lightweighttag to get read() function

Post by artistmonkey »

Russ C. wrote:I may need a bit more context on what you're trying to do, but it sounds like you should just be able to read the tag value using the Globals.Tags.<YourTagName>.Value
I just try to use dynamic string tag and when I use in script module, I notice that lightweighttag doesn't update value which real value in plc. I made some script that use dynamic tag script to read value and send to remote server using mqtt. So, some tags were freeze. Datalogger tags and Alarm tag go working. So, I force to use read() and send to mqtt broker. But I can't use on lightweighttag. Right now my stupid solution is I make script that initial with if clause that always false and write Globals.Tags.tag.Read(); for everytag. The purpose is make every tag change to GlobalDataTag while compile time. But Globals.Tags.tag.Read(); function never fire coz of if(false) condition. Any Idea?
Thanks you.

artistmonkey
Posts: 7
Joined: Sun May 27, 2018 9:11 pm

Re: how can i make lightweighttag to get read() function

Post by artistmonkey »

Nightf1y wrote:You need to do the following...

add...

using Interfaces.Tag;

and then access the tag as below.

((IBasicTag)Globals.Tags.Auto_BatchEndOkToSaveData).Read();


Regards
Hi Nightf1y
I am using below script to get dynamic tag value

public string Str2Tags(string TagName)
{
//string to tagName and get relative value, if unavilable return NaN
string strb;
try
{
double bb = double.Parse(getVal(TagName).Value.ToString());
strb = bb.ToString("0.##");
return strb;

}
catch(Exception)
{
return "NaN";
}
}

public static T StringToTag<T>(string tagName) where T : class
{
Type type = typeof(T);
FieldInfo[] props = Globals.Tags.GetType().GetFields();
foreach (FieldInfo prop in props) {
if (prop.FieldType.Equals(type)) {
if (prop.Name.Equals(tagName, StringComparison.CurrentCultureIgnoreCase)) {
return (T)prop.GetValue(Globals.Tags);
}
}
}
return null;
}

public VariantValue getVal(string tagName)
{
GlobalDataItem GDI = StringToTag<GlobalDataItem>(tagName);
if (GDI == null)
{
LightweightTag LWT = StringToTag<LightweightTag>(tagName);
if (LWT == null)
return null;
else
{
return LWT.Value;
}
}
else
{
GDI.Read();
return GDI.Value;
}
}

I used GDI.Read(); to get actual value in plc. But I don't know how to get LWT value when screen is not openned which has lightweighttag.

artistmonkey
Posts: 7
Joined: Sun May 27, 2018 9:11 pm

Re: how can i make lightweighttag to get read() function

Post by artistmonkey »

artistmonkey wrote:
Nightf1y wrote:You need to do the following...

add...

using Interfaces.Tag;

and then access the tag as below.

((IBasicTag)Globals.Tags.Auto_BatchEndOkToSaveData).Read();


Regards
Hi Nightf1y
Hi Nightf1y,
it's working. I put ((IBasicTag)LWT).Read();
Thank You very much.

Post Reply