boost::asio::serial

xiaoxiao2021-02-28  43

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; }
转载请注明原文地址: https://www.6miu.com/read-76804.html

最新回复(0)