Need help either scaling an OPC write or invokingAsyc a system.tag.writeSync

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.

Any ideas?

I was playing around with this script today and came up with the code below. I’m using the sleep function, but, I think it’s fine as it’s in a separate thread. Does that (and the rest of the code) look fine?

	if currentValue.value == True:
		
		pathValue = tagPath
		pathValue = tagPath.rsplit("/",1)
		folderPathValue = pathValue[0]
						
		def startDraw(folderPath = folderPathValue):
			import system
			from time import sleep
			recipe = system.tag.read(folderPath+"/Input/New SP").value
			upOneLevel = folderPath.rsplit("/",1)
			upOneLevel = upOneLevel[0]
			system.tag.writeSynchronous(folderPath+"/OPC tags/SP Register", recipe)
			for n in range(5):
				sleep(2)
				recipeInPLC = system.tag.read(upOneLevel+"/BasicInfo/Setpoint")
				if recipeInPLC.value == recipe:
					print "Verified SP was set correctly to " + str(recipe) + ".  Starting draw if not running."
					system.tag.writeSynchronous(upOneLevel+"/BasicInfo/RequestDrawRun",1)
					break
				else:
					print "SP not the same as requested SP, trying to set SP again"
					system.tag.writeSynchronous(folderPath+"/OPC tags/SP Register", recipe)
			else:
				print "Failed to set SP 5 times!  Giving up and not starting draw!"
		
		system.util.invokeAsynchronous(startDraw)
		system.tag.write("[.]Input/SP Change Enabled",0)