溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

boost - 串口通信

發(fā)布時(shí)間:2020-05-28 02:30:27 來源:網(wǎng)絡(luò) 閱讀:2645 作者:mayacong 欄目:移動(dòng)開發(fā)

備忘使用。

  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));   //奇偶檢驗(yàn)為none 
  35.         _pSerialPort->set_option(serial_port::stop_bits(serial_port::stop_bits::one));  //停止位為1 
  36.         _pSerialPort->set_option(serial_port::character_size(8));   //字符大小(數(shù)據(jù)位)為8 
  37.         _pSerialPort->set_option(serial_port::baud_rate(115200));//波特率 
  38.     } 
  39.     void Send(const string& data) 
  40.     {//同步發(fā)數(shù)據(jù) 
  41.         _mutex.lock(); 
  42.         _pSerialPort->write_some(buffer(data, data.size())); 
  43.         _mutex.unlock(); 
  44.     } 
  45.     string Recv() 
  46.     {//異步收數(shù)據(jù) 
  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,//傳送錯(cuò)誤碼 
  52.          boost::asio::placeholders::bytes_transferred//傳送字節(jié)數(shù) 
  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();//異步情況下使用詞句才開始執(zhí)行 
  58.         _ios.reset();//還原狀態(tài) 
  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();//沒有錯(cuò)誤就結(jié)束定時(shí)器 
  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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI