Do-loop behaving weird icw serial i/o

hi all,

we have a really weird problem… we have some serial/usb communication that works fine but as soon as we put it in a do-loop it starts to act different. the corresponding while-loop behaves just fine. any ideas?

the serial communication is nothing special, just write out some characters and read back answers. the first i/o is something like (we do this actually twice in the loop)):

Serial.println("TELLME");
do {
    Particle.process();
    if (Serial.available()) {
          c=Serial.read();
          if((c>31)&&(i<32)) {
                 response[i++]=c;
          }
    }
} while(c!=13);

do-loop (corrupts serial io):

do {

      ... serial i/o ...

} while(strcmp(response,"DONE")!=0);

while-loop (works fine):

while (1) {

      ... serial i/o ...

    if(strcmp(response,"DONE")==0) {
        break;
    }
}

hard to determine without all of the code., where do you null terminate the C string?

how is i reset?

can you post a small complete example of the code that demonstrates your issue?

hi bulldoglowell,

complete code below… again, it works when the do-loop is replaced with a while-forever-loop and break. it is driving me nuts…

(the idea is that we read network credentials via usb. the server sees “TELLME” and respons with the SSID and password, each on a new line. we then echo what we got till the server sees it is correct and proclaims “EMLLET”)

with the do-loop version we get SSID=null (spurious CR). with the same server and the while-loop it always works just fine…

thanks
frank

void setup() {

    Serial.begin(9600);

    char ssid[33];
    char pwd[33];
    char response[33];

    int c;
    int i=0;
    int ok=0;

    // commenting "do" and uncommenting "while" makes it work nicely
    do {
    // while(1) {

      Serial.println("TELLME");

        c=0;
        i=0; 
        do {
            Particle.process();
            if (Serial.available()) {
                c=Serial.read();
                if((c>31)&&(i<32)) {
                    ssid[i++]=c;
                }
            }
        } while(c!=13);
        ssid[i]=0;

        c=0;
        i=0;
        do {
            Particle.process();
            if (Serial.available()) {
                c=Serial.read();
                if((c>31)&&(i<32)) {
                    pwd[i++]=c;
                }
            }
        } while(c!=13);
        pwd[i]=0;

        Serial.print("DONE ");
        Serial.print(ssid);
        Serial.print(" ");
        Serial.println(pwd);

        c=0;
        i=0;
        do {
            Particle.process();
            if (Serial.available()) {
                c=Serial.read();
                if((c>31)&&(i<32)) {
                    response[i++]=c;
                }
            }
        } while(c!=13);
        response[i]=0;

        // just to debug
        Particle.publish("t_ssid", ssid);
        Particle.publish("t_pwd", pwd);
        Particle.publish("response", response);

        // un commenting this "if" and commenting the while will make it work fine
        // if(strcmp(response,"EMLLET")==0) {
        //    break;
        //}
     } while(strcmp(response,"EMLLET")!=0);

    delay(1000);
    Particle.publish("ssid", ssid);
    delay(1000);
    Particle.publish("pwd", pwd);
}

void loop() {

}

your code doesn’t compile

:expressionless:

bracket-confusion. now it does…