Makers Blog

Read and Write PLC Process Data with Python

nilshettig 16 March 2022 min. read
5,360 views 2 comments

Scope

This article describes how to access and write process data with Python utilizing the PyPlcnextRsc library. You can find the library’s documentation here. This page also provides examples for creating and handling complex data structures. If the described techniques do not meet your requirements, you are good to go to the man-page.

Update for firmware version 2022.0.3 LTS

The PyPlcnextRsc library was updated to be compatible with the firmware 2022.0.3 LTS. Previous versions (< 0.1.10) caused an error during the connect process device.connect(). For firmware versions < 2022 no update is required. Nevertheless, it is possible to update the Python package with the following commands:
alias pip='/opt/plcnext/.local/bin/pip'
pip install -U pyplcnextrsc

Prerequisite

  1. The PLC must have internet access.
  2. Install the Python package manager pip if it is not on the PLCnext device yet. You can check it by logging into the PLC and type pip or pip3, and hit enter. If pip is installed, some version information is displayed. Otherwise, install pip:
    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

    python3 get-pip.py
    alias pip='/opt/plcnext/.local/bin/pip'
  3. Install the required package: pip install -U PyPlcnextRsc

Read & Write Process Data

With the following example, we read process data:

# required imports
from PyPlcnextRsc import Device, RscVariant, RscType
from PyPlcnextRsc.Arp.Plc.Gds.Services import IDataAccessService, WriteItem

# we have to login using the standard credentials
# it is not recomended to store the login data in code
# the following example should only show how to interact with the process data
secureInfoSupplier = lambda:("admin","password")

# login to the device
device = Device('127.0.0.1', secureInfoSupplier=secureInfoSupplier)
device.connect()

# service to access the process data
data_access_service = IDataAccessService(device)

# read one variable
read_item = data_access_service.ReadSingle("Arp.Plc.Eclr/test_in2")

# read multiple variables
read_items = data_access_service.Read(("Arp.Plc.Eclr/test_in1", "Arp.Plc.Eclr/test_in2", ))
read_item1 = read_items[0]
read_item2 = read_items[1]

# get the value of the read_item
value = read_item.Value.GetValue()
# get the data type of the read_item
value = read_item.Value.GetType()

Differences exist concerning the type of process data we want to access:

# Extern variable:
# Arp.Plc.Eclr/
read_item = data_access_service.ReadSingle("Arp.Plc.Eclr/test_in2")

# Local variable:
# Arp.Plc.Eclr/.
read_item = data_access_service.ReadSingle("Arp.Plc.Eclr/MainInstance.test_in2")

# Function block variable:
# Arp.Plc.Eclr/..
read_item = data_access_service.ReadSingle("Arp.Plc.Eclr/MainInstance.myFB.test_in2")

And last here is the code to write data:

# first create the variable containing the value and the data type
rscv = RscVariant(16,RscType.Int16)
# create a WriteItem with the destination process variable
wi = WriteItem("Arp.Plc.Eclr/MainInstance.test_in3", rscv)

# write single process variable
data_access_service.WriteSingle(wi)

# write several process variables
data_access_service.Write((wi1, wi2,))

For the different data types, please visit the library’s documentation.

Discussion

Please login/register to comment
Login/Register

Leave a Reply

Your email address will not be published. Required fields are marked *

gjduesseldorf 14.03.2022

