boost - 串口通信 超时处理
来源:互联网 发布:矩阵和行列式的联系 编辑:程序博客网 时间:2024/04/28 11:20
另外一个方法是使用前面说过的异步读写+超时的方式,代码如下:#include <iostream>#include <boost/asio.hpp>#include <boost/bind.hpp>using namespace std;using namespace boost::asio;void handle_read(char *buf,boost::system::error_code ec, std::size_t bytes_transferred){ cout.write(buf, bytes_transferred);}int main(int argc, char* argv[]){ io_service iosev; serial_port sp(iosev, "COM1"); sp.set_option(serial_port::baud_rate(19200)); sp.set_option(serial_port::flow_control()); sp.set_option(serial_port::parity()); sp.set_option(serial_port::stop_bits()); sp.set_option(serial_port::character_size(8)); write(sp, buffer("Hello world", 12)); // 异步读 char buf[100]; async_read(sp, buffer(buf), boost::bind(handle_read, buf, _1, _2)); // 100ms后超时 deadline_timer timer(iosev); timer.expires_from_now(boost::posix_time::millisec(100)); // 超时后调用sp的cancel()方法放弃读取更多字符 timer.async_wait(boost::bind(&serial_port::cancel, boost::ref(sp))); iosev.run(); return 0;} //
boost::asio::async_read(m_port, boost::asio::buffer(m_cData), boost::bind(&SerialPort::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); boost::asio::deadline_timer timer(m_ios); timer.expires_from_now(boost::posix_time::millisec(1000*20)); timer.async_wait(boost::bind(&boost::asio::serial_port::cancel, boost::ref(m_port))); //超时后调用cancel()方法放弃读取更多字符//
boost::asio::async_read() cannot be used with null_buffers()
An attempt to use async_read() with null_buffers() causes the handler to be invoked even when the underlying file descriptor is not yet available for reading.
0 0
- boost - 串口通信 超时处理
- 串口通信问题处理
- boost 串口通信 udp rather than Tcp
- boost实现串口通信(一):小试牛刀
- boost asio 串口操作中修改字节超时间隔参数
- [Boost.asio] C++ boost::asio::serial_port 串口通信类
- 串口通信中ReadFile和WriteFile的超时详解!
- 串口通信中ReadFile和WriteFile的超时详解!
- 串口超时
- C++ boost::asio::serial_port 串口通信类 使用 封装
- unity3d串口通信,双线程一收一处理
- 串口通信--处理子线程中的错误
- 串口通信之数据接收处理1
- QSerialPort处理串口通信简单示例
- 串口通信之数据接收处理1
- ros与下位机通信常用的c++ boost串口通信
- boost 多线程 超时
- 设置串口读写超时
- 【2016-08-15】{spring事务}
- TCP/IP详解
- 设计模式 结构型设计模式之装饰模式
- Android属性动画Property Animation系列一之ValueAnimator
- iOS开发之邓白氏编码申请流程
- boost - 串口通信 超时处理
- iOS 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- Java之ByteBuffer详解
- Java学习(十)
- valgrind交叉编译移植
- ios uitableview 刷新某一个cell 或 section
- 滚动事件
- 【codeforces 704A Thor】
- 1137 矩阵乘法