cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Datatype compatibility between PLC and Python

Datatype compatibility between PLC and Python

rexxon
Established Member

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:

rexxon_0-1653300413124.png

The way of writing variables like boolean ones, does not work for float or integer values:

rexxon_1-1653300413145.png

Neither with a typecast, nor without:

rexxon_2-1653300413178.png

I always get a runtime error:

rexxon_3-1653300413116.png

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

1 REPLY 1

CodeShepherd
Community Moderator
Community Moderator

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)

 

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist