if Arduino connected pc and serial comminication avaiable, i m catch this.
i try if(Serial) not work in "void loop".
I want to do exactly that ;
void loop() {
String DateTime;
DateTime=rtc.getTimeStr();
if (serial port connected)
{
while (sdFile.available()) { Serial.println(sdFile.read() ; } // before if db file exists send db file lines to pc
Serial.println(DateTime); // after send actual DateTime data.
}
else // not connected
{
// sd card write data
sdFile.write(Datetime);
}
}
Shortly if serial2pc connected, send data to pc else save data to sd. in main loop.
How to ?
Have you done a search? It is not possible to tell if there is anything on the other end of the serial port without the cooperation of the program on the other end of the connection.
Your PC might be doing other things so it might take a while before it responds.
More importantly, there is no guarantee that the reply from the PC contains all three bytes; it might well send two bytes followed by one (and readString might time out in between).
It might be better to use a character array instead of String. See the thread Serial Input Basics for ideas. You can implement your own timeout.