Hello, I'm using the firmware 2022.0.3 LTS on an AXC F 2152. I successfully installed the PyPlcnextRsc package. I imported the required modules. I built the login configuration. But device. connect() creates these errors: admin@axcf2152:~$ python3 Python 3.8.11 (default, Jun 28 2021, 10:57:31) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from PyPlcnextRsc import Device, RscVariant, RscType >>> from PyPlcnextRsc.Arp.Plc.Gds.Services import IDataAccessService, WriteItem >>> secureInfoSupplier = lambda:("admin","password") >>> device = Device('127.0.0.1', secureInfoSupplier=secureInfoSupplier) :1: FutureWarning: CURRENT ONLY FOR INTERNAL TEST USE !!! >>> device.connect() Traceback (most recent call last): File "/usr/lib/python3.8/site-packages/PyPlcnextRsc/common/transport/rsc_sock. py", line 178, in connect self._socket.connect(self.info.getAddr()) File "/usr/lib/python3.8/ssl.py", line 1342, in connect self._real_connect(addr, False) File "/usr/lib/python3.8/ssl.py", line 1333, in _real_connect self.do_handshake() File "/usr/lib/python3.8/ssl.py", line 1309, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:1131) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.8/site-packages/PyPlcnextRsc/common/device.py", line 179, in connect self._rscClient.connect() File "/usr/lib/python3.8/site-packages/PyPlcnextRsc/common/transport/rsc_client.py", line 85, in connect self.socketWrapper.connect() File "/usr/lib/python3.8/site-packages/PyPlcnextRsc/common/transport/rsc_sock.py", line 184, in connect raise CommonRemotingException(message="Can not connect to device", cause=e, err=ErrType.SOCKET_UNSPECIFIED) PyPlcnextRsc.common.exceptions.CommonRemotingException: ('Can not connect to device (SOCKET_UNSPECIFIED)', SSLError(1, '[SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:1131)')) What is missing? Many thanks Götz (Goetz.Jaeckel@GJ-Didaktik.de)

Login / Register to reply
Nils Hettig 15.03.2022

Dear Mr. Jäckel, I’ve just encountered the same error, when using the latest firmware 2022.0.3 LTS. However, it worked with the firmware 2021.9. Probably some security mechanisms have changed since the error indicates an incompatible TLS version. As a first workaround I would recommend downgrading the firmware. Since I was not involved in the development of the Python library, I could not fix the error. Nevertheless, I will contact my colleague who developed the library and ask for support. When I get news, I will inform you via this channel. Hope that helps. If you have further questions don’t hesitate to contact me. Best regards Nils Hettig

Dominique Rakowski 16.03.2022

Hi, We have a customer that is interested in using this solution to read/write datas on the PLC, but he also needs a specific package for his Python application, which doesn't work on our Linux distribution. We told him he could use a Docker container, so I tried to use this package inside a container but there are some issues : root@dc0cbfda6a0b:/opt# python3 test_docker.py /opt/test_docker.py:11: FutureWarning: CURRENT ONLY FOR INTERNAL TEST USE !!! device = Device('192.168.254.52', secureInfoSupplier=secureInfoSupplier) Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/PyPlcnextRsc/common/transport/rsc_sock.py", line 178, in connect self._socket.connect(self.info.getAddr()) File "/usr/local/lib/python3.10/ssl.py", line 1374, in connect self._real_connect(addr, False) File "/usr/local/lib/python3.10/ssl.py", line 1365, in _real_connect self.do_handshake() File "/usr/local/lib/python3.10/ssl.py", line 1341, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: NO_CIPHERS_AVAILABLE] no ciphers available (_ssl.c:997) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/test_docker.py", line 12, in device.connect() File "/usr/local/lib/python3.10/site-packages/PyPlcnextRsc/common/device.py", line 179, in connect self._rscClient.connect() File "/usr/local/lib/python3.10/site-packages/PyPlcnextRsc/common/transport/rsc_client.py", line 85, in connect self.socketWrapper.connect() File "/usr/local/lib/python3.10/site-packages/PyPlcnextRsc/common/transport/rsc_sock.py", line 184, in connect raise CommonRemotingException(message="Can not connect to device", cause=e, err=ErrType.SOCKET_UNSPECIFIED) PyPlcnextRsc.common.exceptions.CommonRemotingException: ('Can not connect to device (SOCKET_UNSPECIFIED)', SSLError(1, '[SSL: NO_CIPHERS_AVAILABLE] no ciphers available (_ssl.c:997)')) The docker container is the official Python image with the latest tag. If you need more information, feel free to ask, Cheers,

Login / Register to reply
gjduesseldorf 18.03.2022

Thanks Gentlemen, it works!

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