1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
2 <CFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="cext_xsd.xsd"> |
2 <CFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="cext_xsd.xsd"> |
3 <includes> |
3 <includes> |
4 <![CDATA[]]> |
4 <![CDATA[]]> |
5 </includes> |
5 </includes> |
6 <variables/> |
6 <variables> |
|
7 <variable name="TestInput" type="SINT" class="input"/> |
|
8 <variable name="TestOutput" type="SINT" class="input"/> |
|
9 </variables> |
7 <globals> |
10 <globals> |
8 <![CDATA[void Python_to_C_Call(int value){ |
11 <![CDATA[volatile long Lock=0; |
9 /* That code should never touch to |
12 volatile char PtoC=1,CtoP=2; |
10 variables modified by PLC thread */ |
13 |
11 printf("C code called by Python: %d\n",value); |
14 int Simple_C_Call(int val){ |
|
15 return val+1; |
12 } |
16 } |
13 ]]> |
17 |
|
18 int Python_to_C_Call(int toC, int *fromC){ |
|
19 /* Code called by python should never touch to |
|
20 variables modified by PLC thread directly |
|
21 |
|
22 AtomicCompareExchange comes from |
|
23 beremiz' runtime implementation */ |
|
24 |
|
25 int res = 0; |
|
26 if(!AtomicCompareExchange(&Lock, 0, 1)){ |
|
27 PtoC=toC; |
|
28 *fromC=CtoP; |
|
29 AtomicCompareExchange(&Lock, 1, 0); |
|
30 res=1; |
|
31 } |
|
32 printf("C code called by Python: toC %d fromC %d\n",toC,*fromC); |
|
33 return res; |
|
34 } |
|
35 |
|
36 int PLC_C_Call(int fromPLC, int *toPLC){ |
|
37 /* PLC also have to be realy carefull not to |
|
38 conflict with asynchronous python access */ |
|
39 int res; |
|
40 if(!AtomicCompareExchange(&Lock, 0, 1)){ |
|
41 CtoP = fromPLC; |
|
42 *toPLC = PtoC; |
|
43 AtomicCompareExchange(&Lock, 1, 0); |
|
44 return 1; |
|
45 } |
|
46 return 0; |
|
47 }]]> |
14 </globals> |
48 </globals> |
15 <initFunction> |
49 <initFunction> |
16 <![CDATA[]]> |
50 <![CDATA[]]> |
17 </initFunction> |
51 </initFunction> |
18 <cleanUpFunction> |
52 <cleanUpFunction> |