targets/Win32/plc_Win32_main.c
changeset 229 8bc65076e290
parent 205 ee8d1f4528ef
child 244 85e92d9e34a8
equal deleted inserted replaced
228:848da15cf513 229:8bc65076e290
    29 }
    29 }
    30 
    30 
    31 HANDLE PLC_timer = NULL;
    31 HANDLE PLC_timer = NULL;
    32 void PLC_SetTimer(long long next, long long period)
    32 void PLC_SetTimer(long long next, long long period)
    33 {
    33 {
    34 	LARGE_INTEGER liDueTime;		
    34 	LARGE_INTEGER liDueTime;
    35 	/* arg 2 of SetWaitableTimer take 100 ns interval*/
    35 	/* arg 2 of SetWaitableTimer take 100 ns interval*/
    36 	liDueTime.QuadPart =  next / (-100);
    36 	liDueTime.QuadPart =  next / (-100);
    37 	
    37 	
    38 	/*
    38 	/*
    39 	printf("SetTimer(%lld,%lld)\n",next, period);
    39 	printf("SetTimer(%lld,%lld)\n",next, period);
    41 	
    41 	
    42 	if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
    42 	if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
    43     {
    43     {
    44         printf("SetWaitableTimer failed (%d)\n", GetLastError());
    44         printf("SetWaitableTimer failed (%d)\n", GetLastError());
    45     }
    45     }
       
    46 }
       
    47 
       
    48 /* Variable used to stop plcloop thread */
       
    49 int runplcloop;
       
    50 void PlcLoop()
       
    51 {
       
    52 	runplcloop = 1;
       
    53 	while(runplcloop)
       
    54 	{
       
    55 	// Set a timer
       
    56 	PLC_SetTimer(Ttick,Ttick);
    46 	if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
    57 	if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
    47 	{
    58 	{
    48 		printf("WaitForSingleObject failed (%d)\n", GetLastError());
    59 		printf("WaitForSingleObject failed (%d)\n", GetLastError());
    49 	}
    60 	}
    50 	PLC_timer_notify();
    61 	PLC_timer_notify();
       
    62 	}
    51 }
    63 }
    52 
    64 
    53 int main(int argc,char **argv)
    65 HANDLE DebugLock;
       
    66 HANDLE PLC_thread;
       
    67 
       
    68 int startPLC(int argc,char **argv)
    54 {
    69 {
       
    70 	unsigned long thread_id = 0;
    55 	/* Translate PLC's microseconds to Ttick nanoseconds */
    71 	/* Translate PLC's microseconds to Ttick nanoseconds */
    56 	Ttick = 1000000 * maxval(common_ticktime__,1);
    72 	Ttick = 1000000 * maxval(common_ticktime__,1);
    57 
    73 
       
    74 	DebugLock = CreateMutex( 
       
    75 	        	NULL,              // default security attributes
       
    76 	        	FALSE,             // initially not owned
       
    77 	        	NULL);             // unnamed mutex
       
    78 
       
    79     if (DebugLock == NULL) 
       
    80     {
       
    81         printf("CreateMutex error: %d\n", GetLastError());
       
    82         return;
       
    83     }
       
    84 	
    58 	/* Create a waitable timer */
    85 	/* Create a waitable timer */
    59     PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
    86     PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
    60     if(NULL == PLC_timer)
    87     if(NULL == PLC_timer)
    61     {
    88     {
    62         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
    89         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
    63         return 1;
    90         return 1;
    64     }
    91     }
    65 
       
    66     if( __init(argc,argv) == 0 )
    92     if( __init(argc,argv) == 0 )
    67     {
    93     {
    68     	printf("Tick Time : %d ms\n", common_ticktime__);
    94     	printf("Tick Time : %d ms\n", common_ticktime__);
    69     	while(1)
    95     	PLC_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlcLoop, NULL, 0, &thread_id);
    70     	{
       
    71     		// Set a timer
       
    72     		PLC_SetTimer(Ttick,Ttick);
       
    73     		if (kbhit())
       
    74     		{
       
    75     			printf("Finishing\n");
       
    76     		    break;
       
    77             }
       
    78     	}
       
    79     	PLC_SetTimer(0,0);
       
    80     }
    96     }
    81     __cleanup();
    97     else{
    82     CloseHandle(PLC_timer);
    98     	return 1;
    83     		
    99     }
    84     return 0;
   100     return 0;
    85 }
   101 }
       
   102 
       
   103 int stopPLC()
       
   104 {
       
   105 	runplcloop = 0;
       
   106 	WaitForSingleObject(PLC_thread, INFINITE);
       
   107 	CloseHandle(PLC_thread);
       
   108 	CloseHandle(DebugLock);
       
   109     CloseHandle(PLC_timer);
       
   110     __cleanup();
       
   111 }
       
   112 
       
   113 /* from plc_debugger.c */
       
   114 void WaitDebugData()
       
   115 {
       
   116 	DWORD dwWaitResult;
       
   117 	dwWaitResult = WaitForSingleObject( 
       
   118 					DebugLock,  // handle to mutex
       
   119 					INFINITE);  // no time-out interval
       
   120 }
       
   121  
       
   122 /* Called by PLC thread when debug_publish finished
       
   123  * This is supposed to unlock debugger thread in WaitDebugData*/
       
   124 void InitiateDebugTransfer()
       
   125 {
       
   126     /* signal debugger thread to continue*/
       
   127 	ReleaseMutex(DebugLock);
       
   128 }