我编写了以下 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/