Go Down

Topic: Serial.readBytesUntil badly documented and/or implemented (Read 155 times) previous topic - next topic

jomag

Hi

I find the Serial.readBytesUntil documentation a bit lacking. This is what it says at the time of this writing:

Quote
Serial.readBytesUntil() reads characters from the serial buffer into an array. The function terminates if the terminator character is detected, the determined length has been read, or it times out (see Serial.setTimeout()).

Serial.readBytesUntil() returns the number of characters read into the buffer. A 0 means no valid data was found.

Serial.readBytesUntil() inherits from the Stream utility class.
First, it does not tell if the terminating character is included in the result or not.

Secondly, it says that zero means no valid data, but it should more correctly read "no data". For example, an empty string input may very well be "valid".

Third: if I've read the source correctly the terminating character is not included in the response, which means there is no way to determine if the method timed out or the terminating character was read.

I guess the implementation is hard to change at this point, but at least the documentation should be updated.


PaulS

Quote
Third: if I've read the source correctly the terminating character is not included in the response, which means there is no way to determine if the method timed out or the terminating character was read.
You can use millis() to record when you call the function. You can use millis() to record when the function ends. If the function took an amount of time equal to the timeout window, the terminating character did not arrive.

Better would be to stop using that crutch. Read every byte as soon as it arrives. Put the character in a char array. No need to guess whether the terminating character arrived. You'll KNOW whether it did, or did not. It is easy to record when the first character arrives, and know how long it has been since that happened, so you can determine, in YOUR code, whether you've waited long enough for all the data.
The art of getting good answers lies in asking good questions.

Robin2

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up