* Python Asynchronous execution code
* PLC put python commands in a fifo, respecting execution order
* with the help of C pragmas inserted in python_eval FB code
* Buffer content is read asynchronously, (from non real time part),
* commands are executed and result stored for later use by PLC.
* In this implementation, fifo is a list of pointer to python_eval
* function blocks structures. Some local variables have been added in
* python_eval interface. We use those local variables as buffer and state
#include "iec_types_all.h"
/* The fifo (fixed size, as number of FB is fixed) */
static PYTHON_EVAL* EvalFBs[%(python_eval_fb_count)d];
/* Producer and consumer cursors */
static int Current_PLC_EvalFB;
static int Current_Python_EvalFB;
/* A global IEC-Python gateway state, for use inside python_eval FBs*/
#define PYTHON_LOCKED_BY_PYTHON 0
#define PYTHON_LOCKED_BY_PLC 1
#define PYTHON_MUSTWAKEUP 2
#define PYTHON_FINISHED 4
/* Each python_eval FunctionBlock have it own state */
#define PYTHON_FB_REQUESTED 1
#define PYTHON_FB_PROCESSING 2
#define PYTHON_FB_ANSWERED 3
int WaitPythonCommands(void);
void UnBlockPythonCommands(void);
Current_Python_EvalFB = 0;
PythonState = PYTHON_LOCKED_BY_PYTHON;
for(i = 0; i < %(python_eval_fb_count)d; i++)
PythonState = PYTHON_FINISHED;
/* Check Python thread is not being
* modifying internal python_eval data */
PythonState = TryLockPython() ?
/* If python thread _is_ in, then PythonState remains PYTHON_LOCKED_BY_PYTHON
* and python_eval will no do anything */
if(PythonState & PYTHON_LOCKED_BY_PLC){
/* If runnig PLC did push something in the fifo*/
if(PythonState & PYTHON_MUSTWAKEUP){
/* WakeUp python thread */
* Called by the PLC, each time a python_eval
* FB instance is executed
void __PythonEvalFB(PYTHON_EVAL* data__)
/* detect rising edge on TRIG */
if(data__->TRIG && !data__->TRIGM1 && data__->TRIGGED == 0){
/* make a safe copy of the code */
data__->PREBUFFER = data__->CODE;
/* retain value for next detection */
data__->TRIGM1 = data__->TRIG;
/* python thread is not in ? */
if( PythonState & PYTHON_LOCKED_BY_PLC){
/* got the order to act */
if(data__->TRIGGED == 1 &&
/* and not already being processed */
data__->STATE == PYTHON_FB_FREE)
/* Enter the block in the fifo
/* Don't have to check if fifo cell is free
* as fifo size == FB count, and a FB cannot
EvalFBs[Current_PLC_EvalFB] = data__;
/* copy into BUFFER local*/
data__->BUFFER = data__->PREBUFFER;
/* Set ACK pin to low so that we can set a rising edge on result */
data__->STATE = PYTHON_FB_REQUESTED;
/* Have to wakeup python thread in case he was asleep */
PythonState |= PYTHON_MUSTWAKEUP;
//printf("__PythonEvalFB push %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);
Current_PLC_EvalFB = (Current_PLC_EvalFB + 1) %% %(python_eval_fb_count)d;
}else if(data__->STATE == PYTHON_FB_ANSWERED){
/* Copy buffer content into result*/
data__->RESULT = data__->BUFFER;
/* signal result presece to PLC*/
data__->STATE = PYTHON_FB_FREE;
/* mark as not trigged */
//printf("__PythonEvalFB pop %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);
char* PythonIterator(char* result)
//printf("PythonIterator result %%s\n", result);
/* take python mutex to prevent changing PLC data while PLC running */
data__ = EvalFBs[Current_Python_EvalFB];
if(data__ && /* may be null at first run */
data__->STATE == PYTHON_FB_PROCESSING){ /* some answer awaited*/
data__->BUFFER.len = strlen(result);
/* prevent results overrun */
if(data__->BUFFER.len > STR_MAX_LEN)
data__->BUFFER.len = STR_MAX_LEN;
/* TODO : signal error */
/* Copy results to buffer */
strncpy(data__->BUFFER.body, result, data__->BUFFER.len);
/* remove block from fifo*/
EvalFBs[Current_Python_EvalFB] = NULL;
/* Mark block as answered */
data__->STATE = PYTHON_FB_ANSWERED;
Current_Python_EvalFB = (Current_Python_EvalFB + 1) %% %(python_eval_fb_count)d;
//printf("PythonIterator ++ Current_Python_EvalFB %%d\n", Current_Python_EvalFB);
/* while next slot is empty */
while(((data__ = EvalFBs[Current_Python_EvalFB]) == NULL) ||
/* or doesn't contain command */
data__->STATE != PYTHON_FB_REQUESTED)
/* wait next FB to eval */
//printf("PythonIterator wait\n");
if(PythonState & PYTHON_FINISHED) return NULL;
/* Mark block as processing */
data__->STATE = PYTHON_FB_PROCESSING;
//printf("PythonIterator\n");
/* make BUFFER a null terminated string */
data__->BUFFER.body[data__->BUFFER.len] = 0;
/* next command is BUFFER */
next_command = data__->BUFFER.body;
/* return the next command to eval */