boost::asio::serial_port 串口编程

来源:互联网 发布:常见男性英文名知乎 编辑:程序博客网 时间:2024/06/07 13:45

from: http://blog.csdn.net/anda0109/article/details/41726261

// boostSerialPort.cpp : 定义控制台应用程序的入口点。  //  #include "stdafx.h"  #include <iostream>  #include <boost/asio.hpp>  using namespace std;  //using namespace boost::asio;  int _tmain(int argc, _TCHAR* argv[])  {      try      {          boost::asio::io_service io;          boost::asio::serial_port sp(io,"COM2");          //设置串口参数          sp.set_option(boost::asio::serial_port::baud_rate(9600));          sp.set_option(boost::asio::serial_port::flow_control());          sp.set_option(boost::asio::serial_port::parity());          sp.set_option(boost::asio::serial_port::stop_bits());          sp.set_option(boost::asio::serial_port::character_size(8));          boost::system::error_code err;          while(true)          {              char buf[]="hello";              int nWrite = sp.write_some(boost::asio::buffer(buf),err);              if(err)              {                  cout<<"write_some err,errmessage:"<<err.message()<<endl;                  break;              }              else              {                  char buf[1024];                  sp.read_some(boost::asio::buffer(buf),err);                  if(!err)                  {                      cout<<"recv data:"<<buf<<endl;                  }              }          }          io.run();      }      catch (exception& err)      {          cout << "Exception Error: " << err.what() << endl;      }      return 0;  }  
原创粉丝点击