How to wait Arduino ready with ANY method? 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 &lt;iostream&gt;
#include &lt;fstream&gt;

using namespace std;

void help() {
    cout &lt;&lt; "bmp180dims usage:" &lt;&lt; endl &lt;&lt;
            "bmp180dims port command" &lt;&lt; endl;
}

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

    if( argc != 3 ) {
        help();
        return 0;
    }

    fstream port;
    string answer;

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

    port &lt;&lt; argv[2] &lt;&lt; endl;

    port &gt;&gt; answer;

    cout &lt;&lt; answer &lt;&lt; endl;

    port.close();

    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.

Why? 

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?
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 &lt;&lt; argv[2] &lt;&lt; endl;
// You send argv[2] to the bootloader

port &gt;&gt; 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:


Insert a sleep(3); immediately after opening the port to give the bootloader a chance to time out and start running your sketch, or
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.