Trending

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!

Target

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.

Usage

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:

 m_Serial.InitPort(this);
 m_Serial.StartMonitoring();

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)
 //{{AFX_MSG_MAP(CCommtestDlg)
 ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
 ON_MESSAGE(WM_COMM_CTS_DETECTED, OnCTSDetected)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

Downloads

Download sample project – 66K

History

Date Posted: February 27, 1998

Comments

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.