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– Juraj ♦2018年01月15日 18:41:30 +00:00Commented 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 homeworksrooni– rooni2018年01月15日 18:42:50 +00:00Commented Jan 15, 2018 at 18:42
-
@Juraj i was reading this arduino.stackexchange.com/a/14405/41798 and was curiousrooni– rooni2018年01月15日 18:47:02 +00:00Commented 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 bufferJuraj– Juraj ♦2018年01月15日 18:47:47 +00:00Commented Jan 15, 2018 at 18:47
-
Print is base class of the hierarchy Print <- Stream <- HardwareSerialJuraj– Juraj ♦2018年01月15日 18:55:02 +00:00Commented Jan 15, 2018 at 18:55
1 Answer 1
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 callingPrint.print()
, becausePrint
is a parent class of theHardwareSerial
class. Print.print()
repeatedly callsPrint.write()
for each character to print.Print.write()
is a pure virtual function (which means it's only got the definition in thePrint
class and child classes must implement it).HardwareSerial.write()
implements thePrint.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 theHardwareSerial
class.
-
Serial.write() adds a byte to TX buffer2018年01月15日 18:52:51 +00:00Commented 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– Majenko2018年01月15日 18:54:43 +00:00Commented Jan 15, 2018 at 18:54
-
I didn't say something else2018年01月15日 18:59:39 +00:00Commented Jan 15, 2018 at 18:59