34 void PLC_SetTimer(long long next, long long period) |
34 void PLC_SetTimer(long long next, long long period) |
35 { |
35 { |
36 LARGE_INTEGER liDueTime; |
36 LARGE_INTEGER liDueTime; |
37 /* arg 2 of SetWaitableTimer take 100 ns interval*/ |
37 /* arg 2 of SetWaitableTimer take 100 ns interval*/ |
38 liDueTime.QuadPart = next / (-100); |
38 liDueTime.QuadPart = next / (-100); |
39 |
39 |
40 /* |
40 /* |
41 printf("SetTimer(%lld,%lld)\n",next, period); |
41 printf("SetTimer(%lld,%lld)\n",next, period); |
42 */ |
42 */ |
43 |
43 |
44 if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0)) |
44 if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0)) |
45 { |
45 { |
46 printf("SetWaitableTimer failed (%d)\n", GetLastError()); |
46 printf("SetWaitableTimer failed (%d)\n", GetLastError()); |
47 } |
47 } |
48 } |
48 } |
64 } |
64 } |
65 } |
65 } |
66 |
66 |
67 HANDLE PLC_thread; |
67 HANDLE PLC_thread; |
68 HANDLE debug_sem; |
68 HANDLE debug_sem; |
69 HANDLE debug_wait_sem; |
69 HANDLE debug_wait_sem; |
70 HANDLE python_sem; |
70 HANDLE python_sem; |
71 HANDLE python_wait_sem; |
71 HANDLE python_wait_sem; |
72 |
72 |
73 #define maxval(a,b) ((a>b)?a:b) |
73 #define maxval(a,b) ((a>b)?a:b) |
74 int startPLC(int argc,char **argv) |
74 int startPLC(int argc,char **argv) |
75 { |
75 { |
76 unsigned long thread_id = 0; |
76 unsigned long thread_id = 0; |
77 /* Translate PLC's microseconds to Ttick nanoseconds */ |
77 /* Translate PLC's microseconds to Ttick nanoseconds */ |
78 Ttick = 1000000 * maxval(common_ticktime__,1); |
78 Ttick = 1000000 * maxval(common_ticktime__,1); |
79 |
79 |
80 debug_sem = CreateSemaphore( |
80 debug_sem = CreateSemaphore( |
81 NULL, // default security attributes |
81 NULL, // default security attributes |
82 1, // initial count |
82 1, // initial count |
83 1, // maximum count |
83 1, // maximum count |
84 NULL); // unnamed semaphore |
84 NULL); // unnamed semaphore |
85 if (debug_sem == NULL) |
85 if (debug_sem == NULL) |
86 { |
86 { |
87 printf("startPLC CreateSemaphore debug_sem error: %d\n", GetLastError()); |
87 printf("startPLC CreateSemaphore debug_sem error: %d\n", GetLastError()); |
88 return; |
88 return; |
89 } |
89 } |
90 |
90 |
91 debug_wait_sem = CreateSemaphore( |
91 debug_wait_sem = CreateSemaphore( |
92 NULL, // default security attributes |
92 NULL, // default security attributes |
93 0, // initial count |
93 0, // initial count |
94 1, // maximum count |
94 1, // maximum count |
95 NULL); // unnamed semaphore |
95 NULL); // unnamed semaphore |
96 |
96 |
97 if (debug_wait_sem == NULL) |
97 if (debug_wait_sem == NULL) |
98 { |
98 { |
99 printf("startPLC CreateSemaphore debug_wait_sem error: %d\n", GetLastError()); |
99 printf("startPLC CreateSemaphore debug_wait_sem error: %d\n", GetLastError()); |
100 return; |
100 return; |
101 } |
101 } |
102 |
102 |
103 python_sem = CreateSemaphore( |
103 python_sem = CreateSemaphore( |
104 NULL, // default security attributes |
104 NULL, // default security attributes |
105 1, // initial count |
105 1, // initial count |
106 1, // maximum count |
106 1, // maximum count |
107 NULL); // unnamed semaphore |
107 NULL); // unnamed semaphore |
108 |
108 |
109 if (python_sem == NULL) |
109 if (python_sem == NULL) |
110 { |
110 { |
111 printf("startPLC CreateSemaphore python_sem error: %d\n", GetLastError()); |
111 printf("startPLC CreateSemaphore python_sem error: %d\n", GetLastError()); |
112 return; |
112 return; |
113 } |
113 } |
114 python_wait_sem = CreateSemaphore( |
114 python_wait_sem = CreateSemaphore( |
115 NULL, // default security attributes |
115 NULL, // default security attributes |
116 0, // initial count |
116 0, // initial count |
117 1, // maximum count |
117 1, // maximum count |
118 NULL); // unnamed semaphore |
118 NULL); // unnamed semaphore |
119 |
119 |
120 |
120 |
121 if (python_wait_sem == NULL) |
121 if (python_wait_sem == NULL) |
122 { |
122 { |
123 printf("startPLC CreateSemaphore python_wait_sem error: %d\n", GetLastError()); |
123 printf("startPLC CreateSemaphore python_wait_sem error: %d\n", GetLastError()); |
124 return; |
124 return; |
125 } |
125 } |
126 |
126 |
127 |
127 |
128 /* Create a waitable timer */ |
128 /* Create a waitable timer */ |
129 PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer"); |
129 PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer"); |
130 if(NULL == PLC_timer) |
130 if(NULL == PLC_timer) |
131 { |
131 { |
132 printf("CreateWaitableTimer failed (%d)\n", GetLastError()); |
132 printf("CreateWaitableTimer failed (%d)\n", GetLastError()); |
202 |
202 |
203 /* from plc_python.c */ |
203 /* from plc_python.c */ |
204 int WaitPythonCommands(void) |
204 int WaitPythonCommands(void) |
205 { |
205 { |
206 /* Wait signal from PLC thread */ |
206 /* Wait signal from PLC thread */ |
207 WaitForSingleObject(python_wait_sem, INFINITE); |
207 return WaitForSingleObject(python_wait_sem, INFINITE); |
208 } |
208 } |
209 |
209 |
210 /* Called by PLC thread on each new python command*/ |
210 /* Called by PLC thread on each new python command*/ |
211 void UnBlockPythonCommands(void) |
211 void UnBlockPythonCommands(void) |
212 { |
212 { |
213 /* signal debugger thread it can read data */ |
213 /* signal debugger thread it can read data */ |
214 ReleaseSemaphore(python_wait_sem, 1, NULL); |
214 ReleaseSemaphore(python_wait_sem, 1, NULL); |