Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By dpetican
#75453 I need to wait for serial data then parse that data into a response to a request. The docs say not to use delay() in call backs. Is using a while loop to read serial equivalent to blocking with delay() or something different? In principle all callbacks block for a finite amount of time. How long is too long for TIMEOUT? Alternative strategies? I think I'm asking how to synchronize two asynchronous processes. Its okay that the web server cannot respond to another request while waiting on the serial. Its a custom REST interface and the Android app will only send one command at a time and wait for the response and timeout if none received before sending another. Or alternatively I could return a 404 error if rxBytes is zero.

Code: Select allunsigned long startTime = millis();
int rxBytes = 0;
while((millis() - startTime) < TIMEOUT) {
  rxBytes = Serial.readBytesUntil('\r', rxBuffer, RX_BUFF_SZ - 1);
  if (rxBytes != 0)
    break;
}
rxBuffer[rxBytes] = '\0';