By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,221 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,221 IT Pros & Developers. It's quick & easy.

hardware interrupt in c#

P: n/a
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:

Expand|Select|Wrap|Line Numbers
  1. int InitInterruptHandling32(void)
  2. {
  3. int ret;
  4. if (NULL == (threadData.events[INT_EVENT] = CreateEvent(NULL, FALSE,
  5. FALSE, NULL)))
  6. {
  7. printf("-->Error creating interrupt event 0x%X\n", GetLastError());
  8. return(-1);
  9. }
  10.  
  11. ResetEvent(threadData.events[INT_EVENT]);
  12.  
  13. if (NULL == (threadData.events[END_EVENT] = CreateEvent(NULL, FALSE,
  14. FALSE, NULL)))
  15. {
  16. printf("-->Error creating thread end event 0x%X\n",
  17. GetLastError());
  18. CloseHandle(threadData.events[INT_EVENT]);
  19. return(-1);
  20. }
  21.  
  22. if (CANPC_OK != (ret =
  23. CANPC_set_interrupt_event(threadData.events[INT_EVENT])))
  24. {
  25. printf("-->Error set interrupt event 0x%X\n", ret);
  26. CloseHandle(threadData.events[INT_EVENT]);
  27. CloseHandle(threadData.events[END_EVENT]);
  28. return(-1);
  29. }
  30.  
  31. if (NULL == (intThread = (HANDLE) _beginthreadex(NULL, 0,
  32. interruptThread,
  33. (void *)
  34. &threadData, 0, &tid)))
  35. {
  36. printf("-->Error create interrupt thread 0x%X\n", GetLastError());
  37. INIPC_close_board();
  38. CloseHandle(threadData.events[INT_EVENT]);
  39. CloseHandle(threadData.events[END_EVENT]);
  40. return(-1);
  41. }
  42.  
  43. InitializeCriticalSection(&CriticalSectionForInterrupt);
  44.  
  45. return(0);
  46. }
  47.  
And in main there is this code after the previous function:
Expand|Select|Wrap|Line Numbers
  1. do  // Inner loop
  2. {
  3.  
  4. // Loop till user request
  5. while (!kbhit())
  6. {
  7. if (!bInterruptFlag)  ReadBusEvent(); //Polling mode chosen -->
  8. Poll CANPC_read_ac
  9. }// end while
  10.  
  11. custom=getch();
  12.  
  13. // User request
  14. #ifdef WIN32
  15. if (bInterruptFlag)
  16. EnterCriticalSection(&CriticalSectionForInterrupt);
  17. #else
  18. if (bInterruptFlag) disable_interrupt();
  19. #endif
  20. switch (op_mode)
  21. {
  22. case 'd':
  23. ret=UserRequestDynObjBuf(custom);
  24. break;
  25.  
  26. case 's':
  27. ret=UserRequestStatObjBuf(custom);
  28. break;
  29.  
  30. default:
  31. ret=UserRequestFIFO(custom);
  32. break;
  33. } // End switch
  34.  
  35. #ifdef WIN32
  36. if (bInterruptFlag)
  37. LeaveCriticalSection(&CriticalSectionForInterrupt);
  38. #else
  39. if (bInterruptFlag) enable_interrupt();
  40. #endif
  41.  
  42.  
  43. if (ret < 0)
  44. {
  45. printf("-->User request failed \n");
  46. INIPC_close_board();
  47. return(-1);
  48. }
  49. }
  50. while ((custom) != 'q' && custom != 'i'); // End inner loop
  51. CANPC_reinitialize();
  52. #ifdef WIN32
  53. if (bInterruptFlag) ExitInterruptHandling32();
  54. #else
  55. if (bInterruptFlag) ExitInterruptHandling16();
  56. #endif
  57. }
  58. while (custom == 'i');  // End outer loop
  59. INIPC_close_board();
  60. return(0);
  61. }
  62.  
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

Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

