--- a/tests/python/beremiz.xml Thu Oct 18 17:42:29 2012 +0200
+++ b/tests/python/beremiz.xml Thu Oct 18 20:24:11 2012 +0200
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
+<BeremizRoot URI_location="LOCAL://"> --- a/tests/python/c_code@c_ext/cfile.xml Thu Oct 18 17:42:29 2012 +0200
+++ b/tests/python/c_code@c_ext/cfile.xml Thu Oct 18 20:24:11 2012 +0200
@@ -3,14 +3,48 @@
+ <variable name="TestInput" type="SINT" class="input"/> + <variable name="TestOutput" type="SINT" class="input"/> -<![CDATA[void Python_to_C_Call(int value){
- /* That code should never touch to
- variables modified by PLC thread */
- printf("C code called by Python: %d\n",value);
+<![CDATA[volatile long Lock=0; +volatile char PtoC=1,CtoP=2; +int Simple_C_Call(int val){
+int Python_to_C_Call(int toC, int *fromC){ + /* Code called by python should never touch to + variables modified by PLC thread directly + AtomicCompareExchange comes from + beremiz' runtime implementation */ + if(!AtomicCompareExchange(&Lock, 0, 1)){ + AtomicCompareExchange(&Lock, 1, 0); + printf("C code called by Python: toC %d fromC %d\n",toC,*fromC); +int PLC_C_Call(int fromPLC, int *toPLC){ + /* PLC also have to be realy carefull not to + conflict with asynchronous python access */ + if(!AtomicCompareExchange(&Lock, 0, 1)){ + AtomicCompareExchange(&Lock, 1, 0); --- a/tests/python/plc.xml Thu Oct 18 17:42:29 2012 +0200
+++ b/tests/python/plc.xml Thu Oct 18 20:24:11 2012 +0200
@@ -8,7 +8,7 @@
creationDateTime="2008-12-14T16:21:19"/>
<contentHeader name="Beremiz Python Support Tests"
- modificationDateTime="2012-06-08T23:11:45">
+ modificationDateTime="2012-10-18T20:21:28"> <pageSize x="1024" y="1024"/>
@@ -63,6 +63,16 @@
+ <variable name="FromC"> + <variable name="C_Pragma0"> + <derived name="C_Pragma"/> @@ -202,7 +212,7 @@
<relPosition x="290" y="15"/>
- <expression>'PLCBinary.Python_to_C_Call(5678)'</expression>
+ <expression>'PLCBinary.Simple_C_Call(5678)'</expression> <block localId="12" width="125" height="80" typeName="python_eval" instanceName="Block2">
<position x="650" y="417"/>
@@ -457,9 +467,79 @@
<expression>pytest_var3</expression>
+ <outVariable localId="25" height="30" width="60"> + <position x="820" y="750"/> + <relPosition x="0" y="15"/> + <connection refLocalId="26" formalParameter="OUT"> + <position x="820" y="765"/> + <position x="765" y="765"/> + <expression>FromC</expression> + <inVariable localId="1" height="30" width="30"> + <position x="605" y="750"/> + <relPosition x="30" y="15"/> + <expression>23</expression> + <block localId="26" width="80" height="45" typeName="C_Pragma" instanceName="C_Pragma0"> + <position x="685" y="735"/> + <variable formalParameter="IN"> + <relPosition x="0" y="30"/> + <connection refLocalId="1"> + <position x="685" y="765"/> + <position x="635" y="765"/> + <variable formalParameter="OUT"> + <relPosition x="80" y="30"/> + <pou name="C_Pragma" pouType="functionBlock"> +<![CDATA[(* hereafter is a C pragma accessing FB interface in a clean way *) + int fromPLC = __GET_VAR(data__->IN); + if(PLC_C_Call(fromPLC, &toPLC))__SET_VAR(data__->,OUT,toPLC); +(* If you do not use __GET_VAR and _SET_VAR macro, expect unexpected *) --- a/tests/python/python@py_ext/py_ext.xml Thu Oct 18 17:42:29 2012 +0200
+++ b/tests/python/python@py_ext/py_ext.xml Thu Oct 18 20:24:11 2012 +0200
@@ -1,11 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Python xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="py_ext_xsd.xsd">
-<![CDATA[import time,sys
+<![CDATA[import time,sys,ctypes +Python_to_C_Call = PLCBinary.Python_to_C_Call +Python_to_C_Call.restype = ctypes.c_int +Python_to_C_Call.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_int)]
- PLCBinary.Python_to_C_Call(arg)
+ if(Python_to_C_Call(arg, i)): + print "toC:", arg, "from C:", res + print "Failed Python_to_C_Call failed"