3 #include <time.h> |
3 #include <time.h> |
4 #include <windows.h> |
4 #include <windows.h> |
5 |
5 |
6 int localcount = 0; |
6 int localcount = 0; |
7 |
7 |
8 void timer_notify() |
8 struct _timeb timetmp; |
|
9 void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
9 { |
10 { |
10 struct _timeb timebuffer; |
11 _ftime(&timetmp); |
11 if(++localcount % 50 == 0){ |
12 |
12 printf("PLC tick : %d\n",localcount); |
13 (*CURRENT_TIME).tv_sec = timetmp.time; |
13 fflush(stdout); |
14 (*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000; |
14 } |
15 } |
15 |
16 |
16 _ftime( &timebuffer ); |
17 void PLC_timer_notify() |
17 __CURRENT_TIME.tv_sec = timebuffer.time; |
18 { |
18 __CURRENT_TIME.tv_nsec = timebuffer.millitm * 1000000; |
19 PLC_GetTime(&__CURRENT_TIME); |
19 __run(); |
20 __run(); |
|
21 } |
|
22 |
|
23 HANDLE PLC_timer = NULL; |
|
24 void PLC_SetTimer(long long next, long long period) |
|
25 { |
|
26 LARGE_INTEGER liDueTime; |
|
27 /* arg 2 of SetWaitableTimer take 100 ns interval*/ |
|
28 liDueTime.QuadPart = next / (-100); |
|
29 |
|
30 /* |
|
31 printf("SetTimer(%lld,%lld)\n",next, period); |
|
32 */ |
|
33 |
|
34 if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0)) |
|
35 { |
|
36 printf("SetWaitableTimer failed (%d)\n", GetLastError()); |
|
37 } |
|
38 if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0) |
|
39 { |
|
40 printf("WaitForSingleObject failed (%d)\n", GetLastError()); |
|
41 } |
|
42 PLC_timer_notify(); |
20 } |
43 } |
21 |
44 |
22 int main(int argc,char **argv) |
45 int main(int argc,char **argv) |
23 { |
46 { |
24 HANDLE hTimer = NULL; |
47 /* Translate PLC's microseconds to Ttick nanoseconds */ |
25 LARGE_INTEGER liDueTime; |
48 Ttick = 1000000 * maxval(common_ticktime__,1); |
26 |
49 |
27 liDueTime.QuadPart = -10000 * maxval(common_ticktime__,1); |
50 /* Create a waitable timer */ |
28 |
51 PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer"); |
29 // Create a waitable timer. |
52 if(NULL == PLC_timer) |
30 hTimer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer"); |
|
31 if (NULL == hTimer) |
|
32 { |
53 { |
33 printf("CreateWaitableTimer failed (%d)\n", GetLastError()); |
54 printf("CreateWaitableTimer failed (%d)\n", GetLastError()); |
34 return 1; |
55 return 1; |
35 } |
56 } |
36 |
57 |
37 if( __init(argc,argv) == 0 ){ |
58 if( __init(argc,argv) == 0 ) |
38 |
59 { |
39 printf("Tick Time : %d ms\n", common_ticktime__); |
60 printf("Tick Time : %d ms\n", common_ticktime__); |
40 // Set a timer |
61 while(1) |
41 if (!SetWaitableTimer(hTimer, &liDueTime, common_ticktime__, NULL, NULL, 0)) |
62 { |
42 { |
63 // Set a timer |
43 printf("SetWaitableTimer failed (%d)\n", GetLastError()); |
64 PLC_SetTimer(Ttick,Ttick); |
44 return 2; |
65 if (kbhit()) |
45 } |
66 { |
46 |
67 printf("Finishing\n"); |
47 while(1){ |
68 break; |
48 // Wait for the timer. |
|
49 if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0) |
|
50 { |
|
51 printf("WaitForSingleObject failed (%d)\n", GetLastError()); |
|
52 break; |
|
53 } |
69 } |
54 if (kbhit()) |
70 } |
55 { |
71 PLC_SetTimer(0,0); |
56 printf("Finishing\n"); |
|
57 break; |
|
58 } |
|
59 timer_notify(); |
|
60 } |
|
61 } |
72 } |
62 __cleanup(); |
73 __cleanup(); |
63 |
74 CloseHandle(PLC_timer); |
|
75 |
64 return 0; |
76 return 0; |
65 } |
77 } |