2

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"?

  • Hi, I'm having the same problem. Even worse, the *binary* that worked yesterday gives the same problem as you today. Possibly due to some timeout settings. – Atilla Filiz Aug 27 '17 at 16:16
  • So I ran the exact same code again today, with the exact same compilation process and now it magically works... – Rasmus Tollund Aug 28 '17 at 15:17

2 Answers2

5

This is what worked for me. You need to give your serial port a minimum character limit to have a blocking capability.

stty -F /dev/ttyUSB5 115200 line 0 min 1 time 0

EDIT: A better option seems to just set the port to raw mode.

stty -F /dev/ttyUSB0 raw
Atilla Filiz
  • 2,383
  • 8
  • 29
  • 47
0

So I have figured out that after running Arduino IDE as root, to allow serial communication, my programs stops working and I get the EOF error. Even after Arduino IDE has been closed it does not work, restarting my pc allows it to work again.

But Atilla Filiz answers solves this.