Page 1 of 1

how can i make lightweighttag to get read() function

Posted: Sun May 27, 2018 9:33 pm
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!

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

Posted: Fri Jun 01, 2018 9:17 am
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

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

Posted: Tue Jun 05, 2018 3:43 pm
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

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

Posted: Thu Jun 14, 2018 1:46 am
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.

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

Posted: Fri Jun 15, 2018 4:10 am
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.

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

Posted: Tue Jun 26, 2018 12:34 am
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.