From Arduino code, I have a question for SoftwareSerial Class
First: when I use more example for Esp8266 those functions use while loop to read response string from Esp when we send AT_Command to ESP.
getResponse(const uint32_t wait) {
String tmpData = "";
long int time = millis();
while ((time + wait) > millis()) {
while (esp8266->available() > 0) {
char c = esp8266->read();
if (c == '0円') continue;
tmpData += c;
}
}
tmpData.trim();
return tmpData;
}
I know that SoftwareSerial Class have a public method is readString() then I'm tried to use that and it return same result returned by while loop read:
getResponse(const uint32_t wait) {
String tmpData = "";
tmpData = esp8266->readString();
return tmpData;
}
Thanks for any. I'm working on improving my Eng. Regards
1 Answer 1
Your while
loop reads all the characters up until it reaches character 0 (it also has a timeout, although the timeout has been coded wrong since it can't deal with clock rollover). The .readString()
method reads characters until no characters arrive within a timeout period (1s if memory serves me correctly).
Your while
loop is the more reliable method since it uses a specific delimiter to identify blocks of data (strings) rather than assuming that:
- The string will all arrive with no delays greater than the timeout
- The strings will be sent with a gap of at least the timeout length between them.
Personally I would never recommend using any of the "helpful" functions provided for reading from a Stream
object other than the basic .read()
function.