<bv*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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:

Expand|Select|Wrap|Line Numbers
  1.  int InitInterruptHandling32(void)
  2.  {
  3.   int ret;
  4.   if (NULL == (threadData.events[INT_EVENT] = CreateEvent(NULL, FALSE,
  5.  FALSE, NULL)))
  6.   {
  7.     printf("-->Error creating interrupt event 0x%X\n", GetLastError());
  8.     return(-1);
  9.   }
  10.   ResetEvent(threadData.events[INT_EVENT]);
  11.   if (NULL == (threadData.events[END_EVENT] = CreateEvent(NULL, FALSE,
  12.  FALSE, NULL)))
  13.   {
  14.     printf("-->Error creating thread end event 0x%X\n",
  15.  GetLastError());
  16.     CloseHandle(threadData.events[INT_EVENT]);
  17.     return(-1);
  18.   }
  19.   if (CANPC_OK != (ret =
  20.  CANPC_set_interrupt_event(threadData.events[INT_EVENT])))
  21.   {
  22.     printf("-->Error set interrupt event 0x%X\n", ret);
  23.     CloseHandle(threadData.events[INT_EVENT]);
  24.     CloseHandle(threadData.events[END_EVENT]);
  25.     return(-1);
  26.   }
  27.   if (NULL == (intThread = (HANDLE) _beginthreadex(NULL, 0,
  28.  interruptThread,
  29.                                                    (void *)
  30.  &threadData, 0, &tid)))
  31.   {
  32.     printf("-->Error create interrupt thread 0x%X\n", GetLastError());
  33.     INIPC_close_board();
  34.     CloseHandle(threadData.events[INT_EVENT]);
  35.     CloseHandle(threadData.events[END_EVENT]);
  36.     return(-1);
  37.   }
  38.   InitializeCriticalSection(&CriticalSectionForInterrupt);
  39.   return(0);
  40.  }
  41.  

And in main there is this code after the previous function:
Expand|Select|Wrap|Line Numbers
  1.   do  // Inner loop
  2.     {
  3.     // Loop till user request
  4.     while (!kbhit())
  5.     {
  6.       if (!bInterruptFlag)  ReadBusEvent(); //Polling mode chosen -->
  7.  Poll CANPC_read_ac
  8.     }// end while
  9.         custom=getch();
  10.         // User request
  11.  #ifdef WIN32
  12.     if (bInterruptFlag)
  13.  EnterCriticalSection(&CriticalSectionForInterrupt);
  14.  #else
  15.     if (bInterruptFlag) disable_interrupt();
  16.  #endif
  17.     switch (op_mode)
  18.       {
  19.       case 'd':
  20.           ret=UserRequestDynObjBuf(custom);
  21.         break;
  22.       case 's':
  23.         ret=UserRequestStatObjBuf(custom);
  24.         break;
  25.       default:
  26.           ret=UserRequestFIFO(custom);
  27.         break;
  28.       } // End switch
  29.  #ifdef WIN32
  30.     if (bInterruptFlag)
  31.  LeaveCriticalSection(&CriticalSectionForInterrupt);
  32.  #else
  33.     if (bInterruptFlag) enable_interrupt();
  34.  #endif
  35.     if (ret < 0)
  36.          {
  37.               printf("-->User request failed \n");
  38.           INIPC_close_board();
  39.               return(-1);
  40.             }
  41.     }
  42.   while ((custom) != 'q' && custom != 'i'); // End inner loop
  43.    CANPC_reinitialize();
  44.  #ifdef WIN32
  45.     if (bInterruptFlag) ExitInterruptHandling32();
  46.  #else
  47.     if (bInterruptFlag) ExitInterruptHandling16();
  48.  #endif
  49.   }
  50.  while (custom == 'i');  // End outer loop
  51.  INIPC_close_board();
  52.  return(0);
  53.  }
  54.  

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

Nov 17 '05 #2

