【发布时间】:2017-09-02 03:53:50
【问题描述】:
我目前正在开发一个应用程序,用于与使用串行通信的设备进行通信。为此,我使用了 boost 库 basic_serial_port。现在,我只是尝试从设备中读取数据,并且正在使用 async_wait_until 函数以及 deadline_timer 类中的 async_wait。设置读取和等待的代码如下所示:
async_read_until(port,readData,io_params.delim,
boost::bind(&SerialComm::readCompleted,
this,boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
timer.expires_from_now(boost::posix_time::seconds(1));
timer.async_wait(boost::bind(&SerialComm::timeoutExpired,this,
boost::asio::placeholders::error));
async_read_until 上的回调看起来像
void SerialComm::readCompleted(const boost::system::error_code& error,
const size_t bytesTransferred){
if (!error){
wait_result = success;
bytes_transferred = bytesTransferred;
}
else {
if (error.value() != 125) wait_result = error_out;
else wait_result = op_canceled;
cout << "Port handler called with error code " + to_string(error.value()) << endl;
}
}
成功读取后触发以下代码
string msg;
getline(istream(&readData), msg, '\r');
boost::trim_right_if(msg, boost::is_any_of("\r"));
在此设备的情况下,所有消息都以回车结束,因此在async_read_until 中指定回车应该检索单个消息。但是,我看到的是,在触发处理程序时,新数据不一定会输入缓冲区。所以,我可能看到的是,如果处理程序被触发 20x
- 在第一次调用中将一条线泵入缓冲区
- 在接下来的 6 次调用中都没有
- 下次调用6行
- 接下来的 10 天没有数据
- 10 行以下 ...
我显然没有正确地做某事,但它是什么?
【问题讨论】:
标签: c++ c++11 asynchronous boost boost-asio