I am trying to automate some things for Codesys and it has been a very frustrating process so far. I am having trouble getting consistent behavior and finding any decent documentation.
I have a .bat file with the following line. It works fine.
Am I doing something wrong or is this just broken in PowerShell?
Also is there real Python API documentation anywhere? There are a number of broken links on the help site and what's available online is not very in depth.
Last edit: food 2021-12-26
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yeah it also took me awhile to get it correct, the profile argument is very sensitive and has to get the correct string (quotes and all) otherwise it will just open the GUI
This is what I used to use to run CODESYS python scripts from PowerShell 7.2
function_start_codesys_process{[cmdletbinding()]param([validatenotnullorempty()][parameter(mandatory=$true, helpmessage='Path to CODESYS.exe')][string]${codesys_bin_path},[validatenotnullorempty()][parameter(mandatory=$true, helpmessage='Profile for CODESYS.exe')][string]${codesys_profile},[validatenotnullorempty()][parameter(mandatory=$true, helpmessage='Path to pythons script')][string]${python_script_path},);if(-not([psobject]${codesys_bin_obj}=Get-Item-path${codesys_bin_path})){throw;}if(-not${codesys_bin_obj}.name.equals('CODESYS.exe')){throw(${codesys_bin_obj}.fullname+' is not a valid CODESYS .exe binary!');}if(-not${codesys_profile}){${codesys_profile}='CODESYS V3.5 SP16 Patch 4 for Safety V1.6.1';Write-Verbose('codesys_profile not provided, defaulting to '+${codesys_profile});}if(-not([psobject]${python_script_obj}=Get-Item-path${python_script_path})){throw;}[hashtable]${process_arg_table}=@{wait=$true;erroraction='stop';filepath=${codesys_bin}.fullname;argumentlist=('--profile="'+${codesys_profile}+'"'),('--noUI'),('--runscript="'+${python_script_obj}.fullname+'"');}Start-Process@process_arg_table;}
It's slightly old code so you might need to edit it abit to fix it if it doesn't run as is with
If anyone does find better documentation though, let me know cause yeah a lot of CODESYS' leaves much to be desired (especially when paid components have old and new documentation mixed together)
Last edit: void 2021-12-28
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks. After contemplating dropping Codesys itself for this project I decided to drop PowerShell instead and use Python for everything. This works fine:
I've seen multiple links to what seems to be further documentation for the scriptengine but all the links are broken. it's probably just the same lack luster docs available online. It took me way too long to figure out how to export all nodes from a project. I've got it figured out now but it was hours of pain because the documentation is so bad.
IronPython is what Codesys is using? That would explain by my scripts work when I run them through Codesys but fail when I try to run them with my Python interpreter.
Hello,
I am trying to automate some things for Codesys and it has been a very frustrating process so far. I am having trouble getting consistent behavior and finding any decent documentation.
I have a .bat file with the following line. It works fine.
start /b /wait CODESYS.exe --profile="CODESYS V3.5 SP17 Patch 2" --runscript="C:\Users\Karl\Desktop\codesys-scripts\export.py" --noUI
I have tried this with a PowerShell script but does not respect the --profile switch and opens the GUI anyways.
& "C:\Program Files\CODESYS 3.5.17.20\CODESYS\Common\CODESYS.exe" --profile="CODESYS V3.5 SP17 Patch 2" --runscript="C:\Users\Karl\Desktop\codesys-scripts\export.py" --noUI
Am I doing something wrong or is this just broken in PowerShell?
Also is there real Python API documentation anywhere? There are a number of broken links on the help site and what's available online is not very in depth.
Last edit: food 2021-12-26
Yeah it also took me awhile to get it correct, the profile argument is very sensitive and has to get the correct string (quotes and all) otherwise it will just open the GUI
This is what I used to use to run CODESYS python scripts from PowerShell 7.2
It's slightly old code so you might need to edit it abit to fix it if it doesn't run as is with
_start_codesys_process -codesys_bin_path <single_quoted_path> -codesys_profile <single_quoted_profile> -python_script_path <single_quoted_path>
Otherwise, it should be a good template for what you want to do
As for the python API documentation, best I can tell you is look at https://help.codesys.com/webapp/idx-scriptingengine;product=ScriptEngine;version=3.5.16.0 (use Ctrl+F), https://ironpython.net/documentation/dotnet/dotnet.html and of course examples on the forum
If anyone does find better documentation though, let me know cause yeah a lot of CODESYS' leaves much to be desired (especially when paid components have old and new documentation mixed together)
Last edit: void 2021-12-28
Thanks. After contemplating dropping Codesys itself for this project I decided to drop PowerShell instead and use Python for everything. This works fine:
subprocess.run('C:\\Program Files\\CODESYS 3.5.17.20\\CODESYS\\Common\\CODESYS.exe --profile="CODESYS V3.5 SP17 Patch 2" --runscript="C:\\Users\\Karl\\Desktop\\codesys-scripts\\export.py" --noUI')
I've seen multiple links to what seems to be further documentation for the scriptengine but all the links are broken. it's probably just the same lack luster docs available online. It took me way too long to figure out how to export all nodes from a project. I've got it figured out now but it was hours of pain because the documentation is so bad.
IronPython is what Codesys is using? That would explain by my scripts work when I run them through Codesys but fail when I try to run them with my Python interpreter.
Hi, you really should read here ;-)
Python Scripting