0

Serial is causing me a lot of pain, and I think there are certain basic principles that I simple have not understood. I hope that by understanding how to test/demonstrate the buffer size, I will come one big step closer to understanding how serial works.

I suspect it is only possible to demonstrate this by writing a piece of software for "the other end"; the pc. I do understand Python, and to some extent how to work with serial on Python, so if possible, please write the pc code in Python.

If you supply an example (piece of code), please explain how the example works.

  • What example are you talking about? – Gerben Mar 7 '16 at 19:00
  • Do you mean what causes me problems? A lot of stuff does :) Basically my lack of understanding of how the buffer behaves is what causes me problems. I think it must be ok on SE to ask a question like this, analogues to stackoverflow.com/questions/12791864/…. Please elaborate if I misunderstood your comment. – Mads Skjern Mar 7 '16 at 19:04
  • In your last line you ask how "the example" works. Do you mean the Arduino serial example sketch? – Gerben Mar 7 '16 at 20:38
  • Now I understand. I have edited my answer to make clear what I meant. Thx. – Mads Skjern Mar 7 '16 at 21:08
  • On the Arduinos with 1000 bytes of RAM or more, the hardware serial input and output buffers are both 64 bytes. For devices with less RAM they are 16 bytes. This information is in the file HardwareSerial.h – Nick Gammon Mar 7 '16 at 21:17
1

You can think of the incoming serial buffer as a bucket of water under a dripping tap. Next to the bucket is a cup.

Water arrives through the tap and drips into the bucket. When you want to get some water, if there is enough water in the bucket, you dip the cup in and remove a cupful of water. If there isn't enough water you don't get anything in your cup.

If you don't dip the cup in for a while too much water will dribble into the bucket and it will overflow all over the floor.

It's exactly the same with serial data. It arrives through the UART hardware and is placed into the "bucket" by the UART receive interrupt. If you don't dip your cup in fast enough (Serial.read()) the bucket will overflow and any new data that arrives will spill onto the metaphorical floor.

To test this out you can just send more data to the Arduino than will fit into the serial buffer (typically 64 bytes) without reading any. Then after you have finished flooding the buffer you can read what is actually in the buffer and send it back again (maybe formatted nicely) so you can see what has actually ended up in the buffer and what hasn't.

One quick and dirty test would be to send the raw bytes 0 to 255 to the Arduino, then after a delay read them back and display the DEC values for them (Serial.print(Serial.read(), DEC))) to see exactly which bytes arrived and which didn't (hint: you should see the numbers 0 to 63 if the buffer is 64 bytes big).


For the transmit buffer it's slightly different. You can think of it more as a bucket with a small hole in the bottom.

You pour data (water) in the top at whatever speed you want, and it trickles out of the bottom at a pre-set rate (the baud rate - the size of the hole). You don't like wet messes though, so if the bucket is full you wait a while before putting more water into it. Just like with serial - if the transmit buffer is full any attempts to add to it will block until data has been sent out by the UART transmit interrupt.

  • How about a library like ArduinoJson, with the prettyPrintTo(Serial) command, how does it make sure that it does not write too fast to the serial? – Mads Skjern Mar 7 '16 at 21:06
  • It doesn't. It just writes to the serial object when the serial object allows it to. – Majenko Mar 7 '16 at 21:08
  • What do you mean "allows it to"? Does ArduinoJson in some way query if it is allowed to? – Mads Skjern Mar 7 '16 at 21:09
  • The key word in my answer is the one in italics: blocking. That is it stops and waits until there is room. – Majenko Mar 7 '16 at 21:10
  • Ok, so it is simply not possible to spill over when Arduino is writing. It just blocks if the buffer is full. I see it clearly in your answer now. Thanks. – Mads Skjern Mar 7 '16 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.