I have an application using the Boost library with the following code:
//Set up for wait and read.
wait_result = in_progress;
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::milliseconds(1000));
timer.async_wait(boost::bind(&SerialComm::timeoutExpired,this,
boost::asio::placeholders::error));
// This blocks until an event on io_service_ is set.
io_service_.run_one();
while(wait_result == in_progress){
// Brackets in success case limit scope of new variables
switch(wait_result){
case success:{
timer.cancel();
// Remove the delimiter byte - don't want this.
bytes_transferred -= 1;
istream is(&readData);
// Allocation of string
string msg(bytes_transferred, '\0');
is.read(&msg[0], bytes_transferred);
// Remove the delimiter
is.ignore(1);
data_handler(msg);
break ;}
case timeout_expired:
port.cancel();
cout << "Time is up..." << endl;
break ;
case error_out:
cout << "Serial port error read..." << endl;
break ;
case in_progress:
break;
}
}
where timer is a boost::asio::deadline_timer and the callback function timeoutExpired looks like:
void SerialComm::timeoutExpired(const boost::system::error_code &error){
if (!error && wait_result == in_progress){wait_result = timeout_expired;}
}
The problem is that the callback function is not expiring. If I place a breakpoint in the callback function, it is never hit. If I put a watch on the timer variable, what I notice is that the expiry is exactly 6 h ahead of where it should be (I am assuming this is because it is UTC).
According to the boost documentation of the deadline_timer, the clock works in UTC, but this appeared to be functioning properly yesterday with no problem so I can assume that UTC is not the problem. Any ideas as to why this might not be firing properly?
Aucun commentaire:
Enregistrer un commentaire