diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 860c2c0f..be80ca27 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -131,6 +131,24 @@ int HardwareSerial::read(void) return -1; } +// read characters into buffer +// terminates if size characters have been read, or no further are pending +// returns the number of characters placed in the buffer +// the buffer is NOT null terminated. +size_t HardwareSerial::read(uint8_t *buffer, size_t size) +{ + size_t avail = available(); + if (size < avail) { + avail = size; + } + size_t count = 0; + while(count < avail) { + *buffer++ = uartRead(_uart); + count++; + } + return count; +} + void HardwareSerial::flush(void) { uartFlush(_uart); diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index 7d9f26d2..82047674 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -62,11 +62,19 @@ public: int availableForWrite(void); int peek(void); int read(void); + size_t read(uint8_t *buffer, size_t size); + inline size_t read(char * buffer, size_t size) + { + return read((uint8_t*) buffer, size); + } void flush(void); void flush( bool txOnly); size_t write(uint8_t); size_t write(const uint8_t *buffer, size_t size); - + inline size_t write(const char * buffer, size_t size) + { + return write((uint8_t*) buffer, size); + } inline size_t write(const char * s) { return write((uint8_t*) s, strlen(s));