From Wiring

Jump to: navigation, search
This page is intended as a reference for core developers only, and the content is only changed after a consensus has been reached through discussion of any such changes. Please use the Discussion page to talk about any changes or issues you have with the current implementation.

The HardwareSerial class is one of the cornerstone classes of the Wiring Framework. Most microcontrollers will have at least one hardware USART/UART. The microcontrollers that don't have a hardware USART/UART should implement some sort of software serial class to accommodate the serial data interface that is very often used in user sketches.


Class Definition

Here is the basic class definition for the HardwareSerial class. This is only a guideline, and you can implement the methods in the class definition, if need be.

class HardwareSerial : public Stream
  friend void Serial_RX_vect();  // These are examples of how to interface allow
  friend void Serial_TX_vect();  // interrupt vectors to interface with the class.
    FIFO<uint8_t,RX_BUFFER_SIZE> rxfifo;
    FIFO<uint8_t,TX_BUFFER_SIZE> txfifo;
    // ~HardwareSerial();  // Destructor if your GCC implementation requires
    void begin(const uint32_t baud = 9600,
               const uint8_t data_bits = 8,
               const uint8_t stop_bits = 1,
               const uint8_t parity = 0);
    void end(void);
    int available(void);
    void flush(void);
    int peek(void);
    int read(void);
    void write(uint8_t);


  • rxfifo and txfifo utilize the Wiring FIFO Common Library for the input and output buffers, respectively.


  • HardwareSerial() - Contructor
  • ~HardwareSerial() - Destructor
  • begin() - Initializer
  • end() - Terminator
  • available() - A pure virtual method implementation from the Stream abstract base class, Stream.available(). Returns the number of chars available in the receive buffer, if any.
  • flush() - Stream.flush(). Flushes (i.e. clears) the data from the receive buffer.
  • peek() - Stream.peek(). Returns the first char available in the receive buffer, without removing it, if available.
  • read() - Stream.read(). Returns and removes the first char available in the receive buffer, if available.
  • write() - Stream.write(). Puts the given char into the transmit buffer.

Buffered I/O

Buffered I/O is recommended, if possible, although, not required. read(), and write() can block, if necessary.

Personal tools