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.
- The PLC must have internet access.
- 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 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'
- Install the required package:
pip3 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 read_item2 = read_items # 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.