Hello,
I am currently working on a project to send JSONs to my PLC axf2152 with firmware version 2023/6 via HTTP. I have chosen to use Linux and Python that are embedded in the PLC.
Since I am quite new to PLCnext, I am doing everything step by step, and I am now simply trying to write a string to a variable in the PLC. I do this using the following python code:
import logging
from PyPlcnextRsc import Device, RscVariant
from PyPlcnextRsc.Arp.Plc.Gds.Services import IDataAccessService, WriteItem
from PyPlcnextRsc.tools import DataTypeStore
Configure logging
logging.basicConfig(level=logging.ERROR, format=‘%(asctime)s - %(levelname)s - %(message)s’)
def secure_info_supplier(_):
return ‘admin’, ‘secret’
if name == “main”:
try:
TypeStore = DataTypeStore.fromString(
“”"
TYPE
mystringtype : STRING;
END_TYPE
“”")
plc_ip = “192.secret”
with Device(plc_ip, secureInfoSupplier=secure_info_supplier) as device:
Create an instance of mystringtype
myString = TypeStore.NewSchemaInstance(“mystringtype”)
if myString is None:
logging.error(“Could not initialize mystringtype.”)
exit(1)
Set the string value
myString.Value = “Your Text Here”
data_access_service = IDataAccessService(device)
Write the string to the PLC
data_access_service.Write((WriteItem(“Arp.Plc.Eclr/myStringVariable”, RscVariant.of(myString)),))
Read back the string and print the result
read_items = data_access_service.Read((“Arp.Plc.Eclr/myStringVariable”,))
if not read_items or read_items[0] is None:
logging.error(“Could not read the value of myStringVariable.”)
exit(1)
rcv_myString = TypeStore.ReceiveAsSchemaInstance(“mystringtype”, read_items[0].Value)
if rcv_myString is None or not hasattr(rcv_myString, ‘Value’):
logging.error(“Received data is not valid.”)
exit(1)
print(rcv_myString.Value)
except Exception as e:
logging.exception("An unexpected error occurred: ", exc_info=e)
However, I am stuck on where to place this variable. It is not writing anything at the moment. I have set this in the data types (see figure).
My questions now are how to proceed, how to make this work properly, and where else I should write this variable. I am sure that the connection is correct, as another code to simply fetch the firmware and post it via curl to my local server does work.
Thank you in advance.
Kind regards,
Daan van der Veeken