author | laurent |
Fri, 09 Oct 2009 17:52:00 +0200 | |
changeset 416 | 3b3f7f5a8e40 |
parent 397 | 6a7ff66a811d |
child 446 | 1edde533db19 |
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:
276
diff
changeset
|
1 |
/** |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
2 |
* Win32 specific code |
329 | 3 |
**/ |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
4 |
|
205 | 5 |
#include <stdio.h> |
6 |
#include <sys/timeb.h> |
|
7 |
#include <time.h> |
|
8 |
#include <windows.h> |
|
9 |
||
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
10 |
/* provided by POUS.C */ |
397
6a7ff66a811d
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
329
diff
changeset
|
11 |
extern unsigned long common_ticktime__; |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
12 |
|
244 | 13 |
long AtomicCompareExchange(long* atomicvar, long compared, long exchange) |
205 | 14 |
{ |
15 |
return InterlockedCompareExchange(atomicvar, exchange, compared); |
|
16 |
} |
|
17 |
||
18 |
struct _timeb timetmp; |
|
19 |
void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
|
20 |
{ |
|
21 |
_ftime(&timetmp); |
|
329 | 22 |
|
205 | 23 |
(*CURRENT_TIME).tv_sec = timetmp.time; |
24 |
(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000; |
|
25 |
} |
|
26 |
||
27 |
void PLC_timer_notify() |
|
28 |
{ |
|
29 |
PLC_GetTime(&__CURRENT_TIME); |
|
30 |
__run(); |
|
31 |
} |
|
32 |
||
33 |
HANDLE PLC_timer = NULL; |
|
34 |
void PLC_SetTimer(long long next, long long period) |
|
35 |
{ |
|
229 | 36 |
LARGE_INTEGER liDueTime; |
205 | 37 |
/* arg 2 of SetWaitableTimer take 100 ns interval*/ |
38 |
liDueTime.QuadPart = next / (-100); |
|
329 | 39 |
|
205 | 40 |
/* |
41 |
printf("SetTimer(%lld,%lld)\n",next, period); |
|
42 |
*/ |
|
329 | 43 |
|
205 | 44 |
if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0)) |
45 |
{ |
|
46 |
printf("SetWaitableTimer failed (%d)\n", GetLastError()); |
|
47 |
} |
|
229 | 48 |
} |
49 |
||
50 |
/* Variable used to stop plcloop thread */ |
|
51 |
int runplcloop; |
|
52 |
void PlcLoop() |
|
53 |
{ |
|
54 |
runplcloop = 1; |
|
55 |
while(runplcloop) |
|
56 |
{ |
|
57 |
// Set a timer |
|
58 |
PLC_SetTimer(Ttick,Ttick); |
|
205 | 59 |
if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0) |
60 |
{ |
|
61 |
printf("WaitForSingleObject failed (%d)\n", GetLastError()); |
|
62 |
} |
|
63 |
PLC_timer_notify(); |
|
229 | 64 |
} |
205 | 65 |
} |
66 |
||
229 | 67 |
HANDLE PLC_thread; |
244 | 68 |
HANDLE debug_sem; |
329 | 69 |
HANDLE debug_wait_sem; |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
70 |
HANDLE python_sem; |
329 | 71 |
HANDLE python_wait_sem; |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
72 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
73 |
#define maxval(a,b) ((a>b)?a:b) |
229 | 74 |
int startPLC(int argc,char **argv) |
205 | 75 |
{ |
229 | 76 |
unsigned long thread_id = 0; |
397
6a7ff66a811d
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
329
diff
changeset
|
77 |
/* Define Ttick to 1ms if common_ticktime not defined */ |
6a7ff66a811d
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
329
diff
changeset
|
78 |
Ttick = common_ticktime__?common_ticktime__:1000000; |
205 | 79 |
|
329 | 80 |
debug_sem = CreateSemaphore( |
244 | 81 |
NULL, // default security attributes |
82 |
1, // initial count |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
83 |
1, // maximum count |
244 | 84 |
NULL); // unnamed semaphore |
329 | 85 |
if (debug_sem == NULL) |
244 | 86 |
{ |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
87 |
printf("startPLC CreateSemaphore debug_sem error: %d\n", GetLastError()); |
244 | 88 |
return; |
89 |
} |
|
329 | 90 |
|
91 |
debug_wait_sem = CreateSemaphore( |
|
244 | 92 |
NULL, // default security attributes |
93 |
0, // initial count |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
94 |
1, // maximum count |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
95 |
NULL); // unnamed semaphore |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
96 |
|
329 | 97 |
if (debug_wait_sem == NULL) |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
98 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
99 |
printf("startPLC CreateSemaphore debug_wait_sem error: %d\n", GetLastError()); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
100 |
return; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
101 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
102 |
|
329 | 103 |
python_sem = CreateSemaphore( |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
104 |
NULL, // default security attributes |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
105 |
1, // initial count |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
106 |
1, // maximum count |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
107 |
NULL); // unnamed semaphore |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
108 |
|
329 | 109 |
if (python_sem == NULL) |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
110 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
111 |
printf("startPLC CreateSemaphore python_sem error: %d\n", GetLastError()); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
112 |
return; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
113 |
} |
329 | 114 |
python_wait_sem = CreateSemaphore( |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
115 |
NULL, // default security attributes |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
116 |
0, // initial count |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
117 |
1, // maximum count |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
118 |
NULL); // unnamed semaphore |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
119 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
120 |
|
329 | 121 |
if (python_wait_sem == NULL) |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
122 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
123 |
printf("startPLC CreateSemaphore python_wait_sem error: %d\n", GetLastError()); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
124 |
return; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
125 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
126 |
|
329 | 127 |
|
205 | 128 |
/* Create a waitable timer */ |
129 |
PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer"); |
|
130 |
if(NULL == PLC_timer) |
|
131 |
{ |
|
132 |
printf("CreateWaitableTimer failed (%d)\n", GetLastError()); |
|
133 |
return 1; |
|
134 |
} |
|
135 |
if( __init(argc,argv) == 0 ) |
|
136 |
{ |
|
137 |
printf("Tick Time : %d ms\n", common_ticktime__); |
|
229 | 138 |
PLC_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlcLoop, NULL, 0, &thread_id); |
205 | 139 |
} |
229 | 140 |
else{ |
141 |
return 1; |
|
142 |
} |
|
205 | 143 |
return 0; |
144 |
} |
|
397
6a7ff66a811d
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
329
diff
changeset
|
145 |
static unsigned long __debug_tick; |
244 | 146 |
|
147 |
int TryEnterDebugSection(void) |
|
148 |
{ |
|
149 |
//printf("TryEnterDebugSection\n"); |
|
150 |
return WaitForSingleObject(debug_sem, 0) == WAIT_OBJECT_0; |
|
151 |
} |
|
152 |
||
153 |
void LeaveDebugSection(void) |
|
154 |
{ |
|
155 |
ReleaseSemaphore(debug_sem, 1, NULL); |
|
156 |
//printf("LeaveDebugSection\n"); |
|
157 |
} |
|
229 | 158 |
|
159 |
int stopPLC() |
|
160 |
{ |
|
161 |
runplcloop = 0; |
|
162 |
WaitForSingleObject(PLC_thread, INFINITE); |
|
244 | 163 |
__cleanup(); |
164 |
__debug_tick = -1; |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
165 |
ReleaseSemaphore(debug_wait_sem, 1, NULL); |
244 | 166 |
CloseHandle(debug_sem); |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
167 |
CloseHandle(debug_wait_sem); |
244 | 168 |
CloseHandle(PLC_timer); |
229 | 169 |
CloseHandle(PLC_thread); |
170 |
} |
|
171 |
||
172 |
/* from plc_debugger.c */ |
|
397
6a7ff66a811d
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
329
diff
changeset
|
173 |
unsigned long WaitDebugData() |
229 | 174 |
{ |
329 | 175 |
if(WaitForSingleObject(debug_wait_sem, INFINITE) != WAIT_OBJECT_0) return -1; |
244 | 176 |
return __debug_tick; |
229 | 177 |
} |
329 | 178 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
179 |
/* Called by PLC thread when debug_publish finished |
229 | 180 |
* This is supposed to unlock debugger thread in WaitDebugData*/ |
181 |
void InitiateDebugTransfer() |
|
182 |
{ |
|
244 | 183 |
/* remember tick */ |
184 |
__debug_tick = __tick; |
|
185 |
/* signal debugger thread it can read data */ |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
186 |
ReleaseSemaphore(debug_wait_sem, 1, NULL); |
229 | 187 |
} |
244 | 188 |
|
189 |
void suspendDebug() |
|
190 |
{ |
|
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
280
diff
changeset
|
191 |
__DEBUG = 0; |
244 | 192 |
/* Prevent PLC to enter debug code */ |
329 | 193 |
WaitForSingleObject(debug_sem, INFINITE); |
244 | 194 |
} |
195 |
||
196 |
void resumeDebug() |
|
197 |
{ |
|
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
280
diff
changeset
|
198 |
__DEBUG = 1; |
244 | 199 |
/* Let PLC enter debug code */ |
200 |
ReleaseSemaphore(debug_sem, 1, NULL); |
|
201 |
} |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
202 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
203 |
/* from plc_python.c */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
204 |
int WaitPythonCommands(void) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
205 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
206 |
/* Wait signal from PLC thread */ |
329 | 207 |
return WaitForSingleObject(python_wait_sem, INFINITE); |
208 |
} |
|
209 |
||
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
210 |
/* Called by PLC thread on each new python command*/ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
211 |
void UnBlockPythonCommands(void) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
212 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
213 |
/* signal debugger thread it can read data */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
214 |
ReleaseSemaphore(python_wait_sem, 1, NULL); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
215 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
216 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
217 |
int TryLockPython(void) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
218 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
219 |
return WaitForSingleObject(python_sem, 0) == WAIT_OBJECT_0; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
220 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
221 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
222 |
void UnLockPython(void) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
223 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
224 |
ReleaseSemaphore(python_sem, 1, NULL); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
225 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
226 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
227 |
void LockPython(void) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
228 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
229 |
WaitForSingleObject(python_sem, INFINITE); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
230 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
231 |