Click here to Skip to main content
12,830,743 members (42,138 online)
Click here to Skip to main content
Add your own
alternative version

Stats

46.2K views
8.1K downloads
43 bookmarked
Posted 5 Aug 2010

CSerialIO - A Useful and Simple Serial Communication Class

, 5 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
It implements the serial communication using a thread to enable data being captured anytime, you just handle the event of READ/WRITE/OPEN/CLOSE via inheriting this class

Introduction

I have searched a lot of samples supporting synchronous and asynchronous serial communication, but the drawback is that I need to implement a thread or a timer to always keep reading the serial data, so I encapsulated this serial communication class with a thread to make it easy to handle the event (READ/WRITE/OPEN/CLOSE) of accessing serial port.

The SerialCtrlDemo project illustrates how to use CSerialIO class and is easy to present the serial communication event information as depicted in the following UI:

SerialCtrl/serial.JPG

CSerialIO Class

The CSerialIO class is defined for external API and the primary methods listed below:

class CSerialIO{ 
public: 
CSerialIO(); 
virtual ~CSerialIO(); 
void OpenPort(CString strPortName,CString strBaudRate);//open serial port with 
					//parameter port name and baud rate 
virtual void OnEventOpen(BOOL bSuccess);	//handle the event whether the port is 
					//successfully opened 
void ClosePort();//close serial port 
virtual void OnEventClose(BOOL bSuccess);	//handle the event whether the port 
					//is successfully closed 
virtual void OnEventRead(char *inPacket,int inLength);	//handle the received data 
						//from serial 
void Write(char *outPacket,int outLength);	// write data directly 
virtual void OnEventWrite(int nWritten); 	//handle the event of how many bytes 
					//have been written 
… 
}; 

How to Use CSerialIO Class

Take the following steps to use the CSerialIO class:

  1. Add the SerialCtrl.h & SerialCtrl.cpp files in your VC++ project.
  2. Add the line #include "SerialCtrl.h" in your dialog's header file.
  3. Inherit the CSerialIO class in your dialog.
  4. Handle the event (READ/WRITE/OPEN/CLOSE) in your dialog class via overwrite the virtual function.
//Handle the event of opening serial port
void CSerialCtrlDemoDlg::OnEventOpen(BOOL bSuccess)
{
 CString str;
 if (bSuccess)
 {
  str=m_strPortName+" open successfully";
  bPortOpened=TRUE;
  m_btnOpen.SetWindowText("Close");  
 }
 else
 {
  str=m_strPortName+" open failed";
 }
 m_staticInfo.SetWindowText(str);
}

//handle the event of closing serial port
void CSerialCtrlDemoDlg::OnEventClose(BOOL bSuccess)
{
 CString str;
 if (bSuccess)
 {
  str=m_strPortName+" close successfully";
  bPortOpened=FALSE;
  m_btnOpen.SetWindowText("Open");
 }
 else
 {
  str=m_strPortName+" close failed";
 }
 m_staticInfo.SetWindowText(str);
}

//Handle the event of reading data from serial port
void CSerialCtrlDemoDlg::OnEventRead(char *inPacket,int inLength)
{ 
 m_listboxRead.AddString(inPacket);
 CString str;
 str.Format("%d bytes read",inLength);
 m_staticInfo.SetWindowText(str);
}

//Handle the event of writing data 
void CSerialCtrlDemoDlg::OnEventWrite(int nWritten)
{
 if (nWritten>0)
 {
  CString str;
  str.Format("%d bytes written",nWritten);
  m_staticInfo.SetWindowText(str);
 }
 else
 {
  m_staticInfo.SetWindowText("Write failed");
 }
}

Note

This code has been verified with RS-232 connector, and you can easily customize the event handler in run function of SerialThread class.

History

  • 5th August, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

tojine
Engineer
China China
I am a software engineer with programming experience C/C++, MFC/Win32,Assembler(C51/ARM),Java and work in Science Park of ShenZhen City,the global manufacturer center,in China.
I like pingpong,basketball,bowling and table tennis sport.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionHow to open and handle multi ports? Pin
Bill Lee Vn12-Feb-15 16:50
memberBill Lee Vn12-Feb-15 16:50 
Questioncan not show inPacket in listbox Pin
liunan_201012-Dec-11 4:47
memberliunan_201012-Dec-11 4:47 
QuestionUnable to read.............. Pin
PRASAD DHABEKAR22-Jul-11 0:31
memberPRASAD DHABEKAR22-Jul-11 0:31 
Generaldoubt in A Useful and Simple Serial Communication Class Pin
steven8Gerrard28-May-11 2:09
membersteven8Gerrard28-May-11 2:09 
General不能设置波特率 还有串口时延很长 Pin
PLA70111-Feb-11 0:04
memberPLA70111-Feb-11 0:04 
Generalusing SerialIO class in win32 application Pin
Member 446051218-Jan-11 21:44
memberMember 446051218-Jan-11 21:44 
GeneralDetected memory leaks Pin
GuKaKa-CC3-Jan-11 22:00
memberGuKaKa-CC3-Jan-11 22:00 
AnswerRe: Detected memory leaks - Fixed Pin
Steve Mayfield5-Mar-11 19:26
memberSteve Mayfield5-Mar-11 19:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 5 Aug 2010
Article Copyright 2010 by tojine
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid