2

Serial is documented on the Reference pages, but seems to receive special treatment (no need to include header), and there does not seem to be any file with the Source code.

Stream is the base class for Serial and inherits from the Print class.

There is source for Stream.h

LiquidCrystal_I2C also inherits from the Print class, and I was planning to write code which outputs to either LiquidCrystal_I2C or Serial; there are many options, but I was looking for Serial to explore its inner workings.

5
  • The HardwareSerial.cpp file is located in ...hardware/arduino/avr/cores/arduino, if that is what you are looking for Commented Jun 17, 2017 at 6:03
  • @Fauzan There is no Serial class in the location above, which is where I found the other source. Commented Jun 17, 2017 at 6:44
  • Serial is an instance of class HardwareSerial. Commented Jun 17, 2017 at 7:50
  • @Majenko Where is it defined? I cannot even find the symbol Serial anywhere? Commented Jun 17, 2017 at 8:36
  • 1
    github.com/arduino/Arduino/blob/master/hardware/arduino/avr/… is where Serial is defined. On AVR MCUs with built-in USB it is defined here github.com/arduino/Arduino/blob/master/hardware/arduino/avr/…. Commented Jun 17, 2017 at 8:57

1 Answer 1

5

Serial is an instance of the HardwareSerial class. It is defined in HardwareSerial0.cpp:

#if defined(UBRRH) && defined(UBRRL)
  HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
#else
  HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
#endif

HardwareSerial is a child of Stream as defined in HardwareSerial.h:

class HardwareSerial : public Stream
{
    ....
};

Stream is a child of Print as defined in Stream.h:

class Stream : public Print
{
    ....
};

Print is a top-level class with no inheritance.

Serial is not a class. It is an instance of a class. That is, it's an object that has been constructed from the class definition. Anything you access member functions of using a . is an instance of a class (aka an object) not a class itself. Anything you access member functions with :: is a class using static member functions.

Examples:

Serial.println(); // Call the println() function in the Serial object
Wire.beginTransmission(0x34); // Call the beginTransmission function in the Wire object
Foo::bar(); // Call the static function bar() in the Foo class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.