zoukankan      html  css  js  c++  java
  • 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 << "	" <<  data << endl;
    52 }
    53 
    54 void SuperTerminal::handle_read(char buf[], boost::system::error_code ec,std::size_t bytes_transferred)
    55 {
    56     cout << "
    handle_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 }
  • 相关阅读:
    SpringMVC + MyBatis简单示例
    JSP---JSTL核心标签库的使用
    HBASE安装 (HA)
    HBase 常用Shell命令(转载)
    kafka quick start
    在IDEA中实战Git
    kibana6.2.2安装
    elasticsearch-head插件安装
    elasticsearch6.2.2安装
    jdk1.8安装
  • 原文地址:https://www.cnblogs.com/kernel0815/p/3624632.html
Copyright © 2011-2022 走看看