Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void foo()
- {
- io_service io_svc;
- serial_port ser_port(io_svc, "your string here");
- deadline_timer timeout(io_svc);
- unsigned char my_buffer[1];
- bool data_available = false;
- ser_port.async_read_some(boost::asio::buffer(my_buffer),
- boost::bind(&read_callback, boost::ref(data_available), boost::ref(timeout),
- boost::asio::placeholders::error,
- boost::asio::placeholders::bytes_transferred));
- timeout.expires_from_now(boost::posix_time::milliseconds(<<your_timeout_here>>));
- timeout.async_wait(boost::bind(&wait_callback, boost::ref(ser_port),
- boost::asio::placeholders::error));
- io_svc.run(); // will block until async callbacks are finished
- if (!data_available)
- {
- kick_start_the_device();
- }
- }
- void read_callback(bool& data_available, deadline_timer& timeout, const boost::system::error_code& error, std::size_t bytes_transferred)
- {
- if (error || !bytes_transferred)
- {
- // No data was read!
- data_available = false;
- return;
- }
- timeout.cancel(); // will cause wait_callback to fire with an error
- data_available = true;
- }
- void wait_callback(serial_port& ser_port, const boost::system::error_code& error)
- {
- if (error)
- {
- // Data was read and this timeout was canceled
- return;
- }
- ser_port.cancel(); // will cause read_callback to fire with an error
- }
- boost::asio::ip::tcp::socket socket(io_service);
- ...
- boost::asio::socket_base::non_blocking_io command(true);
- socket.io_control(command);
- boost::asio::ip::tcp::socket socket(io_service);
- ...
- boost::asio::socket_base::bytes_readable command(true);
- socket.io_control(command);
- std::size_t bytes_readable = command.get();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement