A communication class for serial port

本文介绍了一个用于串口通信的CSerialPort类,适用于驱动或读取硬件设备。该类易于使用,只需创建实例并调用InitPort即可初始化串口设置。文章提供了示例代码展示如何在软件中使用此类进行数据收发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
 
  1. // the owner (CWnd) of the port (receives message)
  2.  
  3. BOOL CSerialPort::InitPort(CWnd* pPortOwner,
  4. UINT portnr, // portnumber (1..4)
  5. UINT baud, // baudrate
  6. char parity, // parity
  7. UINT databits, // databits
  8. UINT stopbits, // stopbits
  9. DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
  10. 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:

 
  1. portnr = 1
  2. baud = 19200
  3. parity = 'N'
  4. databits = 8,
  5. stopsbits = 1,
  6. dwCommEvents = EV_RXCHAR | EV_CTS,
  7. nBufferSize = 512);

So the follwing code is enough to make communication possible:

in the header of the owner:

 
  1. CSerialPort m_Serial;

in the code:

 
  1. m_Serial.InitPort(this);
  2. 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:

 
  1. BEGIN_MESSAGE_MAP(CCommtestDlg, CDialog)
  2. //{{AFX_MSG_MAP(CCommtestDlg)
  3. ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
  4. ON_MESSAGE(WM_COMM_CTS_DETECTED, OnCTSDetected)
  5. //}}AFX_MSG_MAP
  6. END_MESSAGE_MAP()
and they must be handled:
 
  1. LONG CCommtestDlg::OnCommunication(WPARAM ch, LPARAM port)
  2. {
  3. // do something with the received character
  4.  
  5. return 0;
  6. }

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

Downloads

Download sample project - 66K


转自:http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2483/A-communication-class-for-serial-port.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值