A communication class for serial port

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

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值