Strange behaviour when executing a script with a while loop doing computation and writing to Memory Tag

Dear All

Would greatly appreciate your kind advises or assistance for this peculiar issue with memory tag that I have had experience and had been cracking my head and also searching and reading the forum or manuals for few days but still could not find any solution. There wasnt any error message though but just that the memory tag isnt showing the correct value after executing the script.

I tried to put in a while loop to check certain condition and to read from another memory tag and upon checking, perform a mathematical operation and then write to this memory tag. However, it was discovered that on the very first time after i started my computer , it works but subsequently, it didnt works. Similarly if i restart my pc again, for the first time, it work but after that when i run it again, it didnt work any more.

from datetime import datetime
import jarray, traceback
from java.lang import String, Thread
from java.util import Date
import time
import re
import sys
import system
from decimal import *

counter = 0
opm = False
status = False

current = round("EM/Opm/Eel").value , 6)
W4_input ="EM/Opm/W4").value
W4 = round(W4_input, 6)

if -0.125 < current < 0.125 : 
	opm = True

	while counter <3 and not opm:
		if -0.125 < current < 0.125 :
			opm = True
			W4_input ="EM/Opm/W4").value
			W4 = round(W4_input, 6)
			W4_new = W4*0.75

			current = round("EM/Opm/Eel").value , 6)
			if -0.125 < current < 0.125 : 
				opm = True
		counter +=1

The inital value of W4 is 100.
Thus when “current” is not in the defined range, it will go into the while loop for 3 times.
However, at the third time, the memory tag is still 75 instead of 42.1875.
Meaning i loop it 3 times but the value remain at the first time of mathematical execution only.

Wonder what am I missing? Or what has gone wrong?
Would appreciate any advise to correct the error.

Thank you very much

system.tag.write() is not synchronous - writes are put into a queue, so scripts will continue to run while the write operating is pending. Depending on where you are running the script, you could switch to system.tag.writeSynchronous instead.

1 Like

Thanks for your reply

The script was put in a Button component and configure the script on a mouseClicked event handlers.

Yes, when I tried it out on a script console, i could see often the value of 2 after each write statement is executed which means it is pending .
It is what you have pointed out correctly that the write is in pending !

Will try it out using “system.tag.writeSynchronous” instead.

Thank you so much

Had tried it out
It works
thanks a lot

1 Like

Hi there,
Beg your pardon, wish to clarify -
Had only tried out the above using “system.tag.writeSynchronous” in the Ignition Script Console and it worked.

However, when the script using “system.tag.writeSynchronous” was put in the Button component and configure the script on a mouseClicked event handlers, it gave an error as follows which i have no idea why is there an error or what this error is?

I have checked the Ignition user manual but there isnt any “system.tag.writeAsynchronous” command.

Would greatly appreciate any advise on how to resolve this error.

Thank you very much

It is never safe to run potentially long-running functions from any event task. This is so obviously dangerous with writeSynchronous that IA added this error message in recent versions. It used to just let you freeze your entire client. See the documentation for system.util.invokeAsynchronous.

Dear Sir

Thanks for your kind reply…
It is something very new to me.
I have checked the documentation for system.util.invokeAsynchronous

But after reading it several times and checking various sources, I am still very lost and clueless on how to use it.
If possible, it would be great if there are more examples or explanation on how to use it.

Perhaps, the burning question that I wish to clarify is, where do I put this code/line for system.util.invokeAsynchronous ? Do I put it in the button mouse click event in replacement of those long-running functions that is giving the error?

Secondly, noted that it is not safe to run the long-running functions from the event task(currently using a button and mouse click event as usually this is how I do it as a newbie, am not sure of any other way) :sweat:
Would greatly appreciate if could kindly advise so as to learn.
Wonder what is the correct way or where would be a more correct method to run the above script which i had placed in the button mouse click event?

Thank you

Hi there


Had tried to put all those long process codes in a def and added in the system.util.invokeLater and system.util.invokeAsynchronous as per the example.
Still, I put all these scripts in the button event mouse click.
Somehow, there is no more error and seems to work.
But not sure is this the correct way to do it or is there a better way to do it.
If there is, appreciate any sharing.

Thank you