boost实现串口通信(一):小试牛刀

 1 /************************************************************************/
 2 /* 功能:boost实现串口通信类                                            */
 3 /* 作者: kernelmain QQ:835609451                                        */
 4 /* 版本:v0.1 build at 2014.3.25                                        */
 5 /************************************************************************/
 6 
 7 #include <boost/asio.hpp>
 8 #include <boost/bind.hpp>
 9 #include <iostream>
10 
11 using namespace std;
12 using namespace boost::asio;
13 
14 typedef string anytype;
15 
16 class SuperTerminal
17 {
18 public:
19     SuperTerminal(const anytype &port_name);
20     ~SuperTerminal();
21 
22 private:
23     bool init_port(const anytype port, const unsigned int char_size);
24 
25 public:
26     void write_to_serial(const anytype data);
27     void read_from_serial();
28     void handle_read(char buf[], boost::system::error_code ec,std::size_t bytes_transferred);
29     void call_handle();
30 
31 private:
32     io_service m_ios;
33     serial_port *pSerialPort;
34     anytype m_port;
35     boost::system::error_code m_ec;
36 };
 1 /************************************************************************/
 2 /* 功能:boost实现串口通信类                                            */
 3 /* 作者: kernelmain QQ:835609451                                        */
 4 /* 版本:v0.1 build at 2014.3.25                                        */
 5 /************************************************************************/
 6 #include "SuperTerminal.h"
 7 
 8 SuperTerminal::SuperTerminal(const anytype & port_name):pSerialPort(NULL)
 9 {
10     pSerialPort = new serial_port(m_ios);
11     if (pSerialPort)
12     {
13         init_port(port_name,8);
14     }
15 }
16 
17 SuperTerminal::~SuperTerminal()
18 {
19     if (pSerialPort)
20     {
21         delete pSerialPort;
22     }
23 }
24 
25 bool SuperTerminal::init_port(const anytype port, const unsigned int char_size)
26 {
27     if (!pSerialPort)
28     {
29         return false;
30     }
31 
32     pSerialPort->open(port,m_ec);
33 
34     pSerialPort->set_option(serial_port::baud_rate(115200),m_ec);
35     pSerialPort->set_option(serial_port::flow_control(serial_port::flow_control::none),m_ec);
36     pSerialPort->set_option(serial_port::parity(serial_port::parity::none),m_ec);
37     pSerialPort->set_option(serial_port::stop_bits(serial_port::stop_bits::one),m_ec);
38     pSerialPort->set_option(serial_port::character_size(char_size),m_ec);
39 
40     return true;
41 }
42 
43 void SuperTerminal::write_to_serial(const anytype data)
44 {
45     if (!pSerialPort)
46     {
47         return;
48     }
49 
50     size_t len = write(*pSerialPort, buffer(data),m_ec);
51     cout << len << "\t" <<  data << endl;
52 }
53 
54 void SuperTerminal::handle_read(char buf[], boost::system::error_code ec,std::size_t bytes_transferred)
55 {
56     cout << "\nhandle_read: " ;
57     cout.write(buf, bytes_transferred);
58 }
59 
60 void SuperTerminal::read_from_serial()
61 {
62     char v[10];
63     async_read(*pSerialPort, buffer(v), boost::bind(&SuperTerminal::handle_read,this,v,_1, _2));
64 }
65 
66 void SuperTerminal::call_handle()
67 {
68     m_ios.run();
69 }

一直比较喜欢c++的简洁, 尤其用上boost后,代码显得更加简洁。

想把之前给同学用C#做的刷苹果设备软件用C++重写一下,一点一点来, 计划:

(1). 在控件台程序中调试熟悉boost串口通信

(2). 用C++ builder xe5画界面,调试。

 (3). 实现windows超级终端的所有功能。

 (4). 添加扩展功能。

 

下面是控件台程序测试代码:

 1 #include <vector>
 2 #include <string>
 3 
 4 #define BOOST_REGEX_NO_LIB
 5 #define BOOST_DATE_TIME_SOURCE
 6 #define BOOST_SYSTEM_NO_LIB
 7 #include "SuperTerminal.h"
 8 
 9 int main()
10 {
11     try
12     {
13         SuperTerminal sp("COM1");
14         sp.write_to_serial("serialPort");
15         sp.read_from_serial();
16         sp.call_handle();
17         getchar();
18         return 0;
19     }
20     catch (std::exception &e)
21     {
22         cout << e.what();
23         getchar();
24     }
25 }

 

 

posted on 2014-03-26 00:30 kernel_main 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/kernel0815/p/3624632.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值