温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

boost - 串口通信

发布时间:2020-05-28 02:30:27 来源:网络 阅读:2630 作者:mayacong 栏目:移动开发

备忘使用。

  1. #include <boost/bind.hpp> 
  2. #include <boost/asio.hpp> 
  3. #include <boost/thread.hpp> 
  4. using boost::asio::io_service; 
  5. using boost::system::error_code; 
  6. using boost::asio::serial_port; 
  7. using boost::asio::deadline_timer; 
  8. using boost::asio::buffer; 
  9.  
  10. class MyCom 
  11. public
  12.     MyCom(void
  13.     { 
  14.         _pSerialPort= new serial_port(_ios); 
  15.      _pTimer = new deadline_timer(_ios); 
  16.     } 
  17.     ~MyCom(void
  18.     { 
  19.         if (_pTimer != NULL) 
  20.         { 
  21.          delete _pTimer; 
  22.          _pTimer = NULL; 
  23.         } 
  24.         if (_pSerialPort != NULL) 
  25.         { 
  26.          delete _pSerialPort; 
  27.          _pSerialPort = NULL; 
  28.         } 
  29.     } 
  30.     void Open(const string& comName); 
  31.     { 
  32.         _pSerialPort->open(comName); 
  33.         _pSerialPort->set_option(serial_port::flow_control(serial_port::flow_control::none));   //流量控制为none 
  34.         _pSerialPort->set_option(serial_port::parity(serial_port::parity::none));   //奇偶检验为none 
  35.         _pSerialPort->set_option(serial_port::stop_bits(serial_port::stop_bits::one));  //停止位为1 
  36.         _pSerialPort->set_option(serial_port::character_size(8));   //字符大小(数据位)为8 
  37.         _pSerialPort->set_option(serial_port::baud_rate(115200));//波特率 
  38.     } 
  39.     void Send(const string& data) 
  40.     {//同步发数据 
  41.         _mutex.lock(); 
  42.         _pSerialPort->write_some(buffer(data, data.size())); 
  43.         _mutex.unlock(); 
  44.     } 
  45.     string Recv() 
  46.     {//异步收数据 
  47.         _mutex.lock(); 
  48.         memset(_buf, 0, sizeof(_buf)); 
  49.         _pSerialPort->async_read_some(buffer(_buf, 256),  
  50.          boost::bind(&MyCom::RecvHandle, this,  
  51.          boost::asio::placeholders::error,//传送错误码 
  52.          boost::asio::placeholders::bytes_transferred//传送字节数 
  53.             )); 
  54.         _mutex.unlock(); 
  55.         _pTimer->expires_from_now(boost::posix_time::millisec(SLEEP_TIME)); 
  56.         _pTimer->async_wait(boost::bind(&serial_port::cancel, _pSerialPort)); 
  57.         _ios.run();//异步情况下使用词句才开始执行 
  58.         _ios.reset();//还原状态 
  59.         return string(_buf, _ret); 
  60.     } 
  61.     void Close() 
  62.     { 
  63.         _mutex.lock(); 
  64.         if (_pSerialPort->is_open()) 
  65.          _pSerialPort->close(); 
  66.         _mutex.unlock(); 
  67.     } 
  68.  
  69. protected
  70.     void RecvHandle(const error_code& error, size_t bytes_transferred) 
  71.     { 
  72.         if (!error) 
  73.          _pTimer->cancel();//没有错误就结束定时器 
  74.         _ret = bytes_transferred; 
  75.     } 
  76.  
  77. private
  78.     boost::asio::io_service _ios; 
  79.     serial_port* _pSerialPort; 
  80.     deadline_timer* _pTimer; 
  81.     char _buf[256]; 
  82.     size_t _ret; 
  83.     boost::mutex _mutex; 
  84. }; 

 

 

 

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI