How does the Arduino handle serial buffer overflow? Does it throw away the newest incoming data or the oldest? How many bytes can the buffer hold?
migrated from stackoverflow.com May 27 '14 at 15:25This question came from our site for professional and enthusiast programmers. |
|||
|
For hardware serial ports you can see in HardwareSerial.cpp that the buffer size varies depending on the amount of RAM available on the particular AVR:
For a software serial port in SoftwareSerial.h the receiver buffer size Ideally it would be best to ensure the buffer always gets emptied in a prompt manner to avoid the buffer filling. Maybe take a look at timers and implementing a simple state machine if your problem is related to other code blocking the main loop. |
|||||
|
ReceivingYou can see from the source of HardwareSerial that if an incoming byte finds the ring buffer full it is discarded:
Yes it will be discarded. There is no software or hardware flow control, unless you implement your own. However with a 64-byte buffer, and receiving data at (say) 9600 baud, you get one byte every 1.04 ms, and thus it takes 66.6 ms to fill up the buffer. On a 16 MHz processor you should be able to check the buffer often enough that it doesn't fill up. All you really have to do is move the data from the HardwareSerial buffer to your own, if you don't want to process it right now. You can see from the SendingData that you write is placed in a same-sized buffer (16 or 64 bytes). In the case of sending if the buffer fills up the code "blocks" waiting for an interrupt to send the next byte out the serial port. If interrupts are turned off this will never happen, thus you do not do Serial prints inside an Interrupt Service Routine. |
|||
|