etisserant@49: #include etisserant@49: #include etisserant@49: #include etisserant@49: #include etisserant@49: etisserant@203: long AtomicCompareExchange(long* atomicvar,long exchange, long compared) etisserant@203: { etisserant@203: return InterlockedCompareExchange(atomicvar, exchange, compared); etisserant@203: } etisserant@203: etisserant@203: //long AtomicExchange(long* atomicvar,long exchange) etisserant@203: //{ etisserant@203: // return InterlockedExchange(atomicvar, exchange); etisserant@203: //} etisserant@110: greg@196: struct _timeb timetmp; greg@196: void PLC_GetTime(IEC_TIME *CURRENT_TIME) etisserant@49: { greg@196: _ftime(&timetmp); greg@196: greg@196: (*CURRENT_TIME).tv_sec = timetmp.time; greg@196: (*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000; greg@196: } etisserant@49: greg@196: void PLC_timer_notify() greg@196: { greg@196: PLC_GetTime(&__CURRENT_TIME); greg@196: __run(); greg@196: } greg@196: greg@196: HANDLE PLC_timer = NULL; greg@196: void PLC_SetTimer(long long next, long long period) greg@196: { greg@196: LARGE_INTEGER liDueTime; greg@196: /* arg 2 of SetWaitableTimer take 100 ns interval*/ greg@196: liDueTime.QuadPart = next / (-100); greg@196: greg@196: /* greg@196: printf("SetTimer(%lld,%lld)\n",next, period); greg@196: */ greg@196: greg@196: if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0)) greg@196: { greg@196: printf("SetWaitableTimer failed (%d)\n", GetLastError()); greg@196: } greg@196: if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0) greg@196: { greg@196: printf("WaitForSingleObject failed (%d)\n", GetLastError()); greg@196: } greg@196: PLC_timer_notify(); etisserant@49: } etisserant@49: etisserant@49: int main(int argc,char **argv) etisserant@49: { greg@196: /* Translate PLC's microseconds to Ttick nanoseconds */ greg@196: Ttick = 1000000 * maxval(common_ticktime__,1); etisserant@49: greg@196: /* Create a waitable timer */ greg@196: PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer"); greg@196: if(NULL == PLC_timer) etisserant@49: { etisserant@49: printf("CreateWaitableTimer failed (%d)\n", GetLastError()); etisserant@49: return 1; etisserant@49: } etisserant@49: greg@196: if( __init(argc,argv) == 0 ) greg@196: { etisserant@75: printf("Tick Time : %d ms\n", common_ticktime__); greg@196: while(1) greg@196: { greg@196: // Set a timer greg@196: PLC_SetTimer(Ttick,Ttick); greg@196: if (kbhit()) greg@196: { greg@196: printf("Finishing\n"); greg@196: break; etisserant@57: } greg@196: } greg@196: PLC_SetTimer(0,0); etisserant@49: } etisserant@49: __cleanup(); greg@196: CloseHandle(PLC_timer); greg@196: etisserant@49: return 0; etisserant@49: }