But be aware that this is a "blocking method", so as long as your program is in the while loop it can't do anything else. And if no data arrives, you have an infinite loop.
This is pretty much on the same track as my initial attempt... If I would add some kind of a timeout to that it just might do the job.
The blocking in this instance does not matter since all the Arduino does is just sit and wait for response. If timeout does occur it'll raise an error flag.
I think MikMo meant to have double equals in the while loop.
here is a fragment that you can expand to get a timeout:
unsigned int timeout = 0;
void loop() {
while ( serial.available()==0) {
if( ++timeout > 10000){ // set this to your timeout value in milliseconds
// your error handling code here
break;
}
}
timeout = 0; // got a char so reset timeout
// code hear to read data
}
Yes i ment ==, and the timeout is a really good way to avoid the infinite loop, especially if the program does not assume that there was data, but actually handles the situation.
I come from a VB.net world i make the == error on a daily basis when programming i C