Checking connection to PLC through a tag

I have a tag in the PLC which changes its value every 2 seconds (0 to 1 then back to 0 and so on)

Using this tag, can we write a script that checks this tag and if the tag value doesn’t change for about 5 seconds, it writes 1 to another tag (memory tag) indicating that the connection to PLC is lost?

I can think of several ways to do this.

To implement it strictly the way you suggested, you could use a gateway timer script. One of the several problems with doing it the way you suggested is that if you read it the value and run the script on it at just the wrong times, the value will have changed and changed back again, looking like it didn’t change. If you read it TOO frequently, it won’t have changed yet. These issues can be overcome with some hassle and error checking. A slightly better, but still bad, way of doing it would be to at least let the tag in the PLC change through a range of values, like 1 through 10000 every second. That way, if it hasn’t changed in the 5 second interval of your timer, there’s certainly something wrong (but the PLC could just be in program mode).

A much preferred way of solving the problem would be using one of the several ways to detect exactly the thing you’re checking for.

For example, SQL tags have a “Quality” property that tell if the the tag is good or bad, the scripting language has system.opc.getServerState command, tags and even transaction group items have alerting built right in, to name a few ways.

If you get a little more specific, the forum users will be able to help you better and if you demonstrate that you’ve already tried to help yourself, they’ll be more willing to help.

[quote=“oxanemi”]I have a tag in the PLC which changes its value every 2 seconds (0 to 1 then back to 0 and so on)

Using this tag, can we write a script that checks this tag and if the tag value doesn’t change for about 5 seconds, it writes 1 to another tag (memory tag) indicating that the connection to PLC is lost?[/quote]

you can do this, create an expression tag which updates itself

if(dateDiff({[~]Heartbeat.lastchange}, now(), "sec") >= 5, 1, 0)

Just make sure and create a one second scan class and put both tags in it, that way the tags will evaluate every second. the sub property .lastchange will update each time the tag changes. so all we are doing here is subtracting the last time the tag changed from the current time which gives you a number in seconds. we then say if that is greater or equal to 5 then make the expression tag a 1, if it isnt then make the expression tag a zero.

And here’s a gateway script that will work…

[code]value1 = system.tag.read(‘Merch/TestTag’).value
value2 = system.tag.read(‘Mem Bools/MemBool1’).value
if value1 == value2 :
system.tag.write(‘Mem Bools/MemBool3’,1)
else:
system.tag.write(‘Mem Bools/MemBool3’,0)

system.tag.write(‘Mem Bools/MemBool1’,value1)
[/code]

[quote=“diat150”][quote=“oxanemi”]I have a tag in the PLC which changes its value every 2 seconds (0 to 1 then back to 0 and so on)

Using this tag, can we write a script that checks this tag and if the tag value doesn’t change for about 5 seconds, it writes 1 to another tag (memory tag) indicating that the connection to PLC is lost?[/quote]

you can do this, create an expression tag which updates itself

if(dateDiff({[~]Heartbeat.lastchange}, now(), "sec") >= 5, 1, 0)

Just make sure and create a one second scan class and put both tags in it, that way the tags will evaluate every second. the sub property .lastchange will update each time the tag changes. so all we are doing here is subtracting the last time the tag changed from the current time which gives you a number in seconds. we then say if that is greater or equal to 5 then make the expression tag a 1, if it isnt then make the expression tag a zero.[/quote]

I just tried this and it worked! The tag shows the value 1 when the PLC isn’t communicating with Ignition! Exactly how I wanted it to work!

I tried replicating this by writing a gateway script but it doesn’t work

import datetime
value1 = system.tag.read('CPM2VC/PLC_Heartbeat').LastChange
value2 = datetime.now()

if(value1.seconds)-(value2.seconds)>=5:
    system.tag.write('CMP2VC/MemTag',1)
else:
    system.tag.write('CMP2VC/MemTag',0)

[quote=“cbanzet”]And here’s a gateway script that will work…

[code]value1 = system.tag.read(‘Merch/TestTag’).value
value2 = system.tag.read(‘Mem Bools/MemBool1’).value
if value1 == value2 :
system.tag.write(‘Mem Bools/MemBool3’,1)
else:
system.tag.write(‘Mem Bools/MemBool3’,0)

system.tag.write(‘Mem Bools/MemBool1’,value1)
[/code][/quote]

I understand the concept and I do not know why it didn’t work. I think when the PLC disconnects, it locks the value in the tag. Therefore, value1 = value2 at all times.
This is the first time I wrote a gateway startup script, any newbie mistakes I might be doing?