A communication class for serial port

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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


Comments

  • How to start using the provided project in VC2010

    Posted by Rizwan on 09/21/2017 09:03am

    Hi, i converted the project in VC2010 and it worked fine. I can build the project but where is the main method. How can i use it?? if i create a new class with main method i start getting all sort of compilation errors. Can someone let me know?

    Reply
  • How to Run it in VS 2015

    Posted by kamran bigdely on 02/15/2017 04:24pm

    Simply open the .dsp file (VC++ Project file) using VS 2015. VS asks you whether you want to convert it to upgrade it to the new project structure. Accept it. Then according to this post: (http://stackoverflow.com/questions/29336562/visual-studio-error-d8016-zi-and-o2-command-line-options-are-incompatible) you should : Go to the project's property page and change the value for: C/C++ | General | Debug Information Format To something other than "Program Database for Edit and Continue (/ZI)" For example, "Program Database (/Zi)" should work.

    Reply
  • vc6

    Posted by guoft on 02/07/2014 07:11pm

    thanks very much,i can try it.

    Reply
  • Thank you.

    Posted by nook on 10/29/2012 07:05pm

    Thank for your source code that help me to develop my project. I try communicate serial port in win7 but can't because it have some error in other library. Your source help me very much but I can use it only sent data but can't receive data went I use in my project. But it doesn't worry for me because I only sent data my project can work. Thank you.Remon

    Reply
  • Thank you verymuch

    Posted by nook on 10/29/2012 07:04pm

    Thank for your source code that help me to develop my project. I try communicate serial port in win7 but can't because it have some error in other library. Your source help me very much but I can use it only sent data but can't recieve data went I use in my project. But it does't worry for me because I only sent data my project can work. Thank you.Remon

    Reply
  • cann't compile under vc++2010

    Posted by wen on 08/23/2012 12:48am

    thank you But it cann't compile under vc++2010 would you help me solve it?

    Reply
  • cann't compile under VC++2010

    Posted by wen on 08/23/2012 12:47am

    thank you But it cann't compile under vc++2010 would you help me solve it?

    • 2010

      Posted by roy on 01/25/2016 05:52am

      tell me the problem I have used it in VS2010 without any problem

      Reply
    Reply
  • some problem in the VC++2010

    Posted by wen on 08/23/2012 12:45am

    Thank you your source coder I have some question it cann't compile under VC++2010 condition. Would you help me solve it?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date