author | greg |
Fri, 10 Apr 2009 07:53:42 +0200 | |
changeset 335 | c5f3f71e7260 |
parent 329 | 22e65b8e20f4 |
permissions | -rw-r--r-- |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
1 |
/* |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
2 |
* Python Asynchronous execution code |
329 | 3 |
* |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
4 |
* PLC put python commands in a fifo, respecting execution order |
329 | 5 |
* with the help of C pragmas inserted in python_eval FB code |
6 |
* |
|
7 |
* Buffer content is read asynchronously, (from non real time part), |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
8 |
* commands are executed and result stored for later use by PLC. |
329 | 9 |
* |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
10 |
* In this implementation, fifo is a list of pointer to python_eval |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
11 |
* function blocks structures. Some local variables have been added in |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
12 |
* python_eval interface. We use those local variables as buffer and state |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
13 |
* flags. |
329 | 14 |
* |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
15 |
* */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
16 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
17 |
#include "iec_types_all.h" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
18 |
#include "POUS.h" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
19 |
#include <string.h> |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
20 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
21 |
/* The fifo (fixed size, as number of FB is fixed) */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
22 |
static PYTHON_EVAL* EvalFBs[%(python_eval_fb_count)d]; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
23 |
/* Producer and consumer cursors */ |
281
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
24 |
static int Current_PLC_EvalFB; |
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
25 |
static int Current_Python_EvalFB; |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
26 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
27 |
/* A global IEC-Python gateway state, for use inside python_eval FBs*/ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
28 |
static int PythonState; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
29 |
#define PYTHON_LOCKED_BY_PYTHON 0 |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
30 |
#define PYTHON_LOCKED_BY_PLC 1 |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
31 |
#define PYTHON_MUSTWAKEUP 2 |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
281
diff
changeset
|
32 |
#define PYTHON_FINISHED 4 |
329 | 33 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
34 |
/* Each python_eval FunctionBlock have it own state */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
35 |
#define PYTHON_FB_FREE 0 |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
36 |
#define PYTHON_FB_REQUESTED 1 |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
37 |
#define PYTHON_FB_PROCESSING 2 |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
38 |
#define PYTHON_FB_ANSWERED 3 |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
39 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
40 |
int WaitPythonCommands(void); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
41 |
void UnBlockPythonCommands(void); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
42 |
int TryLockPython(void); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
43 |
void UnLockPython(void); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
44 |
void LockPython(void); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
45 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
46 |
void __init_python() |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
47 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
48 |
int i; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
49 |
/* Initialize cursors */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
50 |
Current_Python_EvalFB = 0; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
51 |
Current_PLC_EvalFB = 0; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
52 |
PythonState = PYTHON_LOCKED_BY_PYTHON; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
53 |
for(i = 0; i < %(python_eval_fb_count)d; i++) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
54 |
EvalFBs[i] = NULL; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
55 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
56 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
57 |
void __cleanup_python() |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
58 |
{ |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
281
diff
changeset
|
59 |
PythonState = PYTHON_FINISHED; |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
281
diff
changeset
|
60 |
UnBlockPythonCommands(); |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
61 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
62 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
63 |
void __retrieve_python() |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
64 |
{ |
329 | 65 |
/* Check Python thread is not being |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
66 |
* modifying internal python_eval data */ |
329 | 67 |
PythonState = TryLockPython() ? |
68 |
PYTHON_LOCKED_BY_PLC : |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
69 |
PYTHON_LOCKED_BY_PYTHON; |
329 | 70 |
/* If python thread _is_ in, then PythonState remains PYTHON_LOCKED_BY_PYTHON |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
71 |
* and python_eval will no do anything */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
72 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
73 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
74 |
void __publish_python() |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
75 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
76 |
if(PythonState & PYTHON_LOCKED_BY_PLC){ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
77 |
/* If runnig PLC did push something in the fifo*/ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
78 |
if(PythonState & PYTHON_MUSTWAKEUP){ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
79 |
/* WakeUp python thread */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
80 |
UnBlockPythonCommands(); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
81 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
82 |
UnLockPython(); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
83 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
84 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
85 |
/** |
329 | 86 |
* Called by the PLC, each time a python_eval |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
87 |
* FB instance is executed |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
88 |
*/ |
300
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
89 |
void __PythonEvalFB(int poll, PYTHON_EVAL* data__) |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
90 |
{ |
329 | 91 |
/* detect rising edge on TRIG to trigger evaluation */ |
92 |
if(((data__->TRIG && !data__->TRIGM1) || |
|
300
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
93 |
/* polling is equivalent to trig on value rather than on rising edge*/ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
94 |
(poll && data__->TRIG )) && |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
95 |
/* trig only if not already trigged */ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
96 |
data__->TRIGGED == 0){ |
281
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
97 |
/* mark as trigged */ |
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
98 |
data__->TRIGGED = 1; |
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
99 |
/* make a safe copy of the code */ |
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
100 |
data__->PREBUFFER = data__->CODE; |
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
101 |
} |
300
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
102 |
/* retain value for next rising edge detection */ |
281
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
103 |
data__->TRIGM1 = data__->TRIG; |
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
104 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
105 |
/* python thread is not in ? */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
106 |
if( PythonState & PYTHON_LOCKED_BY_PLC){ |
300
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
107 |
/* if some answer are waiting, publish*/ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
108 |
if(data__->STATE == PYTHON_FB_ANSWERED){ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
109 |
/* Copy buffer content into result*/ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
110 |
data__->RESULT = data__->BUFFER; |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
111 |
/* signal result presece to PLC*/ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
112 |
data__->ACK = 1; |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
113 |
/* Mark as free */ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
114 |
data__->STATE = PYTHON_FB_FREE; |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
115 |
/* mark as not trigged */ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
116 |
if(!poll) |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
117 |
data__->TRIGGED = 0; |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
118 |
/*printf("__PythonEvalFB pop %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);*/ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
119 |
}else if(poll){ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
120 |
/* when in polling, no answer == ack down */ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
121 |
data__->ACK = 0; |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
122 |
} |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
123 |
/* got the order to act ?*/ |
281
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
124 |
if(data__->TRIGGED == 1 && |
329 | 125 |
/* and not already being processed */ |
126 |
data__->STATE == PYTHON_FB_FREE) |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
127 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
128 |
/* Enter the block in the fifo |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
129 |
/* Don't have to check if fifo cell is free |
329 | 130 |
* as fifo size == FB count, and a FB cannot |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
131 |
* be requested twice */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
132 |
EvalFBs[Current_PLC_EvalFB] = data__; |
281
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
133 |
/* copy into BUFFER local*/ |
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
134 |
data__->BUFFER = data__->PREBUFFER; |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
135 |
/* Set ACK pin to low so that we can set a rising edge on result */ |
300
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
136 |
if(!poll){ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
137 |
/* when not polling, a new answer imply reseting ack*/ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
138 |
data__->ACK = 0; |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
139 |
}else{ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
140 |
/* when in polling, acting reset trigger */ |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
141 |
data__->TRIGGED = 0; |
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
142 |
} |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
143 |
/* Mark FB busy */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
144 |
data__->STATE = PYTHON_FB_REQUESTED; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
145 |
/* Have to wakeup python thread in case he was asleep */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
146 |
PythonState |= PYTHON_MUSTWAKEUP; |
300
7f7912ae5ee8
Added python_poll FB, to poll python variable without waiting ack
etisserant
parents:
286
diff
changeset
|
147 |
/*printf("__PythonEvalFB push %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);*/ |
281
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
148 |
/* Get a new line */ |
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
149 |
Current_PLC_EvalFB = (Current_PLC_EvalFB + 1) %% %(python_eval_fb_count)d; |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
150 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
151 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
152 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
153 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
154 |
char* PythonIterator(char* result) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
155 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
156 |
char* next_command; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
157 |
PYTHON_EVAL* data__; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
158 |
//printf("PythonIterator result %%s\n", result); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
159 |
/* take python mutex to prevent changing PLC data while PLC running */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
160 |
LockPython(); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
161 |
/* Get current FB */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
162 |
data__ = EvalFBs[Current_Python_EvalFB]; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
163 |
if(data__ && /* may be null at first run */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
164 |
data__->STATE == PYTHON_FB_PROCESSING){ /* some answer awaited*/ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
165 |
/* If result not None */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
166 |
if(result){ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
167 |
/* Get results len */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
168 |
data__->BUFFER.len = strlen(result); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
169 |
/* prevent results overrun */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
170 |
if(data__->BUFFER.len > STR_MAX_LEN) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
171 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
172 |
data__->BUFFER.len = STR_MAX_LEN; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
173 |
/* TODO : signal error */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
174 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
175 |
/* Copy results to buffer */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
176 |
strncpy(data__->BUFFER.body, result, data__->BUFFER.len); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
177 |
}else{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
178 |
data__->BUFFER.len = 0; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
179 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
180 |
/* remove block from fifo*/ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
181 |
EvalFBs[Current_Python_EvalFB] = NULL; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
182 |
/* Mark block as answered */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
183 |
data__->STATE = PYTHON_FB_ANSWERED; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
184 |
/* Get a new line */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
185 |
Current_Python_EvalFB = (Current_Python_EvalFB + 1) %% %(python_eval_fb_count)d; |
281
3f0fc8de99b0
fixed plc_python.c to assure data coherancy from the PLC point of view. Also fix non work multiple python_eval FB instance case.
etisserant
parents:
280
diff
changeset
|
186 |
//printf("PythonIterator ++ Current_Python_EvalFB %%d\n", Current_Python_EvalFB); |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
187 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
188 |
/* while next slot is empty */ |
329 | 189 |
while(((data__ = EvalFBs[Current_Python_EvalFB]) == NULL) || |
190 |
/* or doesn't contain command */ |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
191 |
data__->STATE != PYTHON_FB_REQUESTED) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
192 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
193 |
UnLockPython(); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
194 |
/* wait next FB to eval */ |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
281
diff
changeset
|
195 |
//printf("PythonIterator wait\n"); |
329 | 196 |
if(WaitPythonCommands()) return NULL; |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
281
diff
changeset
|
197 |
/*emergency exit*/ |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
281
diff
changeset
|
198 |
if(PythonState & PYTHON_FINISHED) return NULL; |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
199 |
LockPython(); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
200 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
201 |
/* Mark block as processing */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
202 |
data__->STATE = PYTHON_FB_PROCESSING; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
203 |
//printf("PythonIterator\n"); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
204 |
/* make BUFFER a null terminated string */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
205 |
data__->BUFFER.body[data__->BUFFER.len] = 0; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
206 |
/* next command is BUFFER */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
207 |
next_command = data__->BUFFER.body; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
208 |
/* free python mutex */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
209 |
UnLockPython(); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
210 |
/* return the next command to eval */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
211 |
return next_command; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
212 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff
changeset
|
213 |