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!

  • is it your home work from school? – Juraj Jan 15 '18 at 18:41
  • No how can that be a homework I was just just curious about serial communications. AND BTW I DONT DO homeworks – rimalonfire Jan 15 '18 at 18:42
  • @Juraj i was reading this arduino.stackexchange.com/a/14405/41798 and was curious – rimalonfire Jan 15 '18 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 Jan 15 '18 at 18:47
  • Print is base class of the hierarchy Print <- Stream <- HardwareSerial – Juraj Jan 15 '18 at 18:55
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.
  • Serial.write() adds a byte to TX buffer – Juraj Jan 15 '18 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 Jan 15 '18 at 18:54
  • I didn't say something else – Juraj Jan 15 '18 at 18:59
-3

Its simple, add one byte wait till the byte is transmitted, And add another byte till the input data isn't exhausted.

There is rx bufffer not tx buffer of 64B.

  • 2
    There is a TX buffer. It's called unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE]; and it's defined in HardwareSerial.h. – Majenko Jan 15 '18 at 18:49

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.