Recently I knew, that Arduino restarts any time somebody accesses it's serial port. I was trying to use this feature so that for each request Arduino restarts and give answer on request. Unfortunately, I found this is impossible with shell script, because shell script can't keep port open all time I send requests and wait responses: Arduino restarts on each port touch.

So, I wrote simple C++ program:

#include <iostream>
#include <fstream>

using namespace std;

void help() {
    cout << "bmp180dims usage:" << endl <<
            "bmp180dims port command" << endl;

int main(int argc, char* argv[]) {

    if( argc != 3 ) {
        return 0;

    fstream port;
    string answer;

    port.open(argv[1], std::fstream::in | std::fstream::out);

    port << argv[2] << endl;

    port >> answer;

    cout << answer << endl;


    return 0;


Since this program has explicit open and close statements, I was thinking, this program will wait for Arduino answer, because of stream reading functions. But I failed again: this program returns nothing as if it is not waiting.


Apparently, the only way to communicate with Arduino, is to have daemon program, which always listen it's port. Is this really so?

Arduino-side code is here: How can program/arduino restarted by IDE?

1 Answer 1


Again, you're not waiting for the bootloader to complete before sending your text:

port.open(argv[1], std::fstream::in | std::fstream::out);
// Board resets and enters bootloader

port << argv[2] << endl;
// You send argv[2] to the bootloader

port >> answer;
// The bootloader doesn't understand what you sent so doesn't reply with anything

You must wait for the bootloader to complete before sending ANYTHING to the Arduino.

The two ways you can do that are:

  1. Insert a sleep(3); immediately after opening the port to give the bootloader a chance to time out and start running your sketch, or
  2. Provide an initial "I Am Ready" message from your sketch that your C++ program waits for before sending the text you want to send to elicit an answer.
  • 1
    I understand that I am not waiting for send, but I am waiting for receive. Why port >> answer not waiting if bootloader sends nothing? Shouldn't port >> answer wait for space or newline?
    – Dims
    Oct 16, 2015 at 10:15
  • It can only wait for something if you are sending something. You aren't sending anything because you aren't receiving anything, because what you send isn't getting through to your sketch.
    – Majenko
    Oct 16, 2015 at 10:22
  • How this is implemented? How >> can know that I didn't send anything? How it can distinguish silence from delay between request and response and initial silence? Suppose I implement your suggestion on "I Am Ready" how can I wait for "I Am Ready" if >> will just drop waiting when it wants?
    – Dims
    Oct 16, 2015 at 10:38
  • P.S. Note that my questions are for understand what is happening
    – Dims
    Oct 16, 2015 at 10:39
  • If the port is opened in non-blocking mode then it will never wait for anything, ever. I don't use fstream so don't know how it opens the port - if it's non blocking then you cannot use >> to read from it. You should be using low level IO for talking to a serial port, not file IO. Use open(), read() and write(), with a good smattering of select() to see if there is actually anything to read from the port.
    – Majenko
    Oct 16, 2015 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.