Hello,
I want to use asio library for my serial communication module but I'm having trouble on asynchronous functionality.
In the following code block, I wanted to read 5 bytes from the first serial port of my computer in 10 seconds. But none of the handler functions were called. Firstly I waited not inputing any bytes but timer did not call wait_handler. Secondly I sent 5 bytes in 10 seconds from the other computer but read_handler was not called.
I could read the bytes using synchronous functions like read function, I mean there is nothing wrong with the communication.
I tried this code both on a Windows and a Linux host but results were the same..
Can you please check what can be wrong with the following code?
**************************************************************
#include <iostream>
#include
#include
#include
using namespace std;
using namespace boost::asio;
using namespace boost::system;
using namespace boost::posix_time;
struct wait_handler {
void operator()(const error_code& ec){
cout << "Timeout!" << endl;
}
};
struct read_handler {
void operator()(const error_code& ec, size_t bytes_transferred) {
cout << "Data read!" << endl;
}
};
int main(int argc, char* argv[])
{
char read_msg_[5];
io_service io;
serial_port theport(io, "com1");
deadline_timer * timer = new deadline_timer(io);
timer->expires_from_now(seconds(10));
wait_handler whandler;
timer->async_wait(whandler);
read_handler rhandler;
async_read(theport, buffer(read_msg_, 5), rhandler);
Sleep(30000); //30 seconds
return 0;
}
**************************************************************
Best Regards,
Gözde Bahar Koş
######################################################################
Dikkat:
Bu elektronik posta mesaji kisisel ve ozeldir. Eger size
gonderilmediyse lutfen gondericiyi bilgilendirip mesaji siliniz.
Firmamiza gelen ve giden mesajlar virus taramasindan gecirilmekte,
guvenlik nedeni ile kontrol edilerek saklanmaktadir. Mesajdaki
gorusler ve bakis acisi gondericiye ait olup Aselsan A.S. resmi
gorusu olmak zorunda degildir.
######################################################################
Attention:
This e-mail message is privileged and confidential. If you are
not the intended recipient please delete the message and notify
the sender. E-mails to and from the company are monitored for
operational reasons and in accordance with lawful business practices.
Any views or opinions presented are solely those of the author and
do not necessarily represent the views of the company.
######################################################################