Advertisement
mesmariusz

Untitled

Apr 7th, 2016
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <boost/asio/serial_port.hpp>
  4. #include <boost/asio.hpp>
  5. #include "blocking_reader.h"
  6.  
  7. using namespace boost;
  8.  
  9. std::string read_response() {
  10.  
  11. asio::io_service io;
  12. asio::serial_port port(io);
  13.  
  14. port.open("COM3");
  15. port.set_option(asio::serial_port_base::baud_rate(115200));
  16.  
  17. // A blocking reader for this port that
  18. // will time out a read after 500 milliseconds.
  19. blocking_reader reader(port, 500);
  20.  
  21. char c;
  22.  
  23. std::string rsp;
  24.  
  25. // read from the serial port until we get a
  26. // \n or until a read times-out (500ms)
  27. while (reader.read_char(c) && c != '\n') {
  28. rsp += c;
  29. }
  30.  
  31. if (c != '\n') {
  32. // it must have timed out.
  33. throw std::exception("Read timed out!");
  34. }
  35.  
  36. return rsp;
  37. }
  38.  
  39. int main()
  40. {
  41. char character;
  42. std::cout << "Waiting for response..." << std::endl;
  43. character = read_response();
  44. std::cout << "Character is: " << character << std::endl;
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement