0

How does the Serial.print() transmit the data? Can you please explain this with respect to TX buffers?

Also, What would happen if i try to output the data that is greater than 64 bytes? How is this transmitted?

And please explain the difference with Serial.write()

Thanks!

asked Jan 15, 2018 at 18:37
7
  • is it your home work from school? Commented Jan 15, 2018 at 18:41
  • No how can that be a homework I was just just curious about serial communications. AND BTW I DONT DO homeworks Commented Jan 15, 2018 at 18:42
  • @Juraj i was reading this arduino.stackexchange.com/a/14405/41798 and was curious Commented Jan 15, 2018 at 18:47
  • you could look into the source code. class Print has all the print functions implemented using write function. HardwareSerial only implements write byte function, which adds a byte into TX buffer Commented Jan 15, 2018 at 18:47
  • Print is base class of the hierarchy Print <- Stream <- HardwareSerial Commented Jan 15, 2018 at 18:55

1 Answer 1

2

Serial.print() feeds the data into the TX buffer one character at a time (through the Print class's inherited .write() function which HardwareSerial implements). If there isn't enough room in the TX buffer for the next character it waits for there to be room available.

Characters are removed one at a time from the TX buffer and sent through the physical UART by the UART's TX interrupt.

To put it in more detail:

  • When you call Serial.print() it's actually calling Print.print(), because Print is a parent class of the HardwareSerial class.
  • Print.print() repeatedly calls Print.write() for each character to print.
  • Print.write() is a pure virtual function (which means it's only got the definition in the Print class and child classes must implement it).
  • HardwareSerial.write() implements the Print.write() function which takes the character and (with possible blocking while waiting for space) adds it to the TX buffer.
  • Serial is an instance of the HardwareSerial class.
answered Jan 15, 2018 at 18:47
3
  • Serial.write() adds a byte to TX buffer Commented Jan 15, 2018 at 18:52
  • @Juraj Serial.print() is actually Print.print() which calls Print.write() which is a pure virtual function implemented by the HardwareSerial child class. Commented Jan 15, 2018 at 18:54
  • I didn't say something else Commented Jan 15, 2018 at 18:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.