targets/Win32/plc_Win32_main.c
author lbessard
Fri, 13 Feb 2009 13:58:54 +0100
changeset 314 ad4db7099355
parent 290 3bd617ae7a05
child 329 22e65b8e20f4
permissions -rw-r--r--
Bug with fonts on some systems fixed
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
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     5
#include <stdio.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     6
#include <sys/timeb.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     7
#include <time.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     8
#include <windows.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     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
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    13
long AtomicCompareExchange(long* atomicvar, long compared, long exchange)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    14
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    15
    return InterlockedCompareExchange(atomicvar, exchange, compared);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    16
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    17
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    18
struct _timeb timetmp;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    19
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    20
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    21
	_ftime(&timetmp);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    22
	
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    23
	(*CURRENT_TIME).tv_sec = timetmp.time;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    24
	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    25
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    26
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    27
void PLC_timer_notify()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    28
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    29
    PLC_GetTime(&__CURRENT_TIME);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    30
    __run();
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    31
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    32
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    33
HANDLE PLC_timer = NULL;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    34
void PLC_SetTimer(long long next, long long period)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    35
{
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    36
	LARGE_INTEGER liDueTime;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    37
	/* arg 2 of SetWaitableTimer take 100 ns interval*/
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    38
	liDueTime.QuadPart =  next / (-100);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    39
	
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    40
	/*
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    41
	printf("SetTimer(%lld,%lld)\n",next, period);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    42
	*/
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    43
	
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    44
	if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    45
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    46
        printf("SetWaitableTimer failed (%d)\n", GetLastError());
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    47
    }
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    48
}
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    49
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    50
/* Variable used to stop plcloop thread */
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    51
int runplcloop;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    52
void PlcLoop()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    53
{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    54
	runplcloop = 1;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    55
	while(runplcloop)
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    56
	{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    57
	// Set a timer
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    58
	PLC_SetTimer(Ttick,Ttick);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    59
	if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    60
	{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    61
		printf("WaitForSingleObject failed (%d)\n", GetLastError());
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    62
	}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    63
	PLC_timer_notify();
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    64
	}
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    65
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    66
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    67
HANDLE PLC_thread;
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    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
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    74
int startPLC(int argc,char **argv)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    75
{
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    76
	unsigned long thread_id = 0;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    77
	/* Translate PLC's microseconds to Ttick nanoseconds */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    78
	Ttick = 1000000 * maxval(common_ticktime__,1);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    79
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    80
	debug_sem = CreateSemaphore( 
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    81
							NULL,           // default security attributes
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    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
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    84
					        NULL);          // unnamed semaphore
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    85
    if (debug_sem == NULL) 
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    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
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    88
        return;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    89
    }
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    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
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    92
					        NULL,           // default security attributes
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    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
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   127
	
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   128
	/* Create a waitable timer */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   129
    PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   130
    if(NULL == PLC_timer)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   131
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   132
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   133
        return 1;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   134
    }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   135
    if( __init(argc,argv) == 0 )
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   136
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   137
    	printf("Tick Time : %d ms\n", common_ticktime__);
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   138
    	PLC_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlcLoop, NULL, 0, &thread_id);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   139
    }
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   140
    else{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   141
    	return 1;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   142
    }
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   143
    return 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   144
}
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   145
static int __debug_tick;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   146
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   147
int TryEnterDebugSection(void)
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   148
{
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   149
	//printf("TryEnterDebugSection\n");
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   150
	return WaitForSingleObject(debug_sem, 0) == WAIT_OBJECT_0;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   151
}
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   152
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   153
void LeaveDebugSection(void)
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   154
{
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   155
	ReleaseSemaphore(debug_sem, 1, NULL);
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   156
    //printf("LeaveDebugSection\n");
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   157
}
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   158
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   159
int stopPLC()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   160
{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   161
	runplcloop = 0;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   162
	WaitForSingleObject(PLC_thread, INFINITE);
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   163
	__cleanup();
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   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
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   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
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   168
	CloseHandle(PLC_timer);
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   169
	CloseHandle(PLC_thread);
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   170
}
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   171
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   172
/* from plc_debugger.c */
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   173
int WaitDebugData()
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   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
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   176
	return __debug_tick;
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   177
}
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   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
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   180
 * This is supposed to unlock debugger thread in WaitDebugData*/
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   181
void InitiateDebugTransfer()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   182
{
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   183
    /* remember tick */
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   184
    __debug_tick = __tick;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   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
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   187
}
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   188
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   189
void suspendDebug()
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   190
{
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 280
diff changeset
   191
	__DEBUG = 0;
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   192
    /* Prevent PLC to enter debug code */
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   193
	WaitForSingleObject(debug_sem, INFINITE);  
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   194
}
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   195
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   196
void resumeDebug()
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   197
{
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 280
diff changeset
   198
	__DEBUG = 1;
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   199
    /* Let PLC enter debug code */
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   200
	ReleaseSemaphore(debug_sem, 1, NULL);
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   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