pw_stream_uart_linux#

pw_stream_uart_linux implements the pw::stream::NonSeekableReaderWriter interface for reading from and writing to a UART using Linux TTY interfaces.

Note

This module will likely be superseded by a future pw_uart interface.

C++#

class UartStreamLinux : public pw::stream::NonSeekableReaderWriter#

pw::stream::NonSeekableReaderWriter implementation for UARTs on Linux.

Public Functions

inline Status Open(const char *path, uint32_t baud_rate)#

Open a UART device using the specified baud rate.

Parameters:
  • path[in] Path to the TTY device.

  • baud_rate[in] Baud rate to use for the device.

Returns:

Code

Description

OK

The device was successfully opened and configured.

INVALID_ARGUMENT

An unsupported baud rate was supplied.

FAILED_PRECONDITION

A device was already open.

UNKNOWN

An error was returned by the operating system.

Status Open(const char *path, Config config)#

Open a UART device using the specified Config struct.

Parameters:
  • path[in] Path to the TTY device.

  • config[in] UartStreamLinux configuration structure.

Returns:

Code

Description

OK

The device was successfully opened and configured.

INVALID_ARGUMENT

Invalid config, for e.g. unsupported baud rate.

FAILED_PRECONDITION

A device was already open.

UNKNOWN

An error was returned by the operating system.

struct Config#

Examples#

A simple example illustrating only changing baud-rate and writing to a UART:

constexpr const char* kUartPath = "/dev/ttyS0";
constexpr pw::stream::UartStreamLinux::Config kConfig = {
  .baud_rate = 115200,
 // Flow control is unmodified on tty.
};
pw::stream::UartStreamLinux stream;
PW_TRY(stream.Open(kUartPath, kConfig));

std::array<std::byte, 10> to_write = {};
PW_TRY(stream.Write(to_write));

A simple example illustrating enabling flow control and writing to a UART:

constexpr const char* kUartPath = "/dev/ttyS0";
constexpr pw::stream::UartStreamLinux::Config kConfig = {
  .baud_rate = 115200,
  .flow_control = true, // Enable hardware flow control.
};
pw::stream::UartStreamLinux stream;
PW_TRY(stream.Open(kUartPath, kConfig));

std::array<std::byte, 10> to_write = {};
PW_TRY(stream.Write(to_write));

Caveats#

No interfaces are supplied for configuring data bits, stop bits, or parity. These attributes are left as they are already configured on the TTY; only the speed or flow control is modified.