howto-howto-1444x512.png

How to use ctrlX PLC Engineering API with Python

Mohann99
Member

Overview

This blog will show you how to operate the ctrlX PLC Engineering API using the Python programming language. The ctrlX PLC Engineering API is used to generate and edit ctrlX CORE PLC projects. Some examples are explained in this blog to show how to use the API with Python.

A Documentation of the API can be accessed in the ctrlX PLC Engineering:2022-01-26_15h44_36.png

 

Prerequisites
  1. Programming environment (for example Visual Studio Code)
  2. Python library "requests":
    pip install requests ​
  3. ctrlX PLC Engineering has run on your PC

 

Implementation

Communication is established between the programming environment and the ctrlX PLC Engineering API by http protocol using the requests library.

Hint: read = GET, write = PUT, create = POST, delete = DELETE

1. Get content of an object

A GET request is sent to the REST API with the URL of the PLC_PRG in JSON format. The REST API sends back a response, the response can then be displayed as JSON format in terminal.

Bild1 .png

 

 

 

import requests# import requests after we have installed it
import json #to transfer the response into json format 
url='http://localhost:9002/plc/engineering/api/v2/devices/Device/PLC%20Logic/Application/PLC_PRG'# the URL of the PLC_PRG
response=requests.get(f'{url}')# sent the get requests to the REST API
print(f"The REST API rsepone is \n {response.json()}")# print the response in the Console in json format 

 

 

Response from REST API in the terminal:

3.png

2. Create a new object in your PLC project

A code example in Python to add a GVL to your Application.

4.png

 

 

import requests# import requests after we have installed it
import json #to transfer the response into json format 
#gvl is defined as dictionaries. this corresponds to the JSON format.  
gvl={
  "name": "GVL",
  "elementType": "GVL",
  "elementProperties": {
    "build": {
      "excludeFromBuild": False,
      "external": False,
      "enableSystemCall": False,
      "linkAlways": False,
      "compilerDefines": ""
    }
  },
  "declaration": "{attribute 'qualified_only'}\nVAR_GLOBAL\nEND_VAR"
}
url='http://localhost:9002/plc/engineering/api/v2/devices/Device/PLC%20Logic/Application'# the URL of the Application 
response = requests.post(f'{url}',json=gvl)# sent a post requests to the REST API 
print(response)#  to see the REST API response 

 

 

The REST API returns a response with code status 201, which means “created”.

3. Modify an existing Object

A code example in Python that defines a variable in the GVL.

5.png

 

 

 

import requests# import requests after we have installed it
import json #to transfer the response into json format 
#gvl is defined as dictionaries this corresponds to the JSON format with the corresponding changes
gvl={
  "name": "GVL",
  "elementType": "GVL",
  "elementProperties": {
    "build": {
      "excludeFromBuild": False,
      "external": False,
      "enableSystemCall": False,
      "linkAlways": False,
      "compilerDefines": ""
    }
  },
  "declaration": "{attribute 'qualified_only'}\nVAR_GLOBAL\n AM_FROM_Python: BOOL := FALSE ; \n END_VAR"# Modify data
}
url='http://localhost:9002/plc/engineering/api/v2/devices/Device/PLC%20Logic/Application/GVL'# the URL of the GVL 
response = requests.put(f'{url}',json=gvl)# sent a PUT requests to the REST API 
print(response.json())#  to see the REST API response

 

 

The response in the terminal is "200". This means the command was executed.

4. Delete an existing object

A code example in Python to delete a GVL from your Application.

 

 

import requests# import requests after we have installed it
import json #to transfer the response into json format
# delete the GVL from the Application root
url='http://localhost:9002/plc/engineering/api/v2/devices/Device/PLC%20Logic/Application/GVL'# the URL of GVL
response = requests.delete(url)# sent the delete requests to the REST API 
print(response.json())#  to see the REST API response 

 

 

The response in the terminal is "200". This means the command was executed.

5. Import a POU.project as an XML file from your PC

A code example in Python that import a POU.project (Test.xml).

6.png

 

 

import requests# import requests after we have installed it
import json #to transfer the response into json format
#define the command as json format
ImportPlcOpenXml={
  "jobType": "ImportPlcOpenXmlJob",
  "jobParameters": {
    "nodeUrl": "/devices/device/PLC Logic/Application",
    "filePath": "D:\\mine\\Api\\XML_Files",# This is the Path of the projekt 
    "fileName": "Test.xml"# the projekt name 
  }
}
url='http://localhost:9002/plc/engineering/api/v2/jobs'# url for Jobs 
response = requests.post(url,json=ImportPlcOpenXml)#sent the requests to REST API 
print(response.json())

 

 

The REST API returns a response with code status 201, which means “created”.

6. Close an open project

A code example in Python to close an open project.

 

 

import requests# import requests after we have installed it
import json #to transfer the response into json format
ProjectClose={
  "jobType": "ProjectJob",
  "jobParameters": {
    "action": "Close"
  }
}
url='http://localhost:9002/plc/engineering/api/v2/jobs'
response = requests.post(url,json=ProjectClose)#sent the requests to REST API 
print(response.json())

 

 

 The response in the terminal is "200". This means the command was executed.

Get the Code

Download the code from GitHub and try it yourself. 

Additional Information
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