您的位置:首页 > 其它

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

2016-04-28 16:14 459 查看
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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: