c++ - 如果读取处理程序不会被调用,有什么方法可以知道在 async_read 函数中传输的字节数?

标签 c++ boost boost-asio

我编写了以下 DoRead 函数,它从打开的串口读取数据,除一件事外它按预期工作:

  • 当读取完成前超时过去时,将不会调用任何读取处理程序,此时我无法获取读取的字节数。

这是我的代码:

std::size_t wxSerialPort::DoRead(std::string& str, const int timeout)
{
    m_bytes_transferred_read = 0;

    boost::asio::async_read(m_serialPort, boost::asio::buffer(str),
                            std::bind(&wxSerialPort::AsyncReadHandler, this,
                                      std::placeholders::_1, std::placeholders::_2));

    m_io_context.restart();

    if (timeout == wxTIMEOUT_INFINITE)
    {
        m_io_context.run_until(std::chrono::steady_clock::time_point::max());
    }
    else
    {
        m_io_context.run_for(std::chrono::milliseconds(timeout));
    }

    return m_bytes_transferred_read; // At this point I always get 0 bytes read.
}

void wxSerialPort::AsyncReadHandler(const boost::system::error_code& error, std::size_t bytes_transferred)
{
    m_bytes_transferred_read = bytes_transferred;
}

请记住,任何以 m_ 开头的变量都是成员变量。

但是如果我给函数一个小缓冲区,那么读取处理程序将在超时之前被调用,并且我得到读取的实际字节数。

提前谢谢你。

最佳答案

听起来您需要调用 async_read_some 而不是 async_read

async_read函数确保在异步操作完成之前读取请求的数据量,即在调用读取处理程序之前需要足够的数据来填充缓冲区。

basic_serial_port::async_read_some方法在接收到数据时调用读取处理程序,无论缓冲区是否已满。

因此只需将对 async_read 的调用替换为:

m_serialPort.async_read_some(boost::asio::buffer(str),
                        std::bind(&wxSerialPort::AsyncReadHandler, this,
                                  std::placeholders::_1, std::placeholders::_2));

关于c++ - 如果读取处理程序不会被调用,有什么方法可以知道在 async_read 函数中传输的字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57300321/

相关文章:

c++ - 提升 ASIO : How can a server know if a client is still connected?

c++ - boost async_write() 和 non_blocking socket.send() 之间的区别

c++ - 什么时候应该将多线程与 asio 一起使用?

c++ - 对分配器感知类调用复制构造函数中的 vector 元素的引用

c++ - 设备数据链路设计模式

c++ - CreateFile2、WriteFile 和 ReadFile : how can I enforce 16 byte alignment?

c++ - libboost_log_setup.a 库的原因?

C++ Boost 读取 CSV 数据并向某些列添加双引号,然后重新写回文件

c++ - 绑定(bind)功能麻烦

c++ - 在 vector 中查找结构