[ESP32] use of HardwareSerial Library

brief introduction

HardwareSerial library is a serial driver library written in c + + and applied to esp32 Arduino application environment. After installing the Arduino development environment of ESP32, we have integrated this serial operation library, which can be directly referenced in practical application.

Note: ESP32 has three UART ports in total, of which UART1 is used for Flash read / write. When using serial port 1, we need to pay attention to that serial port 1 needs to be mapped to other gpios. I tested GPIO12 and GPIO13, which can run perfectly when using serial port 1

Serial port nameArduino nameTXRX
UART0Serialpin1pin3
UART1Serial1pin10pin9
UART2Serial2pin17pin16

Effect demonstration

demo program, transmit the data received by serial port 0 to serial port 1, and then transmit the data received by serial port 1 to serial port 0

#include <Arduino.h>

#define SERIAL_BAUD 115200

HardwareSerial cardSerial(1);//Serial port 1

int distance = 0;

void setup() {
  //Initialize serial port 0
  Serial.begin(SERIAL_BAUD);
  //Initialize serial port 1
  cardSerial.begin(SERIAL_BAUD,SERIAL_8N1,12,13);
  //Initialize serial port 2
  Serial2.begin(SERIAL_BAUD);
}

void loop() {
   while (cardSerial.available() > 0) {
        uint8_t byteFromSerial = cardSerial.read();
        Serial.write(byteFromSerial);
    }
    while (Serial.available() > 0) {
        uint8_t byteFromSerial1 = Serial.read();
        cardSerial.write(byteFromSerial1);
    }
}

In the figure above, the use of serial port 1 is realized through the HardwareSerial library. See the following for details.

Detailed description of API functions

To use the HardwareSerial library, you need to declare an object first, such as HardwareSerial mySerial1(1);;
The HardwareSerial class receives an input parameter (0, 1, 2) when declaring an object, representing serial, Serial1, and Serial2 respectively;
After the object is declared, it can be used according to the general serial port method. The method description is as follows:

Initialize serial port

void HardwareSerial::begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL);

Enable the serial port. The parameters are as follows:

  • Baud: serial port baud rate. If the value is written to 0, it will enter the automatic baud rate detection program;
  • config: serial port parameter. The default is SERIAL_8N1 is 8 data bits, no check and 1 stop bit;
  • rxPin: receiving pin pin number;
  • txPin: send pin pin number;
  • invert: flip the logic level. The default high level and low level of the serial port are 1 and 0;
  • timeout_ms: automatically detect the baud rate timeout time. If the baud rate is not obtained after this time, the serial port will not be enabled;

Close the serial port

void HardwareSerial::end();

Disable serial port to release resources;

Reset baud rate

void updateBaudRate(unsigned long baud);

Set receive buffer size

size_t HardwareSerial::setRxBufferSize(size_t new_size)

Set the receiving cache size (256 bytes by default);
ESP32 has 128 byte hardware RX FIFO by default, which will be transferred to the above receiving cache after RX FIFO receives data;

Returns the received cache data length

int HardwareSerial::available(void);

Returns the writable length of the send buffer

int HardwareSerial::availableForWrite(void);

ESP32 has 128 bytes of hardware TX FIFO by default. This method returns the number of free bytes of TX FIFO;

Read one byte of data from the receive buffer (1)

int HardwareSerial::peek(void);

Returns the first byte data in the receive cache, but does not delete it;

Read one byte of data from the receive buffer (2)

int HardwareSerial::read(void);

Return the first byte data in the receiving cache, and the read data will be cleared from the receiving cache;

Wait for the serial port to send and receive

void HardwareSerial::flush();

send data

size_t HardwareSerial::write(uint8_t c)
size_t HardwareSerial::write(const uint8_t *buffer, size_t size)

Write data to TX FIFO, and the data in the transmit FIFO will be automatically output to TX port;
This method has many overloads, which can be used to send string, long integer and shaping;
If TX FIFO is full, the method will block;

Returns the baud rate of the current serial port

uint32_t  HardwareSerial::baudRate()

Log output settings

void HardwareSerial::setDebugOutput(bool en);

Set the serial port to print Debug information (the default is 0, and it will also be set to 0 after disabling);

In addition to the above methods, because it inherits from the Stream class, you can also use the methods of this class, such as size_t readBytes(char *buffer, size_t length); And size_t readBytes(uint8_t *buffer, size_t length), etc.

Keywords: Single-Chip Microcomputer IoT ESP32

Added by chronomantic on Fri, 04 Mar 2022 15:03:44 +0200