Let's say I have the following 8 bytes in the Serial input buffer:
0x7A 0x7B 0x7C 0x7D 0x7E 0x7F 0x80 0x81
Let's examine the results of different read operations, [u]each operated on the same buffer contents[/u] as shown above.
int bytesRead = Serial.readBytesUntil(0x7E, readBytes, 8); // bytesRead == 4. First 4 bytes are read into readBytes (0x7A, 0x7B, 0x7C, 0x7D)
int bytesRead = Serial.readBytesUntil(0x7F, readBytes, 8); // bytesRead == 5. First 5 bytes are read into readBytes (0x7A, 0x7B, 0x7C, 0x7D, 0x7E)
int bytesRead = Serial.readBytesUntil(0x80, readBytes, 8); // bytesRead == 8. Yes 8, and not 6 as one might expect! All the bytes are read (0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81)
Why? It appears that the function readBytesUntil() does not recognize a termination character if b7 is set in that value. This is unfortunate. For example, imagine a protocol where packets are terminated with a value >= 0x80. The function readBytesUntil() can not be used with such a protocol, forcing the programmer to do a great deal of extra programming.
Does anyone else see this as an issue that needs to be addressed?