I'm using the boost::asio library to control a motor with serial connection. The problem is that the boost::asio::serial_port::read() function would get the program to freeze if the device does not send anything for me to read.
How can I check whether the device has something to tell me through the port?
If I send a command to the device, I can get an answer back with no problems. But is there a way to know whether it sent anything without me sending a command? or does that not make any sense in serial connections, where a command is only received as a response to a sent command?
Please check my code
try
{
port = new boost::asio::serial_port(io_serial, comPort.c_str());
}
char rcvd;
std::string res;
while(1)
{
boost::asio::read(*port,boost::asio::buffer(&rcvd, 1)); //here it freezes till something is read
std::cout<<rcvd<<std::endl; //I know it froze in the last line because nothing was written from here
if(rcvd == '\r' || rcvd == '\0')
{
break;
}
res.push_back(rcvd);
}
return res;
Thanks for any efforts.