PL::Uart class

class Uart : public PL::HardwareInterface, public PL::Stream

UART class.

Public Functions

Uart(uart_port_t port, int rxBufferSize = minBufferSize, int txBufferSize = minBufferSize, int txPin = UART_PIN_NO_CHANGE, int rxPin = UART_PIN_NO_CHANGE, int rtsPin = UART_PIN_NO_CHANGE, int ctsPin = UART_PIN_NO_CHANGE)

Creates an UART.

Parameters:
  • port – port number

  • rxBufferSize – RX buffer size

  • txBufferSize – TX buffer size

  • txPin – TX pin

  • rxPin – RX pin

  • rtsPin – RTS pin

  • ctsPin – CTS pin

virtual esp_err_t Lock(TickType_t timeout = portMAX_DELAY) override

Locks the object.

Parameters:

timeout – timeout in FreeRTOS ticks

Returns:

error code

virtual esp_err_t Unlock() override

Unlocks the object.

Returns:

error code

virtual esp_err_t Initialize() override

Initializes the hardware interface.

Returns:

error code

virtual esp_err_t Enable() override

Enables the hardware interface.

Returns:

error code

virtual esp_err_t Disable() override

Disables the hardware interface.

Returns:

error code

esp_err_t EnableLoopback()

Enables the loopback.

Returns:

error code

esp_err_t DisableLoopback()

Disables the loopback.

Returns:

error code

virtual esp_err_t Read(void *dest, size_t size) override

Reads the data from the stream.

Parameters:
  • dest – destination (can be NULL)

  • size – number of bytes to read

Returns:

error code

virtual esp_err_t Write(const void *src, size_t size) override

Writes the data to the stream.

Parameters:
  • src – source

  • size – number of bytes to write

Returns:

error code

virtual bool IsEnabled() override

Checks if the hardware interface is enabled.

Returns:

true if the hardware interface is enabled

virtual size_t GetReadableSize() override

Gets the number of bytes that are available for reading from the stream.

Returns:

number of bytes

virtual TickType_t GetReadTimeout() override

Gets the read operation timeout.

Returns:

timeout in FreeRTOS ticks

virtual esp_err_t SetReadTimeout(TickType_t timeout) override

Sets the read operation timeout.

Parameters:

timeout – timeout in FreeRTOS ticks

Returns:

error code

uint32_t GetBaudRate()

Gets the baud rate.

Returns:

baud rate

virtual esp_err_t SetBaudRate(uint32_t baudRate)

Sets the baud rate.

Parameters:

baudRate – baud rate

Returns:

error code

uint16_t GetDataBits()

Gets the number of data bits.

Returns:

number of data bits

virtual esp_err_t SetDataBits(uint16_t dataBits)

Sets the number of data bits.

Parameters:

dataBits – number of data bits

Returns:

error code

UartParity GetParity()

Gets the parity.

Returns:

parity

virtual esp_err_t SetParity(UartParity parity)

Sets the parity.

Parameters:

parity – parity

Returns:

error code

UartStopBits GetStopBits()

Gets the number of stop bits.

Returns:

number of stop bits

virtual esp_err_t SetStopBits(UartStopBits stopBits)

Set the number of stop bits.

Parameters:

stopBits – number of stop bits

Returns:

error code

UartFlowControl GetFlowControl()

Gets the flow control type.

Returns:

flow control type

virtual esp_err_t SetFlowControl(UartFlowControl flowControl)

Sets the flow control type.

Parameters:

flowControl – flow control type

Returns:

error code

esp_err_t SetMode(uart_mode_t mode)

Sets the mode (UART/IRDA/RS485…)

Parameters:

mode – mode

Returns:

error code

std::string GetName()

Gets the hardware interface name.

Returns:

name

void SetName(const std::string &name)

Sets the hardware interface name.

Parameters:

name

esp_err_t Read(Buffer &dest, size_t offset, size_t size)

Reads the data from the stream to the buffer.

Parameters:
  • dest – destination buffer

  • offset – destination buffer offset

  • size – number of bytes to read

Returns:

error code

esp_err_t ReadUntil(void *dest, size_t maxSize, char termChar, size_t *size)

Reads the data from the stream up to the specified termination character.

Parameters:
  • dest – destination (can be NULL)

  • maxSize – maximum number of bytes to read

  • termChar – termination character

  • size – number of bytes read

Returns:

error code

esp_err_t ReadUntil(Buffer &dest, size_t offset, char termChar, size_t *size)

Reads the data from the stream to the buffer up to the specified termination character.

Parameters:
  • dest – destination buffer

  • offset – destination buffer offset

  • termChar – termination character

  • size – number of bytes read

Returns:

error code

esp_err_t ReadUntil(char termChar)

Reads the data from the stream up to the specified termination character.

Parameters:

termChar – termination character

Returns:

error code

esp_err_t Write(Buffer &src, size_t offset, size_t size)

Writes the data from the buffer to the stream.

Parameters:
  • src – source buffer

  • offset – source buffer offset

  • size – number of bytes to write

Returns:

error code

esp_err_t Write(const std::string &src)

Writes the string to the stream.

Parameters:

src – source string

Returns:

error code

esp_err_t FlushReadBuffer(TickType_t time = 0)

Discards the data in the read buffer until no data is received for the specified period of time.

Parameters:

time – time

Returns:

error code

Public Members

Event<HardwareInterface> enabledEvent

hardware interface enabled event

Event<HardwareInterface> disabledEvent

hardware interface disabled event

Public Static Attributes

static const std::string defaultName

Default hardware interface name.

static const int minBufferSize = SOC_UART_FIFO_LEN + 4

Minimum RX and TX buffer size.

static const TickType_t defaultReadTimeout = 300 / portTICK_PERIOD_MS

Default operation timeout in FreeRTOS ticks.

static const uint32_t defaultBaudRate = 115200

Default baud rate.

static const uint16_t defaultDataBits = 8

Default number of data bits.

static const UartParity defaultParity = UartParity::none

Default parity.

static const UartStopBits defaultStopBits = UartStopBits::one

Default number of stop bits.

static const UartFlowControl defaultFlowControl = UartFlowControl::none

Default flow control type.

static const uart_mode_t defaultMode = UART_MODE_UART

Default mode.