Free Web Hosting | free host | Free Web Space | BlueHost Review

WST 23 - Serial Communication
Home Up Interests Downloads (New) My Resume Links Feedback

 

Interests
Downloads (New)
My Resume
Links
WST
Feedback

Hi all,

Since we are fresh from a seminar by Anupam* on Serial communication, I thought it proper to take a look at serial communication's programming perspective.

Now, Anupam has mentioned about some functions such as inportb() etc, which can be used to communicate with serial ports (any ports infact if you know it's address). However, the problem with these functions is that they don't work on NT based platforms due to NT's enhanced security subsystem. Windows NT based platforms (NT, 2K, XP, 2k3 etc) just don't allow you to communicate directly with any ports (including serial) with their addresses, hence these functions don't work on these platforms. However, they work just perfect on DOS based platforms such as Windows 9x/ME etc.

So, Windows provides a set of communication functions for serial port communication on Windows NT based platforms (infact they work on any platforms). These are called Communication Functions. There is a full chapter on Communications Resources in MSDN which you can refer for details on these.

Let's see what these functions can do for us and how to use them. The first thing you need to do is to open a communication resource (a serial port say). This is done by the multipurpose function CreateFile(). You can look up the details of this function for opening serial port, in MSDN. CreateFile returns the handle of the comm port, which is required as a parameter in any other comm function call.

Now, you can setup the com port using functions such as GetCommConfig(), CommConfigDialog(), SetCommConfig(), GetCommState() and SetCommState(). These function allow you to setup the COM port parameters such as Baud Rate, Parity, Software/Hardware flow control parameters etc.

Next you can write or read from the COM port using WriteFile()/ReadFile() functions. You must be fairly conversant with these functions now.

You can wait/check for specific COM (Serial) port events such as CTS, DSR etc by using functions such as SetCommMask() and WaitCommEvent(). In a typical serial port communication, we usually either SetCommMask() or WaitCommEvent() to wait for serial events (such as receipt of data etc.) and then we take the action (such as reading the data).

You can of course close the comm port handle using CloseHandle() once you are done.

There are some more useful communication functions which you can lookup in MSDN.

Exercises:

1. Study communication functions in MSDN.

2. Write a simple hyperterminal sort of utility (look at windows hyperterminal utility), which can send and receive data from serial ports.

This should be sufficient for the week. Thanks for your time !

-Farooque

* One of my team members.

 

 

HyperCounter
Bpath Counter