您的位置:首页 > 编程语言

boost::asio::serial_port 串口编程

2017-06-08 13:12 405 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: