I'm writing a C++ program for communicating with a Arduino over a serial port using boost::asio. After establishing the connection the Arduino resets itself. However the input buffer of the C++ program still contains old data sent from the Arduino. Since I have no use for this data I'd like clear the input buffer. How can I accomplish that using boost::asio?
My code currently looks like this:
#include <boost/asio.hpp>
#include <vector>
#include <iostream>
using namespace std;
using namespace boost::asio;
int main()
{
io_service io_service;
serial_port port(io_service, "/dev/ttyACM0");
port.set_option(serial_port_base::baud_rate(9600));
vector<char> buf(1);
read(port, buffer(buf));
cout << (int) buf[0] << endl;
return 0;
}
The code should pause at the read command and wait for the arduino to send serial data. However there is old data causing the code to immediately continue.
The Arduino automatically resets when i open the serial port
, why does it do that? Have you designed it this way? - Fantastic Mr Fox