While docu says that simple data types (Integers, Reals, Strings, Bool) are supported, I think it is either a bug in the function or the documentation.
There is another function to write datalayer items with the name: datalayer.write_json(). I attached example code:
PLC variables (SINT, DINT, LINT, USINT, UINT, ULINT, REAL, LINT, BOOL, STRING, WSTRING,...)
python example
function dlWrite where you must provide the type of datalayer variable (more performant than dlWrite2 but more effort to use it)
function dlWrite2 where the type of datalayer variable is automatically read (less performant, but easier to use)
Read & write complete array values
Python example Copy
import sys
import json
# define item names with place holders
sArIntItem = "/plc/app/Application/sym/GVL/arInt[{}]"
sArLIntItem = "/plc/app/Application/sym/GVL/arLInt[{}]"
sArStrItem = "/plc/app/Application/sym/GVL/arStr[{}]"
sArRealItem = "/plc/app/Application/sym/GVL/arReal[{}]"
sArrItem = sArRealItem.format(1) # create array item name
datalayer.write(sArrItem, 1.2)
def dlWrite(sVar, sType, oVal):
# write datalayer variable, type must be provided
if type(oVal) is str:
# use write to avoid problems when string contains special characters like {}"'
datalayer.write(sVar, oVal)
else:
datalayer.write_json(sVar, f'{{"type": "{sType}", "value": {oVal}}}')
def dlWrite2(sVar, oVal):
# write datalayer variable, type is read
sRead = datalayer.read_json(sVar)
jRead = json.loads(sRead)
sType = jRead["type"]
if (sType == "string"):
# use write to avoid problems when string contains special characters like {}"'
datalayer.write(sVar, oVal)
else:
datalayer.write_json(sVar, f'{{"type": "{sType}", "value": {oVal}}}')
for i in range(10):
# loop over array item
# manipulate single int array items
sArrItem = sArIntItem.format(i) # create array item name
iVal = datalayer.read(sArrItem)
iVal += 10
# writeDL(sArrItem, "int16", iVal)
dlWrite2(sArrItem, iVal)
# manipulate single string array items
sArrItem = sArStrItem.format(i) # create array item name
sVal = datalayer.read(sArrItem)
sVal += "{'"
dlWrite2(sArrItem, sVal)
# Read complete array
sArrayLInt = sArLIntItem.replace("[{}]", "")
sRead = datalayer.read_json(sArrayLInt)
jRead = json.loads(sRead)
sType = jRead["type"]
arLInt = jRead["value"]
# manipulate array values
iLen = len(arLInt)
for iID in range(0, iLen):
arLInt[iID] += 10
# Write complete array
datalayer.write_json(sArrayLInt, json.dumps(jRead))
sys.exit(0)
PLC example Copy
{attribute 'symbol' := 'readwrite'}
{attribute 'linkalways'} // export unused variables into symbol file
VAR_GLOBAL
arStr: ARRAY[0..9] OF STRING; // string
arWStr: ARRAY[0..9] OF WSTRING; // string
arBool: ARRAY[0..9] OF BOOL; // bool8
arSInt: ARRAY[0..9] OF SINT; // int8
arInt: ARRAY[0..9] OF INT; // int16
arDInt: ARRAY[0..9] OF DINT; // int32
arLInt: ARRAY[0..9] OF LINT; // int64
arUSInt: ARRAY[0..9] OF USINT; // uint8
arUInt: ARRAY[0..9] OF UINT; // uint16
arUDInt: ARRAY[0..9] OF UDINT; // uint32
arULInt: ARRAY[0..9] OF ULINT; // uint64
arReal: ARRAY[0..9] OF REAL; // float
arLReal: ARRAY[0..9] OF LREAL; // double
END_VAR