I added some notes here:
https://www.cnczone.com/forums/plane...-expr-txt.html
I hope it helps.
I notice that a new version 2020-07-31 is available (although you forgot to update the release post). In its release notes it has:
Add: Multiple Expr.txt files
Add: Expr.txt #OnInit and #OnShutdown sections
Is there more information on these additions? I think they may be possibly useful for me, but without examples or updated documentation it is hard to know.
Cheers
Simon
Similar Threads:
I added some notes here:
https://www.cnczone.com/forums/plane...-expr-txt.html
I hope it helps.
Thank you. That explains it for me. I will try some things tomorrow if it isn't subzero in the shed.
So this is awesome. It gives me the startup event hook that I have been asking for so that I can start an extra python thread!
I have it working, but in a little bit of a roundabout way. I was unable to figure out how to directly run a python script from expr.txt, but was able to use startcode('M990') which is a script that then just starts the python task.
What is the correct syntax to call a python script from an expr.txt file?
Cheers
Simon
Be careful - OnInit and OnShutdown are triggered every time you exit settings.
I'll add 'py' and 'pythr' expression functions.
Ok, that is good to know. I assume OnShutdown would be called first followed by OnInit when exiting settings?
I guess I will need to figure out how to kill the thread that is created in the OnInit in the OnShutdown, so that I don't end up with more than on background thread.
Or you simply check if thread is already running. If it is then you don't "loop" new thread any you allow it to exit.
Your idea goes like this (pythr will be in next version):
Code:#OnInit print('OnInit') pythr('Python\posmon.py') #OnShutdown print('OnShutdown') _posmonstop=1Code:import sys import time import datetime import threading import planetcnc print("|!|bposmon module BEGIN", str(datetime.datetime.now())) planetcnc.setParam("_posmonstop", 0) cnt = 0 while True: if planetcnc.getParam("_posmonstop"): break x = planetcnc.getParam("_hw_motor_x") y = planetcnc.getParam("_hw_motor_y") z = planetcnc.getParam("_hw_motor_z") planetcnc.print("Time PC: ", str(datetime.datetime.now()), " cnt: ", cnt, " Position X: ", x, " Y: ", y, " Z: ", z) print("Time PY:", str(datetime.datetime.now()), "cnt:", cnt, "Position X:", x, "Y:", y, "Z:", z) time.sleep(1) cnt = cnt + 1 #if cnt > 5: # break print("|!|bposmon module END")
That looks promising. The actual script I am starting is this. Basically to incorporate some other home automation stuff that controls other devices in the workshop.
Code:# start with M990 after stating PlanetCNC import sys import time import ctypes import threading import socket import paho.mqtt.client as mqtt import planetcnc # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): client.connected_flag = rc if rc==0: client.subscribe("stat/+/POWER") def on_message(client, userdata, message): if message.payload.decode("utf-8")=="ON": v = 1 else: v = 0 if message.topic=="stat/garage_compressor/POWER": planetcnc.setParam("_compressor", v) if message.topic=="stat/garage_vacuum/POWER": planetcnc.setParam("_vacuum", v) if message.topic=="stat/garage_dustextractor/POWER": planetcnc.setParam("_dustextractor", v) def main(): try: mqtt.Client.connected_flag = -1 client = mqtt.Client("plantcnc-"+socket.gethostname()) client.on_connect = on_connect client.on_message = on_message client.username_pw_set("clientid", "Password") client.connect_async("someipaddress") client.loop_forever() except Exception as e: planetcnc.print("MQTT Exception: ", str(e)) t = threading.Thread(target=main) t.daemon = True t.start()
I tried using variables to test, but it appears they all get cleared when exiting out of settings. Instead I tried naming my thread and then checking if it already exists. So I have ended up with this:
Code:#OnInitprint('OnInit') startcode('M990') #OnShutdown print('OnShutdown')This works to initially start the thread, but when exiting settings I get the OnShutdown message from expr.txt but then PlanetCNC exits. Any ideas?Code:# start with M990 after stating PlanetCNC import sys import time import ctypes import threading import socket import paho.mqtt.client as mqtt import planetcnc # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): client.connected_flag = rc if rc==0: client.subscribe("stat/+/POWER") def on_message(client, userdata, message): if message.payload.decode("utf-8")=="ON": v = 1 else: v = 0 if message.topic=="stat/garage_compressor/POWER": planetcnc.setParam("_compressor", v) if message.topic=="stat/garage_vacuum/POWER": planetcnc.setParam("_vacuum", v) if message.topic=="stat/garage_dustextractor/POWER": planetcnc.setParam("_dustextractor", v) def main(): try: mqtt.Client.connected_flag = -1 client = mqtt.Client("plantcnc-"+socket.gethostname()) client.on_connect = on_connect client.on_message = on_message client.username_pw_set("sun-mqtt", "MDZ6Lnf5^5froH6P") client.connect_async("10.0.1.12") client.loop_forever() except Exception as e: planetcnc.print("MQTT Exception: ", str(e)) planetcnc.print("start mqtt.py") mqtt_thread = list(filter(lambda t: t.name == "mqtt", threading.enumerate())) if len(mqtt_thread) > 0: planetcnc.print("mqtt already running") else: planetcnc.print("mqtt starting") t = threading.Thread(target=main, daemon=True) t.name = "mqtt" t.start() planetcnc.print("TID="+str(t.native_id)) for s in threading.enumerate(): planetcnc.print(s.name)
I should have added that if I execute the M990 command from the MDI everything works as expected.
In next version variables will not be cleared when exiting settings.
Crash happens because Python is reopened when exiting settings. Because thread is still running it crashes. This is actually expected and nothing I can do about it.
In next version I'll check if Python settings are changed and skip reopening it.
That explains it all. Thanks so much for all the crazy fast responses and resolutions.