Makers Blog

Use gRPC to interface Python scripts with PLCnext Engineer Projects.

Pedro_Aguilar 28 May 2025 min. read
207 views 0 comments

The following information serves both as an update and an expansion to this post from 2022.

The approach works the same for the AXC F 2152 and the AXC F 3152, the difference resides on how to install the required libraries.

IMPORTANT: The approach has been tested and seen working for FW 2024.x and 2025.0

For the AXC F 2152:

The libraries grpcio and grpcio-tools need to be cross compiled off device and be installed manually. An overview on the approach I take to perform the cross compilation using QEMU can be found on this forum entry

The cross compiled libraries for Python 3.11, together with the rest of the files to follow this post, have been made available via this link

The following instructions allow you to create a container with Python 3.11 where you can run the scripts that make use of gRPC.

  1. Using WinSCP (or some other software that allows accessing the file system of your controller), create a folder in /opt/plcnext to be used for the gRPC communication, this example will assign the name "grpc2152" to that folder. within grpc2152, create the following folder structure.
grpc2152/
├── include/
│     └── google/
│	    └── (empty folder for now)		
│
├── whl_files311/
│        ├── grpcio-1.71.0-cp311-cp311-linux_armv7l.whl
│        └── grpcio_tools-1.71.0-cp311-cp311-linux_armv7l.whl
│
├── generate_googlegrpc.py
├── grpc_test.py
└── Readme.txt
  1. Download the repository "https://github.com/PLCnext/gRPC" and from there, take the folder "protobuf" and place it inside the previously-created google folder. The resulting folder structure then looks like this:
grpc2152/
├── include/
│        └── google/
│	       └── protobuf/	
│		     └── (content of the protobuf folder from the PLCnext github repo)
│
├── whl_files311/
│        ├── grpcio-1.71.0-cp311-cp311-linux_armv7l.whl
│        └── grpcio_tools-1.71.0-cp311-cp311-linux_armv7l.whl
│
├── generate_googlegrpc.py
├── grpc_test.py
└── Readme.txt
  1. Create a SSH session to your PLC, log in as root user (su) and get the slim version of python 3.11. (at this point, internet connection is necessary to get the python image)
$ podman pull docker.io/library/python:3.11.10-slim-bookworm
  1. Run and attach to the container (linking the folder grpc2152 as a volume and a second volume (run) necessary for the connection to the grpc socket):
$ podman run -it -v /opt/plcnext/grpc2152:/grpc2152 -v /run:/run --restart=always --name grpctester python:3.11.10-slim-bookworm /bin/bash
  1. Once attached to the container console, navigate to the folder whl_files311 and install the grpcio libraries. (internet connection is necessary for the dependencies)
	$ cd /grpc2152/whl_files311/
	$ pip install *.whl
  1. Go back to the parent folder grpc2152 within the container and execute the python script "generate_googlegrpc.py" (some messages should appear -working!-)
	$ cd /grpc2152
	$ python3 generate_googlegrpc.py
  1. Once the script has successfully finished execution, a new folder should be present (pxc_grpc). Inside grpc2152, the python snippets for grpc are now located there. Now you can test the gRPC functionality with the example project and script.

  2. Download and start the PLCnext Engineer project "TestingGRPC.pcwex" to the controller.

  3. Test the gRPC functionality from the console of the container by executing the "grpc_test.py" script. In the PLCnext Engineer project, accessing the variables in debug mode, the content of the variables should be populated with the values from the script.

variablesdebugmode.png

For the AXC F 3152.

Follow the same procedure given above for the 2152 but in the step 5, install the libraries directly from pypi.org

	$ pip install grpcio==1.71.0
	$ pip install grpcio-tools==1.71.0

Data Types and making use of the code snippets.

Now, whenever you want to make use of gRPC for accessing variables, include the folder where the code snippets were generated to the Python path used by your scripts and import the necessary classes (line 6 to 14 of the given grpc_test.py file).

Remember to consider the data types outlined in the old post that I mentioned at the beginning of this article.

Note:

The Makers Blog shows applications and user stories of community members that are not tested or reviewed by Phoenix Contact. Use them at your own risk.

Discussion

Please login/register to comment

Login/Register

Leave a Reply

Newsletter
Never miss a new article
Sign up for the newsletter
Never miss news about PLCnext Technology
Get interesting content via newsletter four times a year
Receive exclusive information before all other users