system.tag.writeBlocking returns good quality, but is not written to OPC tag

Hello!, I have a gateway event which is triggered from a value change from an OPC tag, this event has a script to validate some functions and then write values on the OPC tags.
Some times the function is not writteen to the tag; I already change the function .write to .writeBlocking, but the result its the same.
Im not sure if someone has the same issue, and what I’m doing wrong,

Note: using the function .writeBlocking the results is always good, and I cant get any logs with a error.

Can you post your script?

attached is the part where Im trying to write on the OPC tag

def SendStart(seqn):
	system.tag.write("[.]mensaje",GetCurrentmsg()+ "<br>-Funcion SendStart() ")
	ValueStart=bool(system.tag.read("[.]ValueStart").value)
	ValueStatus=system.tag.read("[.]ValueStatus").value
	system.tag.write("[.]mensaje",GetCurrentmsg()+ "<br>-Valores Enviados al PLC")
	result = system.tag.writeBlocking(["[.]start_PC","[.]status_PC"],[ValueStart,ValueStatus])
	system.tag.write("[.]mensaje",GetCurrentmsg()+ "<br>-Evalua  Start: "+str(result))
	system.tag.write("[.]SeqNo",seqn+1)

You’ll need to edit and post it within a preformatted block. Python is unreadable without intact spacings

However, I’d suggest instead of building your values to write within the function arguments, to write them into a variable which you pass in. It’ll make your life easier when it comes to diagnosing issues as you’ll know exactly what you’re passing into the functions and if they’re in fact valid. Then put a bunch of print statements to check the values

I'm having the same issue with getting system.tag.writeBlocking working.

	tagPath = self.custom.TagPath
	print "tagpath: ", tagPath
	currentValue = system.tag.readBlocking(tagPath)[0].value
	print "tag value: ", currentValue
	if  currentValue == 0:
		system.tag.writeBlocking([tagPath],[int(currentValue) | 1])
	elif currentValue == 1:
		system.tag.writeBlocking([tagPath],[int(currentValue) | 0])

No errors, tag status does not change.

You're not checking for errors, how do you know there are none?

What do you expect to happen with this? (Bitwise OR with zero changes nothing.)

The goal is to simply write a boolean tag in the PLC to 1.

I changed the tagpath to a direct tag that I am monitoring in the Tag Browser.

	tagPath = "[default]DV/UTL/WW1/ALM/ALM_Injection_Alarm_TEST"
	print "tagpath: ", tagPath
	currentValue = system.tag.readBlocking(tagPath)[0].value
	print "tag value: ", currentValue
	if  currentValue == 0:
		system.tag.writeBlocking("[default]DV/UTL/WW1/ALM/ALM_Injection_Alarm_TEST",[1])
	elif currentValue == 1:
		system.tag.writeBlocking("[default]DV/UTL/WW1/ALM/ALM_Injection_Alarm_TEST",[0])

I am following the examples in the ignition helpfile Reading and Writing to Tags

tagPath = "[default]DV/UTL/WW1/ALM/ALM_Injection_Alarm_TEST"
print "tagpath: ", tagPath
currentValue = system.tag.readBlocking(tagPath)[0].value
print "tag value: ", currentValue
result = system.tag.writeBlocking(["[default]DV/UTL/WW1/ALM/ALM_Injection_Alarm_TEST"], [not currentValue])

print result

You need to capture the result of writeBlocking to know if there's anything being reported as a problem.

Boolean tags aren't 0 and 1. They are True and False. You can write 0 or 1 to them as a convenience, but your comparisons are broken. Replace the entire 4-line if-elif block with this:

system.tag.writeBlocking(["[default]DV/UTL/WW1/ALM/ALM_Injection_Alarm_TEST"], [not currentValue])

That is a much simpler version of what I was trying to do.

Here is the working code block:

	tagPath = self.custom.TagPath	#[default]DV/UTL/WW1/ALM/ALM_TEST
	print "tagpath: ", tagPath
	currentValue = system.tag.readBlocking(tagPath)[0].value
	print "tag value: ", currentValue
	result = system.tag.writeBlocking([tagPath], [not currentValue])
	return result

Why does this code not work? How can I check for a boolean?

	tagPath = self.custom.TagPath	#[default]DV/UTL/WW1/ALM/ALM_TEST
	print "tagpath: ", tagPath
	currentValue = system.tag.readBlocking(tagPath)[0].value
	print "tag value: ", currentValue
	if  currentValue == "true":
		system.tag.writeBlocking(["tagPath"],[0])
	elif currentValue == "false":
		system.tag.writeBlocking(["tagPath"],[1])

tagPath is a variable named tagpath. "tagPath" is the literal string tagPath.

I'm referring to the IF statement:
How can I check if the boolean tag is true or false?

	if  currentValue == "true":
		system.tag.writeBlocking(["tagPath"],[0])
	elif currentValue == "false":
		system.tag.writeBlocking(["tagPath"],[1])

You check for a boolean tag value being True or False like this:

if currentValue:
    system.tag.writeBlocking([tagPath],[False])
else:
    system.tag.writeBlocking([tagPath],[True])

However, since all you're wanting to do is invert the current value, then you don't need to "check" the value, just use the not keyword to invert the value.

system.tag.writeBlocking([tagPath],[not currentValue])

Which not coincidentally is exactly what @pturmel suggested.

Be careful with your tagPath references, I'm willing to bet that "tagPath" isn't a valid tag path. As @PGriffith has pointed out.

1 Like

See the Python docs:

when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.

Python uses the singleton objects True and False for the boolean constants, unlike other languages which use true and false as constants.

So you could write:
if currentValue == True:
And it would work just fine. But idiomatic Python tells you to drop the == True, because it's not really adding any clarity. You are obviously welcome to write your own code however you want, though :slight_smile:

And when you do, the pedants around here (like me) will ding you for it. :smirk:

5 Likes

I just know you're talking about me, your attempts at misdirection are futile.

3 Likes

Ok. Caught me. I'm not a pedant. :innocent: