A Communication Class For Serial Port

Published on: October 10, 2000
Last Updated: October 10, 2000

A Communication Class For Serial Port

Published on: October 10, 2000
Last Updated: October 10, 2000

I’ve searched for a good communication class for a while and could not find one. That’s when I decided to write my own and it should be one that’s easy to use.

In the newgroups there are many questions about serial communication so I thought: make it public! It’s freeware. The only thing I expect from users is that they drop me a mail. All modifications on this class are free, but please let me know if it solves a bug or adds some good features. Also comment your code and don’t let me solve your bugs!


The class is not intended to use as a baseclass for modemcommunication but more for driving hardware or reading hardware via the serial port.

From the classes included there is only one class important: CSerialPort. The other classes are only there to illustrate the use of this class.


In your software you only need to create an instance of the CSerialPort class and call InitPort.

// the owner (CWnd) of the port (receives message)

BOOL CSerialPort::InitPort(CWnd* pPortOwner,  
 UINT  portnr,   // portnumber (1..4)
 UINT  baud,    // baudrate
 char  parity,   // parity 
 UINT  databits,   // databits 
 UINT  stopbits,   // stopbits 
 DWORD dwCommEvents,  // EV_RXCHAR, EV_CTS etc
 UINT  writebuffersize) // size of the writebuffer

The dwCommEvents flag can be used for communication with the owner of this class.

The flags can be one of the following (or combined with |):

  • WM_COMM_BREAK_DETECTED A break was detected on input.
  • WM_COMM_CTS_DETECTED The CTS (clear-to-send) signal changed state.
  • WM_COMM_DSR_DETECTED The DSR (data-set-ready) signal changed state.
  • WM_COMM_ERR_DETECTED A line-status error occurred. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY.
  • WM_COMM_RING_DETECTED A ring indicator was detected.
  • WM_COMM_RLSD_DETECTED The RLSD (receive-line-signal-detect) signal changed state.
  • WM_COMM_RXCHAR A character was received and placed in the input buffer.
  • WM_COMM_RXFLAG_DETECTED The event character was received and placed in the input buffer.

Accept the first parameter all parameters are optional. The standard values are:

portnr  = 1
baud  = 19200
parity  = 'N'
databits  = 8, 
stopsbits = 1, 
dwCommEvents = EV_RXCHAR | EV_CTS,
nBufferSize = 512);

So the follwing code is enough to make communication possible:

in the header of the owner:

 CSerialPort m_Serial;

in the code:


Then the tread that watches the port is started and all events on the port are send to the owner. The receive a character the owner needs a messageentry in the messagemap:

BEGIN_MESSAGE_MAP(CCommtestDlg, CDialog)

and they must be handled:

LONG CCommtestDlg::OnCommunication(WPARAM ch, LPARAM port)
 // do something with the received character

 return 0;

This is it for reading. Writing can be done with WriteChar or WriteToPort


Download sample project – 66K


Date Posted: February 27, 1998


Fix for resourse leak in Init() – Wes Jones (2000/10/17)

Data Getting Lost on WriteToPort – Shree (2000/09/12)

program fails if port number > 9 – Shree (2000/09/08)

Connecting two computers – Mani (2000/08/23)

ERROR_IO_PENDING error in WriteFile – Deepu (2000/08/07)

converting the sent string in to ascii – Luke (2000/07/28)

Can i free the com port? – Rob Edwards (2000/07/26)

Commtest not working in Win2000? – matt (2000/05/26)

CreateFile in Win2000 fails – matt (2000/05/25)

XMODEM-1k over serial ports – jermaine (2000/05/24)

linux compatibility? – Callaway (2000/05/18)

Modified CSerial with modem functionnalities?? – Francois (2000/05/18)

Last Version of CSerial ?? – Francois G (2000/05/18)

Error on GetOverlappedResult in WriteFile – Stéphane Lemoine (2000/05/11)

Accessing Logical Ports??? – Shree (2000/04/26)

VERY GOOD WORK!!!! – Nico (2000/04/14)

Send a big file ? One possible answer. – Bill Bateman (2000/04/12)

Send a big file ? – Gary Hu (2000/04/12)

Resource leaks – Ernesto García (2000/04/04)

CSerialPort Class – Priya Nair (2000/03/30)

Chainard Arnaud, you there? – BigDaddy (2000/03/25)

a GUI which go to read the serial port – dido padi (2000/03/23)

How to make… – raj (2000/03/22)

Much Needed Info – mystiqman (2000/03/04)

Bug : Kill Serial port ? – Lee (2000/03/03)

Problem; i don’t know how to send and read data to and from serialport – ajie (2000/02/28)

How to play with the demo – Jiasheng Zhu (2000/02/17)

Serial port terminal emulation? IrDA? – jermaine (2000/02/15)

X/Y/ZMODEM protocols? – jermaine (2000/02/11)

Does this class send NULL CHARACTER???? – X HANIN (2000/02/06)

Assertion failed while WritingToPort – ajie (2000/02/01)

Failed during stream of input data, !) new 2) receive 3) delete 4) new 5) receive – Ali Khan (1999/12/14)

Help…a ameteur here…how to compile program?? – Darren Pang (1999/12/05)

