boost - 串口通信

備忘使用。

  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. }; 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章