Serial Communication Event Handler ( Interrupt) in VC++

M

Thread Starter

Mustafa Husain

I want to use serial port communication. Programming in VC++. Please help me out, how I can use it efficently through interrupts. I have succesfully completed the project with timer interrupt. I have used 5 wires i.e rts, tx, rx, ground, shield. If you have some information regarding the same please deliver.
 
In general when running under Windows and getting data from a serial port you don't need the hardware interrupts. I've never tried attaching to them, but my guess is that requires writing a device driver which isn't what any of us want to do.

Windows provides a driver for the serial port.

In general there are a couple different useful models that can be used to read data from the serial port. You'll need to search the MS docs a little to find the actual APIs

1. Overlapped I/O

The basic idea of overlapped I/O is that you provide a callback routine to be called when data is read from the pipe. Essentially when you call CreateFile to open the serial port you pass in FILE_FLAG_OVERLAPPED. Then your read command will return immediately and later on your routine gets called on I/O completion. Whenever windows is in a "Alertable Wait" it can invoke your callback routine. For example Sleep() is not an alertable wait but SleepEx() is. I believe the Windows Message Pump is an alertable wait.

Overlapped I/O works OK for most simple applications, the disadvantage is that you are in less control of what is going on with the I/O. You need to worry about cancelling the asynchronous reads, etc.

2. Use a thread

The thread model is conceptually the simplest. You create a thread that does nothing but read on the file. When data comes in, it sticks it into a queue (or package it up in Windows Message and send it to the main thread via ::postMessage) and then goes back to reading. The processing thread then retrieves the new data and does something with it.

In this model you want to make sure that only one thread ever reads from the port at a time and only one ever writes to it as a time. Typically with serial communication you are performing a master/slave communication (e.g. poll / response.) In this case you want to make sure the atomic operation of Write / Read occurs.

Conversely once you start using threads you'll need critical sections and events. While conceptually it is simple - threads can cause deadlocks, memory corruption (if your not protecting your shared data with a critical section) that can be difficult to detect since they are very timing dependent. A good plan is important - since typically the only way to verify "correctness" in a multithreaded program is by inspection.

Good luck.
 
Second in the list with similar query!

Well, Mustafa, there has to be a Serial Communications Protocol. Either you can use a DDE server for that particular protocol and access data in a very simple way or you can write the handshaking and exchange of information in
your program.

Please find out the protocol used.
Anand
 
Top