P: n/a

<bv*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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:

Expand|Select|Wrap|Line Numbers
  1.  int InitInterruptHandling32(void)
  2.  {
  3.   int ret;
  4.   if (NULL == (threadData.events[INT_EVENT] = CreateEvent(NULL, FALSE,
  5.  FALSE, NULL)))
  6.   {
  7.     printf("-->Error creating interrupt event 0x%X\n", GetLastError());
  8.     return(-1);
  9.   }
  10.   ResetEvent(threadData.events[INT_EVENT]);
  11.   if (NULL == (threadData.events[END_EVENT] = CreateEvent(NULL, FALSE,
  12.  FALSE, NULL)))
  13.   {
  14.     printf("-->Error creating thread end event 0x%X\n",
  15.  GetLastError());
  16.     CloseHandle(threadData.events[INT_EVENT]);
  17.     return(-1);
  18.   }
  19.   if (CANPC_OK != (ret =
  20.  CANPC_set_interrupt_event(threadData.events[INT_EVENT])))
  21.   {
  22.     printf("-->Error set interrupt event 0x%X\n", ret);
  23.     CloseHandle(threadData.events[INT_EVENT]);
  24.     CloseHandle(threadData.events[END_EVENT]);
  25.     return(-1);
  26.   }
  27.   if (NULL == (intThread = (HANDLE) _beginthreadex(NULL, 0,
  28.  interruptThread,
  29.                                                    (void *)
  30.  &threadData, 0, &tid)))
  31.   {
  32.     printf("-->Error create interrupt thread 0x%X\n", GetLastError());
  33.     INIPC_close_board();
  34.     CloseHandle(threadData.events[INT_EVENT]);
  35.     CloseHandle(threadData.events[END_EVENT]);
  36.     return(-1);
  37.   }
  38.   InitializeCriticalSection(&CriticalSectionForInterrupt);
  39.   return(0);
  40.  }
  41.  

And in main there is this code after the previous function:
Expand|Select|Wrap|Line Numbers
  1.   do  // Inner loop
  2.     {
  3.     // Loop till user request
  4.     while (!kbhit())
  5.     {
  6.       if (!bInterruptFlag)  ReadBusEvent(); //Polling mode chosen -->
  7.  Poll CANPC_read_ac
  8.     }// end while
  9.         custom=getch();
  10.         // User request
  11.  #ifdef WIN32
  12.     if (bInterruptFlag)
  13.  EnterCriticalSection(&CriticalSectionForInterrupt);
  14.  #else
  15.     if (bInterruptFlag) disable_interrupt();
  16.  #endif
  17.     switch (op_mode)
  18.       {
  19.       case 'd':
  20.           ret=UserRequestDynObjBuf(custom);
  21.         break;
  22.       case 's':
  23.         ret=UserRequestStatObjBuf(custom);
  24.         break;
  25.       default:
  26.           ret=UserRequestFIFO(custom);
  27.         break;
  28.       } // End switch
  29.  #ifdef WIN32
  30.     if (bInterruptFlag)
  31.  LeaveCriticalSection(&CriticalSectionForInterrupt);
  32.  #else
  33.     if (bInterruptFlag) enable_interrupt();
  34.  #endif
  35.     if (ret < 0)
  36.          {
  37.               printf("-->User request failed \n");
  38.           INIPC_close_board();
  39.               return(-1);
  40.             }
  41.     }
  42.   while ((custom) != 'q' && custom != 'i'); // End inner loop
  43.    CANPC_reinitialize();
  44.  #ifdef WIN32
  45.     if (bInterruptFlag) ExitInterruptHandling32();
  46.  #else
  47.     if (bInterruptFlag) ExitInterruptHandling16();
  48.  #endif
  49.   }
  50.  while (custom == 'i');  // End outer loop
  51.  INIPC_close_board();
  52.  return(0);
  53.  }
  54.  

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.

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.