Introduction
Serial Data
transmission seems a bit difficult for
those who are new to the world of
serial
communication and
Visual C++.
Long ago, I had searched on ucancode.net
for some help on
serial data transmission
and I got some valuable information. It
was my dream to develop a simple class
for implementing
serial data transmission
since then.
After
getting seven months practical
experience in the field of
serial
communication, I have
developed a simple class for
implementing serial transmission using
WinAPI functions.
Serial Data
Tranmission Basics
In
serial data
transmission the data is
transmitted in serial format with the
LSB of the byte to be transmitted,
shifted out first among the data bits.
The general format for serial
transmission is Start Bit + Data
Bits + Parity Bit (Optional) + Stop Bit.
The
Parity bit is optional. It is used for
error checking in communication. You can
enable or disable parity checking by
software modifications. Also, you can
specify which parity you would like to
use, either 'EVEN' or 'ODD' through
software.
The
various steps to be performed for
sending and receiving data through the
serial port of a PC are listed below:-
-
Open
the
communication
port
-
Configure
the
communication port by
setting the Baud rate, parity, no.
of data bits, etc.
-
Set time-outs for
communication.
-
Write data
to the port.
-
Read data from the port.
-
Close the port.
Opening The
Serial Port
The
CreateFile()
function opens
a
communications port. There
are two ways to call
CreateFile()
to open the port -
OVERLAPPED
and
NON-OVERLAPPED
.
You can open a Communication Port for
OVERLAPPED
IO operation and
NON-OVERLAPPED
IO operation. The
CSerialCom
class is written for
NON-OVERLAPPED
IO operation. For more details on
OVERLAPPED
&
NON-OVERLAPPED
IO, please refer to the MSDN
documentation.
Configuring Serial Ports
The
most critical phase in serial
communication programming is configuring
the port settings with the
DCB
structure. Erroneously initializing the
DCB
structure is a common problem. When a
serial communications function does not
produce the expected results, the
DCB
structure may be in error. A call to the
CreateFile()
function opens a serial port with
default port settings. Usually, the
application needs to change the
defaults. You must set the Baud rate for
communication, Parity functions, no. of
Stop Bits, etc. in accordance with the
requirements of the external device by
calling appropriate WinAPI functions.
Configuring Time-Outs
An
application must always set
communication time-outs using the
COMMTIMEOUTS
structure each time it opens a
communication port. If this structure is
not configured, the port uses default
time-outs supplied by the driver, or
time-outs from a previous communication
application. By assuming specific
time-out settings when the settings are
actually different, an application can
have read/write operations that never
complete or complete too often. You must
configure the read & write time-outs by
calling the appropriate WinAPI
functions.
Writing to
a Serial Port
The
WriteFile()
function transfers data through the
serial connection to another device.
Before calling this function, an
application must open and configure a
serial port.
Reading
from a Serial Port
An
application calls the
ReadFile()
function to receive data from a device
at the other end of a serial connection.
Closing a
Serial Port
You
must close the communications port after
serial transmission in order to make
this port available for other
applications which use this resource. As
long as you are working with a port
(i.e. the port is in an open state),
other threads or applications will not
be able to access to this port till you
close the handle to that port in
NON-OVERLAPPED
IO operation. Call the
CloseHandle()
function to close the serial port.
CloseHandle()
has one parameter, which is the handle
returned by the
CreateFile()
call that opened the port.