Serial
peek()
Returns the next byte (character) of incoming serial data without removing it from the internal serial buffer. That is, successive calls to peek() will return the same character, as will the next call to read().
// PROTOTYPE
virtual int peek(void);
// SYNTAX
Serial.peek();
Serial1.peek();
peek() returns the first byte of incoming serial data available (or -1 if no data is available) - int
Since 6.5.0:
On hardware UART ports (Serial1, etc.) and USB serial (Serial, USBSerial1), you can also peek at more than one byte at a time:
// PROTOTYPE
int peek(char *buffer, size_t size);
// SYNTAX
char buf[16];
int bytesPeeked = Serial1.peek(buf, sizeof(buf));
Copies up to size bytes of the incoming data currently in the internal serial buffer into buffer, without removing them, so a subsequent read(), readBytes(), or peek() call will see the same data. Unlike readBytes(), this call does not wait for more data to arrive: it returns immediately with however many bytes (up to size) are already buffered. Use available() beforehand if you need to know how much data is ready. Returns the number of bytes copied into buffer, or a negative value on error.