Makers Blog

Read and Write PLC Process Data with Python

nilshettig 16 March 2022 min. read
3,632 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.

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