Cyclic execution of Python script
Hi,
What would be the simplest way to execute a script installed on the PLCnext in a cyclic way?
Example, we have a python script in the PLC that reads variables through REST Data Interface and we want to cyclically execute that script.
Is there a way to call it from the PLC programming (Through PLCnext Engineer)?
Thanks
Robin
Comments
Robin,
You can create a loop, see the below example.
<span style="font-family: monospace;">delay = 100 #100ms</span>
try:
print("Press Ctrl+C to stop the script.")
while 1:
Inputs = Pullloop()
<span style="font-family: monospace;"> #(logic here)</span>
Postloop(Outputs)
time.wait(delay)
except KeyboardInterrupt:
sys.exit()
You could call it from Engineer, but keep in mind HTTP requests are synchronous (meaning they will not execute the next line of code until it has completed), and the time between pulls would not be as consistent as doing it in the program.
Hi Robin,
In the next release of PLCnext Base library - due soon, I think - there will be a "Shell" function that you can call from PLCnext Engineer, which you could use to execute your Python script. Then, you could use an ESM cyclic task to implement the timed calling cycle. This shell command will be executed asynchronously, so it will not hold up the processing of the real-time task.
~ Martin.
Was the "shell"function you mentioned ever released?
I am also looking to execute a python scrip from PLCnext on a timed basis
~ Martin.
from threading import Timer