473,657 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

hardware interrupt in c#

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.Interrup t 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
2 7062
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*********@gm ail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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.Interrup t 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

<bv*********@gm ail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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.Interrup t 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.Threadin g 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
ManualResetEven t.
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
6469
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException. "somethread.interrupt()" will wake somethread up when it's in sleeping/waiting state. Is there any way of doing this with python's thread? I suppose thread interrupt is a very primitive functionality for stopping a blocked thread.
2
2446
by: Marco | last post by:
Hallo, I have made a microcontroller with keyboard (16 key). I have a function to handle interrupt that comes from keyboard when a key is pressed. I should make a C software (not graphic) with a menu. I thought to make into main.c a cicle like this: while (1)
0
1757
by: LordHog | last post by:
Hello all, I have a little application that will be used for test environment that utilizes the CAN Messaging. The application will capture and then decode the CAN message that are received. In the API reference material the device will generate an interrupt which the user much provide an interrupt service routine with the entry pointed at the interrupt 0x16 vector ( Address = 0x0083 ). Is it possible to write an interrupt service...
27
2814
by: tomhr | last post by:
I have a Borland Turbo C++ compiler (though I never use the "plus-plus" part), copyright 1990. I use this compiler to write simple programs, one of which I am quite dependent on. A month ago, my parallel-port-output printer died, and I got a Samsung printer that takes info from the USB port. Every time I go into DOS mode and try to send something to that printer, whether from a command line ("dir>prn"), or from one of my programs, I get...
2
4087
by: Hartmut | last post by:
Hi guys out there..., I am not shure if this is the right forum for my Question. Have a problem using my own Keyboard interrupt handler. The handler works fine as long as i do not press keys like pause, arrow keys etc. My system returns with a stack overflow. Is there something i have overseen in my keyboard Interrupt Service
2
3268
by: =?Utf-8?B?QnJ1Y2UgSFM=?= | last post by:
I'm using VS2005 Winforms. I want a method to allow the user to be able to interrupt an SQL query if he decides he doesn't want to wait for it any longer. I've used IAsyncResult to open an async connection that runs an ExecuteNonQuery, but this method doesn't allow interruption (EndExecuteNonQuery gets blocked until the execute finishes), and ideally I'd like to interrupt a Query rather than a NonQuery. It seems to me this is a...
3
1801
by: mani | last post by:
Hi all. Is their any function which can associate the irqno with interrupt handller. and function should not be the kernal function. and more interrupt related function like enable irq and disable irq.... (example in Vxworks IntConnect is for that)
3
8257
by: simon.tran.ca | last post by:
Good Morning folks, Our DB2 servers currently complains about the below error, "User interrupt detected". I've been researching for quite long and I haven't got any good explanation nor solution on this. Could someone give me a hint please? Thanks in advance
6
5729
by: BlueBird | last post by:
Hi, I have a program with a master thread and several slave threads. Whenever an exception occurs, in the master thread or in one of the slave threads, I would like to interrupt all the threads and the main program. Threading API does not seem to provide a way to stop a thread, is there anyway to achieve that ? The closest thing I found is thread.interrupt_main() but it's far from
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.