Just to recall, there are two debugging approaches possible with IronPython in CODESYS:
- The .NET based approach using the .NET debugging APIs
- The Python (settrace) based approach
The python based debugging approach currently requires that one starts CODESYS with special command line parameters, and needs an active "connection" script to be maintained, so the ScriptEngine can actually connect to the debugger.
What is the special command line option? And what sort of script is needed?
Is it just the option to run the script, and is the script just any script that waits a little at the beginning before doing its work?
The python debugger based approach is a little more complicated. You need an Python IDE with "remote debugging" enabled and waiting for connections, and then the connection script needs to hook into sys.settrace() and open the connection to the debugger.
For example, we successfully got it working with PyCharm using the following command line:
CODESYS.exe --profile="CODESYS V3.5 SP5" --scriptDebugger="D:\test\charmdebug\initdebug.py"
The file "initdebug.py" itself looks like:
from __future__ import print_function
from __future__ import unicode_literals
pydevd.settrace('localhost', port=51234, stdoutToServer=True, stderrToServer=True)
You will need to adapt the pathes (and possible hostname and port) to match your own installation.
Also, how does the attach to process work, do I just have the same python script file open in Visual Studio and say "attach to process"? I have Visual Studio Tools for Python installed.
If you're using Python Tools for Visual Studio, I'd suggest you use the .NET based debugger approach, as it is much less troublesome.
You just use "Attach to process" to the running CODESYS instance _before_ you start the script, and select "Python" as well as "Managed (V4.0)". (It may also be "Managed (V4.0, V4.5)" or similar depending on the installed VS and .NET versions, just make sure you don't select the "Managed (V2.0/3.0/3.5)" instead. Then you simply open the script in Visual studio, set the breakpoint, and start it in CODESYS, that should work (and did in all my tests).
Also, I noticed with a script I was running that it was blocking the GUI. Do you have the python interpreter running in the UI thread, so if I run a python script I cannot control the UI anymore? I would like to do both, run python script and control UI at same time.
Yes, the python interpreter is running in the main thread. A technical reason is that the CODESYS architecture is inherentily bound to the UI thread for most API calls for historical reasons, so for running the scripts in a background thread, each interaction between the script and CODESYS would have to be marshalled to the UI thread. Another reason is that the focus of the ScriptEngine is to automate tasks which the user also can do manually - user interaction during those automated tasks would very likely disturb the script due to side-effects.
Depending on your exact use case, the solution may be:
- If you want the script to interact with the user, you can use the methods available in system.ui which provide some simple pre-cooked prompts, or you can use System.Windows.Forms to open more complex dialog windows. (You can also create those Dialogs in Visual Studio, compile them to an assembly, and then load the assembly from IronPython.)
- If you want to automate tests of CODESYS and thus control the UI of codesys (e. G. by using the UI Automation Framework), you should look into the CODESYS Test Manager - it is meant for test automation, and there exists an extended variant which allows user interaction during script execution.
- If you want to extend CODESYS with functionality currently not present, the CODESYS Automation Platform is the right solution for you - it allows you to hook into events, have background tasks, and much more extensions and customizations.
Check out the CODESYS store: http://store.codesys.com/CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions