I am trying to read serial data coming from arduino, but when I run my program it only reads all the data in the buffer, the data that was actually send before the program started. And then i terminates with the following error:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
what(): read: End of file
Aborted (core dumped)
I also do not even want the data from before the program was executed. Here is my c++ code:
#include <iostream>
#include <stdint.h>
#include <boost/asio.hpp>
#include <boost/asio/serial_port.hpp>
using namespace boost;
int main() {
asio::io_service io;
asio::serial_port port(io);
port.open("/dev/ttyUSB0");
port.set_option(asio::serial_port_base::baud_rate(115200));
char d;
while ( true ) {
asio::read(port, asio::buffer(&d, 1));
std::cout << d << std::endl;
}
port.close();
}
As far as I know the read function should be blocking, so it waits for the next input, right? Then how can it reach the end of "file"?