Serial port 如何有效地阻止asio读取,直到串行设备上的数据可用?

Serial port 如何有效地阻止asio读取,直到串行设备上的数据可用?,serial-port,boost-asio,blocking,polling,Serial Port,Boost Asio,Blocking,Polling,我想使用asio从串行设备读取数据,并在字节可用时进行处理 使用async\u read\u时,由于文件结束(EOF)错误,我的回调大部分时间都被调用。因此,我需要退出回调并再次注册它,直到数据可用为止。这会导致一个繁忙的循环,我想阻止它。类似的情况也发生在read_,直到立即返回相同的EOF错误 对于POSIX,有poll来有效地等待数据在设备上可用,但我宁愿使用便携式(包括Windows)asio功能来实现类似的效果。在asio上有asio::descriptor\u base::bytes

我想使用asio从串行设备读取数据,并在字节可用时进行处理

使用
async\u read\u时,由于文件结束(EOF)错误,我的回调大部分时间都被调用。因此,我需要退出回调并再次注册它,直到数据可用为止。这会导致一个繁忙的循环,我想阻止它。类似的情况也发生在
read_,直到
立即返回相同的EOF错误

对于POSIX,有
poll
来有效地等待数据在设备上可用,但我宁愿使用便携式(包括Windows)asio功能来实现类似的效果。在asio上有
asio::descriptor\u base::bytes\u readable
,它允许我事先测试数据是否可用,但是我必须在活动循环中进行测试,直到数据可用为止


asio中是否有方法仅在数据可用时调用异步回调?例如,我是否可以在
async\u read\u中屏蔽错误,直到
,这样就不会对这些错误调用回调?

如果要在数据可用时读取数据,我建议使用该函数而不是
async\u read\u直到
,请参阅

典型的读取处理程序如下所示:

void read_handler(const boost::system::error_code& error, size_t bytes_transferred)
{
  if (error != boost::asio::error::operation_aborted)
  {
    if (error)
      // handle error
    else
    {
      // process bytes_transferred data from the buffer
      // enable reception by calling async_read_some on the socket with a buffer
    }
  }
}

如果要在数据可用时读取数据,我建议使用该函数而不是
async\u read\u,直到
,请参阅

典型的读取处理程序如下所示:

void read_handler(const boost::system::error_code& error, size_t bytes_transferred)
{
  if (error != boost::asio::error::operation_aborted)
  {
    if (error)
      // handle error
    else
    {
      // process bytes_transferred data from the buffer
      // enable reception by calling async_read_some on the socket with a buffer
    }
  }
}
也许你在找也许你在找