Dear Community User! We are updating our platform to a new system.
Read more: Important information on the platform change.

teaserimage

How to set up Python scripts with PLC programming

DanielToGo
New Poster

In this guide we describe the basics of how to set up a ctrlX CORE to work with a Python script by using the PLC app. We will show you a PLC sample project that illustrates how to read/write data in the datalayer and how to let axes move in the Motion app with a Python script.

You can download the example project below and try starting it yourself.

Prerequisites

What you need:

Step-by-Step Guide

So, let's start by adding the CXA_Python library which lets us execute, delete, reset and monitor Python scripts on script instances. Go to the Library Manager, click Add Library and search for CXA_Python.

Add CXA_PythonAdd CXA_Python
The next step is to create a global variables list.

  • The first block represents the nodes we create in the datalayer. You can find them in ‘plc/app/Application/sym/[name of GVL file]/‘ later.
  • The second block represents the script instance.
  • The third block represents the script file that executes the python commands later. Here you can give the instance a name and specify the path to the python script we create in a few steps.

The variables for the script instance and the script file will be linked to the visualization elements later.

Global Variables to create nodes and variables for the instance/visualizationGlobal Variables to create nodes and variables for the instance/visualization

The next step is to create the script instance and the script executable. We do that by creating a new POU program called PRG_Script with Structured Text as implementation language. Here we create some variables for the script instance and file. We can use some structures from the CXA_Python library for that, that will be shown in the visualization later.

In the next part we can create the script instance and start the script file. The instance lets you execute the python script, but an instance only lets you execute one script at the same time. In this case the script instance can only be started on the startup or when restarting after the script finished running.

Use the declared variables to create and start the instance and file.

Creates python instance/file and connects button variables to itCreates python instance/file and connects button variables to it

Another important step is to use the global variables that we want to become nodes in the datalayer (see line 1). This is important because you might not see variables in the next step if you aren’t using them.

So, what we do next is export the variables we want to see in the datalayer. We do that by going into the Symbol Configuration and checking the variables, but first you need to build the project for all variables to appear.

List of variablesList of variables

Then we simply call this new program from PLC_PRG.

Calls PRG_Script programCalls PRG_Script program

Now we can create a visualization and link the buttons and display elements to the according variables. That’s how we control the instance and the file.

Visualization to start/stop the python instance and fileVisualization to start/stop the python instance and file

After the visualization is done, we can start with the python script which is quite simple. We can use it just like a normal python script from here on. Here you can test reading from the datalayer.

Reads data from datalayerReads data from datalayer

You can also manipulate the data and write it back into the datalayer.

Calculates some data and writes it back to the datalayerCalculates some data and writes it back to the datalayer

And finally, here are some examples to show that it’s possible to move axes with the Motions app by using a python script.

Moves axes with the Motion appMoves axes with the Motion app

All we need to do now is to upload the python script onto the ctrlX CORE. To do that click “Manages app data” on the top right corner of the overlay of he ctrlX CORE.

Managing app dataManaging app data

Then open the File View by clicking on the three dots.

Switching to file viewSwitching to file view

Now we can upload the python script to the path we picked earlier as a global variable in the GVL_Script file. In this case it’s just the home folder 'activeConfiguration/MyPythonScript1.py'. You can also edit the script in this view.

How to start the example

We uploaded this project so you can download it below and start it, if ctrlX WORKS is installed.
To test this python script, we need to add a linear axis called “Axis_X” in the motions app.Add axis windowAdd axis window

We start this program by going into the PLC project, connecting it to the ctrlX CORE, logging into the ctrlX via the PLC project and starting it.

Logging inLogging in

Starting the projectStarting the project

Now we can start and stop the python script and instance with the visualization we created earlier.

The script instance needs to be enabled first and then you can execute the file with the buttons. Below the buttons you can see the current state of the script instance and the script file to check if it’s running correctly or if any errors occurred. You can also reset or abort the python script.

Everything should be green in the visualization. You should be able to see the axis move, and you should be able to see the changes in the datalayer if the program is running correctly.

Running Visualization WindowRunning Visualization Window

Axis is movingAxis is moving

Values got assigned and changedValues got assigned and changed

Known Issue

As of June 2024 something prevents a restart of the python script, if the "OPC UA Pub/Sub" app is installed on the same ctrlX CORE.

Related Links
1 Comment
Felix_
Established Member

Hello, 

Can we get the source code of this how to ?

Thank you

Must Read
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist