|
Boost Users : |
Subject: [Boost-users] Asio problems with async_read
From: Allan Nielsen (a_at_[hidden])
Date: 2011-08-08 08:13:08
Hi
I have a funciton where we I am using boost::asio to read from the
serial port of my computer.
I have started out with an implementation which uses read, and I am
now trying to convert it to async_read because I want a time out.
When I am trying to use the async_read version, I get timeouts after I
have read one or two charterers, even though I know there are more
charterers to be read on the wire.
Here is the code:
void set_result( boost::optional<boost::system::error_code> * a,
boost::system::error_code b ) {
a->reset( b );
}
std::vector<uint8_t> receive(boost::asio::io_service & io,
boost::asio::serial_port & cctalk_port){
#if 0
boost::optional<boost::system::error_code> timer_result;
boost::optional<boost::system::error_code> read_result;
boost::asio::deadline_timer timer( io );
timer.expires_from_now( boost::posix_time::milliseconds(5000) );
timer.async_wait( boost::bind(&set_result, &timer_result, _1) );
boost::asio::streambuf result;
boost::asio::async_read(
cctalk_port,
result,
boost::asio::transfer_at_least(1),
boost::bind( &set_result, &read_result, _1 ));
while(1) {
io.poll_one();
if ( read_result ) {
timer.cancel();
std::vector<uint8_t> res(boost::asio::buffer_cast<const
uint8_t * >(result.data()), boost::asio::buffer_cast<const uint8_t *
>(result.data()) + result.size() );
return res;
} else if ( timer_result ) {
throw runtime_error("timeout");
}
}
throw runtime_error("Should not happend");
#else
uint8_t c;
boost::asio::read(cctalk_port, boost::asio::buffer(&c, 1));
std::vector<uint8_t> res;
res.push_back(c);
return res;
#endif
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net