Please help Me (CHANGE THE BAUDRATE)!! – Mustafa (1999/12/02)

Problem in Sending contorl signal through port – Srvidya (1999/11/24)

Added WaitForString(…) – Olivier David (1999/11/22)

Demo doesn’t work with multi-port(2ports) environment – Joo Min, Lee (1999/11/16)

Other solution for lost data reading – Ainurrofiq. Lento (1999/11/04)

Baud Rate – Crystal (1999/11/02)

Excellent, I have based a lot on this class – Robert Pearmain (1999/10/28)

Accessing Parallel Ports. – Cameron (1999/10/19)

who wants to send not-null-terminated-string – Taesung Lee (1999/09/24)

Perfect code. – ajit wadekar. (1999/08/07)

How to compile with Borland c++ – R. Vogts (1999/08/05)

Data transfer from scanner to Pc and back – Narayanan (1999/07/30)

Destructor does not return ? – Daniel Zuppinger (1999/07/24)

Good idea – Pumb (1999/07/12)

PC to HPUNIX RS232 communication – L.Gnaneshwar (1999/07/06)

Thanks!! – jj (1999/06/30)

No Parity and Framing Errors – Hans-Peter von Boetticher (1999/06/15)

How modify when the class send a message WM_COMM_RXCHAR ? – Philippe Rambourg (1999/06/10)

Use the Microsoft’s Serial Communications OLE control – Michael Urvan (1999/06/01)

Can´t get it to work in a OCX !?! – Bjarki (1999/05/19)

Can’t build the sample successfully.Would you help me? – larry (1999/05/13)

How do I send a break signal with this class – ramrocket (1999/05/11)

What the hell is going on with this code? – Timothy O’Connor (1999/05/04)

Problems Creating an Instance of the Class – Roland Fru (1999/05/03)

Modem Communication – Gyoung-rock Kim (1999/04/26)

i have error codes – Orlando Alas (1999/04/22)

Problems using the project! – MarcioJunges (1999/04/17)

Problems with multi-accesses – Paul Martin (1999/04/14)

Can’t unzip the download file too – min ji (1999/04/09)

Shutdown while receiving data – Shawn (1999/04/07)

Bug in WriteToPort – Jayant Umrani (1999/04/02)

How about FlowControl? – Chris Z. (1999/03/31)

How do I assert RTS & DTR? – Paul Martin (1999/03/26)

How to modify to use in Doc/View Architecture – Dhanesh (1999/03/22)

Handshaking uising RTS/CTS ? – Jayant Umrani (1999/03/20)

I can’t use this class – Olivier (1999/03/19)

Modify for unicode – David Laarakkers (1999/03/11)

Modem Initialization and handshaking – Padmanabhan Ganesan (1999/03/11)

bug fixes ?? – Dhanesh Ramachandram (1999/03/10)

Can this class communicate from port1 to port2? – Shahzad (1999/03/06)

work around the hang – Jinhui (1999/03/06)

TimeOuts??? – Padmanabhan Ganesan (1999/03/05)

Very Newbie Question – Brian Masuda (1999/03/03)

CArchive compatable class? – Bill Foust (1999/02/26)

Final correction for data lost ( Yes yes i find… ) – Chainard Arnaud (1999/02/26)

TAPI / Modem / COM / Init information – Seain B. Conover (1999/02/24)

Good Work ! – R. Schnell (1999/02/24)

About modem data communication – Zhaohui Xing (1999/02/23)

Solution for NT – Michel Wassink (1999/02/23)

Modem – Chida (1999/02/22)

How many ports could be read? – Clay Bearden (1999/02/18)

Nasty stuff in Kamals code – Roar Skinderviken (1999/02/17)

There be dragons here 🙂 – Kamal (1999/01/25)

Serial communication using VCOMM – Sanjay Singh (1999/01/25)

Can’t unzip the download file (commtest.zip) – Alan Copeland (1999/01/21)

WaitCommEvent() NT4 problem – Dave Bryan (1999/01/21)

A Class for the serial Port – Frederik Fromm (1999/01/21)

Updated class? – Crister T (1999/01/14)

Modified CSerialPort still doesn’t send data when receiving data – Heiko Bergmann (1998/11/30)

NT problems ? – Carl Chipman (1998/11/25)

modified CSerialPort class – Brian Koh (1998/11/24)

CSerialPort does not shut down, if bytes are still receiving?! – Detlef Schmicker (1998/11/23)

Modified Destructor Code – Brian Koh (1998/11/22)

Some Trouble in receiving data permanently – KangBC (1998/11/21)

Problems reading data – Dennis Rivard (1998/11/19)

Help with demo project – Denis Breton (1998/11/10)

Doesn’t like to be init’d twice – Phil Christensen (1998/10/25)

Possible timing issues – David Hill (1998/10/13)

Excellent serial port class – Steve Feng (1998/10/02)

Stay on top of the latest technology trends — delivered directly to your inbox, free!

Subscription Form Posts

Don't worry, we don't spam

Written by Bobby

Bobby Lawson is a seasoned technology writer with over a decade of experience in the industry. He has written extensively on topics such as cybersecurity, cloud computing, and data analytics. His articles have been featured in several prominent publications, and he is known for his ability to distill complex technical concepts into easily digestible content.