Serial connection 'wait for response'

Hi all!

Does anyone know how to do a serial communications with a 'wait for' mechanism.

I am developing a small program to interface with a mobile phone and what would be really handy is something like

send AT to phone
wait for OK
do something...

send another AT command
wait for response and use it as a variable value
do something...

All I could come up with were quite cumbersome and really did not do the job properly.

Any help is greatly appreciated

One way to do that is

send your data

while ( serial.available()=0) {}

read you data

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.

Thanks!

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 :slight_smile: