Abstract
The Serial.read()/HardwareSerial::read() reads data from receive buffer.
Source Code
The HardwareSerial::read() is defined in hardware/arduino/avr/cores/arduino/HardwareSerial.cpp as below.
|
|
No inputs, output is int.
|
|
If the head and tail of the buffer is same, there is no data in the buffer, returns -1.
|
|
If the head and tail of the buffer is not same, reads the oldest data which is stored in buffer[tail], advance _rx_buffer_tail one byte, then return the data.
|
|
Because the receive buffer is a ring buffer, if the tail gets to the last of the buffer, it should go back to the head of the buffer. To do that, add 1 to tail then get reminder of division by buffer size(SERIAL_RX_BUFFER_SIZE).
To put the received data to the buffer, an interrupt handler named USART_RX_vect is used.
Version
Arduino AVR Boards 1.8.6
Last Update
August 25, 2019