targets/plc_python.c
author etisserant
Sun, 04 Jan 2009 17:29:12 +0100
changeset 286 a2a8a52b0d4f
parent 281 3f0fc8de99b0
child 300 7f7912ae5ee8
permissions -rw-r--r--
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
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
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
     3
 * 
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
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
     5
 * with the help of C pragmas inserted in python_eval FB code 
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
     6
 * 
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
     7
 * Buffer content is read asynchronously, (from non real time part), 
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.
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
     9
 * 
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.
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
    14
 * 
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
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
    33
 
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
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
    65
	/* Check Python thread is not being 
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 */
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
    67
	PythonState = TryLockPython() ? 
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
    68
	                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
    69
	                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
    70
	/* If python thread _is_ in, then PythonState remains 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
    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
/**
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
    86
 * Called by the PLC, each time a python_eval 
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
 */
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
    89
void __PythonEvalFB(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
    90
{
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
    91
	/* detect rising edge on 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
    92
	if(data__->TRIG && !data__->TRIGM1 && data__->TRIGGED == 0){
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
    93
		/* 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
    94
		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
    95
		/* 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
    96
		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
    97
	}
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
	/* retain value for next detection */
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
	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
   100
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   101
	/* 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
   102
	if( PythonState & PYTHON_LOCKED_BY_PLC){
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
		/* got the order to act */
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
		if(data__->TRIGGED == 1 &&
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   105
		   /* and not already being processed */ 
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   106
		   data__->STATE == PYTHON_FB_FREE) 
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   107
		{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   108
			/* 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
   109
			/* Don't have to check if fifo cell is free
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   110
			 * as fifo size == FB count, and a FB cannot 
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   111
			 * 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
   112
			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
   113
			/* 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
   114
			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
   115
			/* Set ACK pin to low so that we can set a rising edge on result */
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   116
			data__->ACK = 0;
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   117
			/* 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
   118
			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
   119
			/* 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
   120
			PythonState |= PYTHON_MUSTWAKEUP;
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
   121
			//printf("__PythonEvalFB push %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);
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
   122
			/* 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
   123
			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
   124
		}else if(data__->STATE == PYTHON_FB_ANSWERED){
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
   125
			/* Copy buffer content into result*/
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   126
			data__->RESULT = data__->BUFFER;
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
   127
			/* signal result presece to PLC*/
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   128
			data__->ACK = 1;
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
   129
			/* Mark as free */
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   130
			data__->STATE = PYTHON_FB_FREE;
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
   131
			/* mark as not 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
   132
			data__->TRIGGED = 0;
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
			//printf("__PythonEvalFB pop %%d - %%*s\n",Current_PLC_EvalFB, data__->BUFFER.len, data__->BUFFER.body);
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   134
		}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   135
	}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   136
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   137
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   138
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
   139
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   140
	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
   141
	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
   142
	//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
   143
	/* 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
   144
	LockPython();
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   145
	/* 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
   146
	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
   147
	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
   148
	   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
   149
	   	/* 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
   150
	   	if(result){
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   151
			/* 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
   152
			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
   153
			/* 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
   154
			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
   155
			{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   156
				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
   157
				/* 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
   158
			}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   159
			/* 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
   160
			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
   161
	   	}else{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   162
	   		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
   163
	   	}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   164
		/* 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
   165
		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
   166
		/* 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
   167
		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
   168
		/* 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
   169
		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
   170
		//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
   171
	}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   172
	/* while next slot is empty */
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   173
	while(((data__ = 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
   174
	 	  /* or doesn't contain command */ 
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   175
	      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
   176
	{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   177
		UnLockPython();
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   178
		/* 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
   179
		//printf("PythonIterator wait\n");
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   180
		WaitPythonCommands();
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
   181
		/*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
   182
		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
   183
		LockPython();
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   184
	}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   185
	/* 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
   186
	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
   187
	//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
   188
	/* 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
   189
	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
   190
	/* 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
   191
	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
   192
	/* 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
   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
	/* 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
   195
	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
   196
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
diff changeset
   197