Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <boost/asio/serial_port.hpp>
- #include <boost/asio.hpp>
- #include "blocking_reader.h"
- using namespace boost;
- std::string read_response() {
- asio::io_service io;
- asio::serial_port port(io);
- port.open("COM3");
- port.set_option(asio::serial_port_base::baud_rate(115200));
- // A blocking reader for this port that
- // will time out a read after 500 milliseconds.
- blocking_reader reader(port, 500);
- char c;
- std::string rsp;
- // read from the serial port until we get a
- // \n or until a read times-out (500ms)
- while (reader.read_char(c) && c != '\n') {
- rsp += c;
- }
- if (c != '\n') {
- // it must have timed out.
- throw std::exception("Read timed out!");
- }
- return rsp;
- }
- int main()
- {
- char character;
- std::cout << "Waiting for response..." << std::endl;
- character = read_response();
- std::cout << "Character is: " << character << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement