Home

codesys.com Ingo aliazzz
Attachments
python-editor.png (60911 bytes)

Python in CODESYS

CODESYS comes with built-in support for Ironpython

The Ironpython scripts are running inside of the CODESYS IDE, and can be used to automate huge parts of the system. That virtually everything within CODESYS can be automated this way, and that CODESYS can even be started headless, running a python script, makes the system really mighty.

There are many use cases you can think of:

  • Test automation
  • CI/CD integration for CODESYS projects
  • Static analysis for CODESYS projects
  • Autodeploy applications on controllers
  • Scan for controllers and update them
  • Automatically discover diagnostic info from multiple controllers in a network
  • Many many more usages ...

See also CODESYS Help

Editing of Python scripts

By default CODESYS doesn't provide you an integrated script editor for Python.

However there is a commercial AddOn available in the CODESYS Store, from 3S - Systems, which integrates well into CODESYS and assists you in writing your CODESYS Python scripts:

If you prefer free alternatives, there are plenty of alternatives available out there. Just to name a few:

Notepad++

  1. Very small but variable installation footprint (dependent on extension choices)
  2. Only provides intellisense help on standard libraries
  3. Syntax highlighting
  4. Indentation help
  5. Quickly edit single files
  6. Less suited for larger projects

VS.Net

Select the "Visual Studio IDE Community" version, install Iron Python support via the installation process

  1. Large but variable installation footprint (heavily dependent on installation/extension choices)
  2. Provides very good intellisense help
  3. Syntax highlighting
  4. Indentation help
  5. Not well suited to edit single files
  6. Suited for large projects

PyCharm

  1. Medium but variable sized installation footprint (dependent on installation/extension choices)
  2. Provides very good intellisense
  3. Syntax highlighting
  4. Indentation help
  5. Not well suited to edit single files
  6. Suited for larger projects

TIP: you can opt to install notepad++ and PyCharm or Visual Studio in tandem as they complement each other well
Visual Studio Code or VS.Net can be an alternative, however, the CFORGE tool is developed using VS.NET, so your mileage may vary.

Executing Scripts

CODESYS IDE

  • Call Tools -> Scripting -> Execute Script File ...
  • Select the script

The output of the script will be written to the message pane.

Command line

Instead of manually selecting the script to execute in the IDE, you can also start CODESYS and let it execute a script without the user interface:

Example:

"C:\Program Files (x86)\CODESYS 3.5.16.0\CODESYS\Common\CODESYS.exe" --noUI --profile='CODESYS V3.5 SP16' --runscript='svn-checkout.py'

With --noUI, CODESYS starts without the IDE and the output of the script is forwarded directly to the terminal:

d:\tmp> "C:\Program Files (x86)\CODESYS 3.5.16.0\CODESYS\Common\CODESYS.exe" --noUI --profile='CODESYS V3.5 SP16' --runscript='svn-checkout.py'
SVN: Information: [12:16:45] ---------- Scripted SVN Operation ----------
SVN: Information: [12:16:45] UpdateStarted <>
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Camera device
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Camera device\[0]
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\I2C
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Onewire
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Plc Logic
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Plc Logic\Application
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Plc Logic\Application\Library Manager
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Plc Logic\Application\PLC_PRG
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Plc Logic\Application\Task Configuration
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Plc Logic\Application\Task Configuration\Main
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Plc Logic\Application\Trace
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Plc Logic\Application\Trace_Axis
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Plc Logic\Application\Visualization
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Plc Logic\Application\Visualization Manager
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\Plc Logic\Application\Visualization_1
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\SPI
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\SoftMotion General Axis Pool
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\SoftMotion General Axis Pool\SM_Drive_RaspiStepper
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\SoftMotion General Axis Pool\SM_Drive_RaspiStepper_1
SVN: Information: [12:16:47] Added CODESYS_Control_for_Raspberry_Pi\[4]
SVN: Information: [12:16:47] Added GlobalTextList
SVN: Information: [12:16:47] Added Library Manager
SVN: Information: [12:16:47] Added Project Settings
SVN: Information: [12:16:47] Added Visualization
SVN: Information: [12:16:47] Added meta.profile
SVN: Information: [12:16:47] Completed at revision: 24
SVN: Information: [12:16:47] Added: 26
SVN: Information: [12:16:48] ---------- Scripted SVN Operation ----------

Help.codesys.com

As of CODESYS version 3.5.14.0, https://help.codesys.com/webapp/idx-scriptingengine;product=ScriptEngine;version=3.5.14.0 offers an exthaustive overview of the various available scripting objects and their possibilities.

References

cforge heavily uses the scripting API to automate some workflows for developers.


Discussion

  • i-campbell

    i-campbell - 2020-02-08

    I think it is important to note that Visual Studio Community is not able to be used legally in all scenarios. If you use it only for creating open source content you are fine (right now), but in all other cases I would check the "Usage" section of this link
    https://visualstudio.microsoft.com/vs/community/

    [History, maybe not relevant]
    At one point Visual Studio Express was available and free in all use cases.
    Now your free options are Visual Studio Community (for some use cases) and Visual Studio Code (as far as I know, doesn't have a 'designer' view to drag and drop controls onto a GUI. As far as I know only uses .net core, not .net framework)

     
  • i-campbell

    i-campbell - 2020-02-09

    This wiki page has a section on help.codesys.com
    currently it links only to the section that details the available classes and functions. Scripting Engine

    There is another section not yet mentioned in the wiki article. It is in another area of help.codesys.com that scripters might find useful, and that is: CODESYS Development System > Using Scripts

    edit: clarified content. I guess it is a pull request to add the 'other' section of help.codesys.com

     

    Last edit: i-campbell 2020-02-09

Log in to post a comment.