Skip to content
Get started

Virtual UART

The virtual_uart component is a UART without pins. A component that uses it with uart_id reads and writes it like any UART bus. What that component writes runs the on_tx automation, and the virtual_uart.inject_rx action gives it bytes to read. This connects a UART device to any other transport in YAML.

# Example configuration entry
virtual_uart:
- id: my_virtual_uart
baud_rate: 9600
on_tx:
- lambda: |-
ESP_LOGD("virtual_uart", "%u bytes written", (unsigned) data.size());
  • id (Optional, ID): Manually specify the ID used for code generation. Use this ID as uart_id in other components.
  • baud_rate (Required, int): The baud rate reported to the components that use this UART.
  • data_bits (Optional, int): The number of data bits reported. Options: 5 to 8. Defaults to 8.
  • parity (Optional): The parity reported. One of NONE, EVEN, ODD. Defaults to NONE.
  • stop_bits (Optional, int): The number of stop bits reported. One of 1, 2. Defaults to 1.
  • rx_buffer_size (Optional, int): The size of the RX buffer that holds the bytes from inject_rx until they are read. At most 65535. Defaults to 256.
  • on_tx (Optional, Automation): Runs for each write, with the written bytes as data (std::span<const uint8_t>); without it, the bytes are discarded. The data is only valid while the automation runs, so delay, wait_until and other actions that wait are rejected in it. A write to the same UART from on_tx is dropped with a warning.
  • debug (Optional, mapping): Log the bytes that pass this UART. Same options as the UART debug option.

Gives the component on this UART bytes to read. A block that does not fit into the free space of the RX buffer is dropped whole, with a warning.

on_...:
- virtual_uart.inject_rx:
id: my_virtual_uart
data: [0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A]
# Templated, returns a std::vector<uint8_t>
- virtual_uart.inject_rx:
id: my_virtual_uart
data: !lambda return {0x01, 0x02};
  • id (Optional, ID): The virtual UART. Required only when more than one virtual UART is configured.
  • data (Required, string, list of bytes or lambda): The bytes.