I have a bit of code that I was using to ensure one OPC parameter in a PLC was set properly before another was set. I had had (extremely rare) problems in the past with the network write failing on the first system.tag.write, the script successfully executing the second tag’s system.tag.write, activating the machine, and finally, the first system.tag.write automatically retrying (the OPC server, actually) the write successfully, but too late. I came up with the following code using system.opc.writes instead, and it seems to be working well.
if currentValue.value == True: recipe = system.tag.read("[.]Input/Recipe number to start").value rtsOPCServer = system.tag.read("[.]OPC tags/Recipe to Start.OPCServer").value rtsOPCPath = system.tag.read("[.]OPC tags/Recipe to Start.OPCItemPath").value returnQuality = system.opc.writeValue(rtsOPCServer, rtsOPCPath, recipe) for n in range(5): if returnQuality.isGood(): recipeInPLC = system.opc.readValue(rtsOPCServer, rtsOPCPath) if recipeInPLC.value == recipe: print "Verified PLC recipe was set correctly to " + str(recipe) + ". Starting recipe." system.tag.write("[.]OPC tags/Start Recipe Bit",1) break else: print "Recipe in PLC not the same as requested recipe, trying to set recipe again" returnQuality = system.opc.writeValue(rtsOPCServer, rtsOPCPath, recipe) else: print "Comm failure while tring to set recipe, trying again." returnQuality = system.opc.writeValue(rtsOPCServer, rtsOPCPath, recipe) else: print "Failed to set recipe number 5 times! Giving up and not starting recipe!" system.tag.write("[.]Input/Recipe Start Enabled",0)
My issue now is that I’m trying to re-purpose this code elsewhere in the system. I ran into an issue when I pointed this code at an OPC tag that I have scaling setup on. When I use system.tag.write, ignition scales the value for me and writes the scaled value out to the PLC. When I use system.opc.write, I need to do the scaling myself.
I’m thinking at this point I can either figure out how to scale my setpoint myself and continue using the opc.write method, or, I can try to figure out how to use the tag.writeSynchronous function and let ignition handle the scaling. I’m noticing that the tag.writeSynchronous function is supposed to be called inside the invokeAsynchronous, but I’m not sure how to wrap that into the script I have written.