FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important
information on the platform change.
05-23-2022 12:08 PM
Hey everybody!
I have a short question regarding the datatypes compability between PLC and python. Is there any way to write non-boolean PLC-datatypes with python, for example floats etc.?
For example with the following two GVL variables:
The way of writing variables like boolean ones, does not work for float or integer values:
Neither with a typecast, nor without:
I always get a runtime error:
Does anybody know, which types are compatible? And do you furthermore know, how to get print() or stdout working in the script engine?
Kind regards
Solved! Go to Solution.
05-23-2022 02:45 PM - edited 05-24-2022 01:11 PM
I did some tests and got it to run with the write_json only up to now. See my code below.
EDIT: Only 64bit signed numbers can be written with "datalayer.write" at the moment. See all working examples below.
#from ctrlxpy import datalayer
import json
#test with bool variable
MyBool = datalayer.read("plc/app/Application/sym/GVL_Test/bTest")
MyBool = not MyBool
datalayer.write("plc/app/Application/sym/GVL_Test/bTest",MyBool)
#test with string variable
MyString = datalayer.read("plc/app/Application/sym/GVL_Test/sTest")
MyString = 'nothing'
datalayer.write("plc/app/Application/sym/GVL_Test/sTest",MyString)
#test with wide string variable
MywString = datalayer.read("plc/app/Application/sym/GVL_Test/wsTest")
MywString = 'totally nothing'
datalayer.write("plc/app/Application/sym/GVL_Test/wsTest",MywString)
#test with long integer variable
MylInt = datalayer.read("plc/app/Application/sym/GVL_Test/liTest")
MylInt += 1
datalayer.write("plc/app/Application/sym/GVL_Test/liTest",MylInt)
datalayer.write("plc/app/Application/sym/GVL_Test/liTest",42)
#test with long real variable
MylReal = datalayer.read("plc/app/Application/sym/GVL_Test/lrTest")
MylReal += 1
datalayer.write("plc/app/Application/sym/GVL_Test/lrTest",MylReal)
datalayer.write("plc/app/Application/sym/GVL_Test/lrTest",4242424242.42)
#read different types
MysInt = datalayer.read("plc/app/Application/sym/GVL_Test/siTest")
MyusInt = datalayer.read("plc/app/Application/sym/GVL_Test/usiTest")
MyInt = datalayer.read("plc/app/Application/sym/GVL_Test/iTest")
MydInt = datalayer.read("plc/app/Application/sym/GVL_Test/diTest")
MyudInt = datalayer.read("plc/app/Application/sym/GVL_Test/udiTest")
MyulInt = datalayer.read("plc/app/Application/sym/GVL_Test/uliTest")
MyReal = datalayer.read("plc/app/Application/sym/GVL_Test/rTest")
MyJSON_sInt = datalayer.read_json("plc/app/Application/sym/GVL_Test/siTest")
MyJSON_usInt = datalayer.read_json("plc/app/Application/sym/GVL_Test/usiTest")
MyJSON_bool = datalayer.read_json("plc/app/Application/sym/GVL_Test/bTest")
MyJSON_string = datalayer.read_json("plc/app/Application/sym/GVL_Test/sTest")
MyJSON_wstring = datalayer.read_json("plc/app/Application/sym/GVL_Test/wsTest")
MyJSON_int = datalayer.read_json("plc/app/Application/sym/GVL_Test/iTest")
MyJSON_dint = datalayer.read_json("plc/app/Application/sym/GVL_Test/diTest")
MyJSON_udint = datalayer.read_json("plc/app/Application/sym/GVL_Test/udiTest")
MyJSON_lint = datalayer.read_json("plc/app/Application/sym/GVL_Test/liTest")
MyJSON_ulint = datalayer.read_json("plc/app/Application/sym/GVL_Test/uliTest")
MyJSON_real = datalayer.read_json("plc/app/Application/sym/GVL_Test/rTest")
MyJSON_lreal = datalayer.read_json("plc/app/Application/sym/GVL_Test/lrTest")
#Not working
#datalayer.write("plc/app/Application/sym/GVL_Test/siTest",MysInt)
#datalayer.write("plc/app/Application/sym/GVL_Test/usiTest",MyusInt)
#datalayer.write("plc/app/Application/sym/GVL_Test/iTest",MyInt)
#datalayer.write("plc/app/Application/sym/GVL_Test/iTest",MydInt)
#datalayer.write("plc/app/Application/sym/GVL_Test/rTest",MyudInt)
#datalayer.write("plc/app/Application/sym/GVL_Test/rTest",MyulInt)
#datalayer.write("plc/app/Application/sym/GVL_Test/rTest",Myreal)
#Insert variable in string
MyInt += 1
datalayer.write_json("plc/app/Application/sym/GVL_Test/iTest",'{"type":"int16","value":'+str(MyInt)+'}')
#create string by hand
MyJSON2 = '{"type":"int16","value":43}'
datalayer.write_json("plc/app/Application/sym/GVL_Test/iTest",MyJSON2)
#read out and manipulate JSON object
MyJSON_string = datalayer.read_json("plc/app/Application/sym/GVL_Test/iTest") #{\n"type": "int16",\n"value": 42\n}
datalayer.write_json("plc/app/Application/sym/GVL_Test/iTest",MyJSON_string)
MyJSON_object = json.loads(MyJSON_string)
MyJSON_object["value"] += 1
MyJSON_string2 = json.dumps(MyJSON_object)
datalayer.write_json("plc/app/Application/sym/GVL_Test/iTest",MyJSON_string2)