hardware interrupt in c#

  • Thread starter Thread starter bvermeersch
  • Start date Start date
B

bvermeersch

Hi,

Here I am again, just learning C# and always wanting to learn the hard
things first. As you might already know from other posts I'm trying to
convert c++ code to C#.

The problem I'm running into now is the creation and handling of an
interrupt. In c++ it is done this way:

Code:
int InitInterruptHandling32(void)
{
int ret;
if (NULL == (threadData.events[INT_EVENT] = CreateEvent(NULL, FALSE,
FALSE, NULL)))
{
printf("-->Error creating interrupt event 0x%X\n", GetLastError());
return(-1);
}

ResetEvent(threadData.events[INT_EVENT]);

if (NULL == (threadData.events[END_EVENT] = CreateEvent(NULL, FALSE,
FALSE, NULL)))
{
printf("-->Error creating thread end event 0x%X\n",
GetLastError());
CloseHandle(threadData.events[INT_EVENT]);
return(-1);
}

if (CANPC_OK != (ret =
CANPC_set_interrupt_event(threadData.events[INT_EVENT])))
{
printf("-->Error set interrupt event 0x%X\n", ret);
CloseHandle(threadData.events[INT_EVENT]);
CloseHandle(threadData.events[END_EVENT]);
return(-1);
}

if (NULL == (intThread = (HANDLE) _beginthreadex(NULL, 0,
interruptThread,
(void *)
&threadData, 0, &tid)))
{
printf("-->Error create interrupt thread 0x%X\n", GetLastError());
INIPC_close_board();
CloseHandle(threadData.events[INT_EVENT]);
CloseHandle(threadData.events[END_EVENT]);
return(-1);
}

InitializeCriticalSection(&CriticalSectionForInterrupt);

return(0);
}

And in main there is this code after the previous function:
Code:
do  // Inner loop
{

// Loop till user request
while (!kbhit())
{
if (!bInterruptFlag)  ReadBusEvent(); //Polling mode chosen -->
Poll CANPC_read_ac
}// end while

custom=getch();

// User request
#ifdef WIN32
if (bInterruptFlag)
EnterCriticalSection(&CriticalSectionForInterrupt);
#else
if (bInterruptFlag) disable_interrupt();
#endif
switch (op_mode)
{
case 'd':
ret=UserRequestDynObjBuf(custom);
break;

case 's':
ret=UserRequestStatObjBuf(custom);
break;

default:
ret=UserRequestFIFO(custom);
break;
} // End switch

#ifdef WIN32
if (bInterruptFlag)
LeaveCriticalSection(&CriticalSectionForInterrupt);
#else
if (bInterruptFlag) enable_interrupt();
#endif


if (ret < 0)
{
printf("-->User request failed \n");
INIPC_close_board();
return(-1);
}
}
while ((custom) != 'q' && custom != 'i'); // End inner loop
CANPC_reinitialize();
#ifdef WIN32
if (bInterruptFlag) ExitInterruptHandling32();
#else
if (bInterruptFlag) ExitInterruptHandling16();
#endif
}
while (custom == 'i');  // End outer loop
INIPC_close_board();
return(0);
}

How do I best convert this? Enter critical section, leave it, ...
I have the feeling it is easier in C#, but then again, who am I
I found some stuff regarding Thread.Interrupt Method but don't fully
understand how to implement with an interrupt that comes from hardware

Waiting for help,

kind regards, Jef
 
You should be able to use an unsafe code block for this.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition

Hi,

Here I am again, just learning C# and always wanting to learn the hard
things first. As you might already know from other posts I'm trying to
convert c++ code to C#.

The problem I'm running into now is the creation and handling of an
interrupt. In c++ it is done this way:

Code:
int InitInterruptHandling32(void)
{
int ret;
if (NULL == (threadData.events[INT_EVENT] = CreateEvent(NULL, FALSE,
FALSE, NULL)))
{
printf("-->Error creating interrupt event 0x%X\n", GetLastError());
return(-1);
}

ResetEvent(threadData.events[INT_EVENT]);

if (NULL == (threadData.events[END_EVENT] = CreateEvent(NULL, FALSE,
FALSE, NULL)))
{
printf("-->Error creating thread end event 0x%X\n",
GetLastError());
CloseHandle(threadData.events[INT_EVENT]);
return(-1);
}

if (CANPC_OK != (ret =
CANPC_set_interrupt_event(threadData.events[INT_EVENT])))
{
printf("-->Error set interrupt event 0x%X\n", ret);
CloseHandle(threadData.events[INT_EVENT]);
CloseHandle(threadData.events[END_EVENT]);
return(-1);
}

if (NULL == (intThread = (HANDLE) _beginthreadex(NULL, 0,
interruptThread,
(void *)
&threadData, 0, &tid)))
{
printf("-->Error create interrupt thread 0x%X\n", GetLastError());
INIPC_close_board();
CloseHandle(threadData.events[INT_EVENT]);
CloseHandle(threadData.events[END_EVENT]);
return(-1);
}

InitializeCriticalSection(&CriticalSectionForInterrupt);

return(0);
}

