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!

7
  • is it your home work from school?
    – Juraj
    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
    – rooni
    Commented Jan 15, 2018 at 18:42
  • @Juraj i was reading this arduino.stackexchange.com/a/14405/41798 and was curious
    – rooni
    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
    – Juraj
    Commented Jan 15, 2018 at 18:47
  • Print is base class of the hierarchy Print <- Stream <- HardwareSerial
    – Juraj
    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.
3
  • Serial.write() adds a byte to TX buffer
    – Juraj
    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.
    – Majenko
    Commented Jan 15, 2018 at 18:54
  • I didn't say something else
    – Juraj
    Commented Jan 15, 2018 at 18:59

Your Answer

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

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