author | lbessard |
Fri, 13 Feb 2009 13:58:54 +0100 | |
changeset 314 | ad4db7099355 |
parent 290 | 3bd617ae7a05 |
child 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:
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 |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
3 |
**/ |
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 */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
11 |
extern int common_ticktime__; |
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); |
|
22 |
||
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); |
|
39 |
||
40 |
/* |
|
41 |
printf("SetTimer(%lld,%lld)\n",next, period); |
|
42 |
*/ |
|
43 |
||
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; |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
69 |
HANDLE debug_wait_sem; |
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; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
71 |
HANDLE python_wait_sem; |
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; |
205 | 77 |
/* Translate PLC's microseconds to Ttick nanoseconds */ |
78 |
Ttick = 1000000 * maxval(common_ticktime__,1); |
|
79 |
||
244 | 80 |
debug_sem = CreateSemaphore( |
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 |
85 |
if (debug_sem == NULL) |
|
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 |
} |
|
90 |
||
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
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 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
97 |
if (debug_wait_sem == NULL) |
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 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
103 |
python_sem = CreateSemaphore( |
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 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
109 |
if (python_sem == NULL) |
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 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
114 |
python_wait_sem = CreateSemaphore( |
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 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
121 |
if (python_wait_sem == NULL) |
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 |
|
229 | 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 |
} |
|
244 | 145 |
static int __debug_tick; |
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 */ |
|
244 | 173 |
int WaitDebugData() |
229 | 174 |
{ |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
175 |
WaitForSingleObject(debug_wait_sem, INFINITE); |
244 | 176 |
return __debug_tick; |
229 | 177 |
} |
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 */ |
193 |
WaitForSingleObject(debug_sem, INFINITE); |
|
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 */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
207 |
WaitForSingleObject(python_wait_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
|
208 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
276
diff
changeset
|
209 |
|
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 |