And in main there is this code after the previous function:
Code:
do  // Inner loop
{

// Loop till user request
while (!kbhit())
{
if (!bInterruptFlag)  ReadBusEvent(); //Polling mode chosen -->
Poll CANPC_read_ac
}// end while

custom=getch();

// User request
#ifdef WIN32
if (bInterruptFlag)
EnterCriticalSection(&CriticalSectionForInterrupt);
#else
if (bInterruptFlag) disable_interrupt();
#endif
switch (op_mode)
{
case 'd':
ret=UserRequestDynObjBuf(custom);
break;

case 's':
ret=UserRequestStatObjBuf(custom);
break;

default:
ret=UserRequestFIFO(custom);
break;
} // End switch

#ifdef WIN32
if (bInterruptFlag)
LeaveCriticalSection(&CriticalSectionForInterrupt);
#else
if (bInterruptFlag) enable_interrupt();
#endif


if (ret < 0)
{
printf("-->User request failed \n");
INIPC_close_board();
return(-1);
}
}
while ((custom) != 'q' && custom != 'i'); // End inner loop
CANPC_reinitialize();
#ifdef WIN32
if (bInterruptFlag) ExitInterruptHandling32();
#else
if (bInterruptFlag) ExitInterruptHandling16();
#endif
}
while (custom == 'i');  // End outer loop
INIPC_close_board();
return(0);
}

How do I best convert this? Enter critical section, leave it, ...
I have the feeling it is easier in C#, but then again, who am I
I found some stuff regarding Thread.Interrupt Method but don't fully
understand how to implement with an interrupt that comes from hardware

Waiting for help,

kind regards, Jef
 
Hi,

Here I am again, just learning C# and always wanting to learn the hard
things first. As you might already know from other posts I'm trying to
convert c++ code to C#.

The problem I'm running into now is the creation and handling of an
interrupt. In c++ it is done this way:

Code:
int InitInterruptHandling32(void)
{
int ret;
if (NULL == (threadData.events[INT_EVENT] = CreateEvent(NULL, FALSE,
FALSE, NULL)))
{
printf("-->Error creating interrupt event 0x%X\n", GetLastError());
return(-1);
}

ResetEvent(threadData.events[INT_EVENT]);

if (NULL == (threadData.events[END_EVENT] = CreateEvent(NULL, FALSE,
FALSE, NULL)))
{
printf("-->Error creating thread end event 0x%X\n",
GetLastError());
CloseHandle(threadData.events[INT_EVENT]);
return(-1);
}

if (CANPC_OK != (ret =
CANPC_set_interrupt_event(threadData.events[INT_EVENT])))
{
printf("-->Error set interrupt event 0x%X\n", ret);
CloseHandle(threadData.events[INT_EVENT]);
CloseHandle(threadData.events[END_EVENT]);
return(-1);
}

if (NULL == (intThread = (HANDLE) _beginthreadex(NULL, 0,
interruptThread,
(void *)
&threadData, 0, &tid)))
{
printf("-->Error create interrupt thread 0x%X\n", GetLastError());
INIPC_close_board();
CloseHandle(threadData.events[INT_EVENT]);
CloseHandle(threadData.events[END_EVENT]);
return(-1);
}

InitializeCriticalSection(&CriticalSectionForInterrupt);

return(0);
}

And in main there is this code after the previous function:
Code:
do  // Inner loop
{

// Loop till user request
while (!kbhit())
{
if (!bInterruptFlag)  ReadBusEvent(); //Polling mode chosen -->
Poll CANPC_read_ac
}// end while

custom=getch();

// User request
#ifdef WIN32
if (bInterruptFlag)
EnterCriticalSection(&CriticalSectionForInterrupt);
#else
if (bInterruptFlag) disable_interrupt();
#endif
switch (op_mode)
{
case 'd':
ret=UserRequestDynObjBuf(custom);
break;

case 's':
ret=UserRequestStatObjBuf(custom);
break;

default:
ret=UserRequestFIFO(custom);
break;
} // End switch

#ifdef WIN32
if (bInterruptFlag)
LeaveCriticalSection(&CriticalSectionForInterrupt);
#else
if (bInterruptFlag) enable_interrupt();
#endif


if (ret < 0)
{
printf("-->User request failed \n");
INIPC_close_board();
return(-1);
}
}
while ((custom) != 'q' && custom != 'i'); // End inner loop
CANPC_reinitialize();
#ifdef WIN32
if (bInterruptFlag) ExitInterruptHandling32();
#else
if (bInterruptFlag) ExitInterruptHandling16();
#endif
}
while (custom == 'i');  // End outer loop
INIPC_close_board();
return(0);
}

How do I best convert this? Enter critical section, leave it, ...
I have the feeling it is easier in C#, but then again, who am I
I found some stuff regarding Thread.Interrupt Method but don't fully
understand how to implement with an interrupt that comes from hardware

Waiting for help,

kind regards, Jef

Take a look at the System.Threading namespace, this include all the stuff
you need to convert this to C#.
For instance Critical sections can be implemented using Monitor.Enter /
Monitor.exit, CreateEvent see WaitHandle and it's derived classes like
ManualResetEvent.
getch(); equivalent is available in v2.0 of the System.Console class, when
running v1.x you'll have to PInvoke some conio functions (like __getch).

Some questions remain however:
- what are you doing in : ReadBusEvent() ??
- why do you want to convert this to C#?

Willy.
 
Back
Top