0

What Im trying to achieve is the following:

Run radio in the receive mode and check for incoming packages. Once a second Id like a timer interrupt to execute and switch the radio into transmit mode to send a package and then return back to the receive mode.

Right now I have the following code:

int main(int argc, char** argv)
{

    boost::asio::io_service io;

    boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
    t.async_wait(boost::bind(sendSyncToDevices, boost::asio::placeholders::error, &t));

    io.run();
}

void sendSyncToDevices(const boost::system::error_code& ,
    boost::asio::deadline_timer* t)
{
  DoSomething();
  t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
  t->async_wait(boost::bind(sendSyncToDevices, boost::asio::placeholders::error, t));


}

however io.run() is a blocking call. Can somebody advice how I could add additional while(1) loop that would be checking for incoming messages on the radio?

Thanks!

Kamiccolo
  • 7,758
  • 3
  • 34
  • 47

1 Answers1

2

You may get your answer in similar post

How do I perform a nonblocking read using asio?

They have discussed a way of having the read/write call non-blocking.

Alternatively,

Doing while(1) on the app code may not be the best way to wait for an event or I/O. You can also try polling style (more of a C -style programming) using select() on those file descriptors. Select() is inherently a poll() function which serves much better than having while(1) and reading the file again and again.

Hope this helps.

Community
  • 1
  • 1
chandank
  • 953
  • 11
  • 24