01/27/2022

How to use ctrlX PLC Engineering API with Python

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:

 

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.

 

 

 

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:

2. Create a new object in your PLC project

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

 

 

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.

 

 

 

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).

 

 

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

3

Latest published/updated articles

Types
How-to
Categories
IO