Stream class

readBytes()

readBytes() read characters from a stream into a buffer. The function terminates if the determined length has been read, or it times out.

// PROTOTYPE
virtual size_t readBytes( char *buffer, size_t length);
// SYNTAX
stream.readBytes(buffer, length);

Parameters:

  • stream : an instance of a class that inherits from Stream
  • buffer : pointer to the buffer to store the bytes in (char *)
  • length : the number of bytes to read (size_t)

Returns: returns the number of characters placed in the buffer (0 means no valid data found)

Since 6.5.0:

On hardware UART ports (Serial1, etc.) and USB serial (Serial, USBSerial1), readBytes() transfers data directly from the underlying buffer instead of calling read() one byte at a time, which is significantly faster for larger buffers. It still honors setTimeout(): it returns once length bytes have been read or the timeout elapses, whichever comes first, so the returned count may be less than length.