asio::read with timeout(asio::read 超时)
问题描述
我需要知道如何超时读取(同步或异步无关紧要).我想检查设备是否连接了串口.
I need to know how to read (sync or async doesn't matters) with a timeout. I want to check if a device is connected with a serial port or not.
为此,我使用 asio::write
然后等待设备的响应.
For that I use asio::write
and then I wait for the response of the device.
如果连接了设备 asio::read(serial, boost::asio::buffer(&r,1))
工作正常,但如果没有设备,程序会停止,这这就是为什么我需要超时
If a device is connected asio::read(serial, boost::asio::buffer(&r,1))
works fine but if there is no device the program stops, which is is why I need the timeout
我知道我需要一个 deadline_timer
但我不知道如何在 async_read
函数中使用它.
I know that I need a deadline_timer
but I have no idea how to use it in the async_read
function.
它是如何工作的一个例子会非常有帮助.
An example of how it works would be really helpful.
我知道有很多类似的主题,我阅读了很多,但我找不到可以帮助我解决问题的解决方案!
I know that there are many similar threads and I read lot of them but I can't find a solution that helps me solving my problem!
推荐答案
Igor R 发布的代码没有为我编译.这是我对他的代码的改进版本,效果很好.它使用 lambdas 来摆脱 set_result
辅助函数.
The code posted by Igor R. did not compile for me. Here is my improved version of his code, which works perfectly. It uses lambdas to get rid of the set_result
helper function.
template <typename SyncReadStream, typename MutableBufferSequence>
void readWithTimeout(SyncReadStream& s, const MutableBufferSequence& buffers, const boost::asio::deadline_timer::duration_type& expiry_time)
{
boost::optional<boost::system::error_code> timer_result;
boost::asio::deadline_timer timer(s.get_io_service());
timer.expires_from_now(expiry_time);
timer.async_wait([&timer_result] (const boost::system::error_code& error) { timer_result.reset(error); });
boost::optional<boost::system::error_code> read_result;
boost::asio::async_read(s, buffers, [&read_result] (const boost::system::error_code& error, size_t) { read_result.reset(error); });
s.get_io_service().reset();
while (s.get_io_service().run_one())
{
if (read_result)
timer.cancel();
else if (timer_result)
s.cancel();
}
if (*read_result)
throw boost::system::system_error(*read_result);
}
这篇关于asio::read 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:asio::read 超时
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01