UART Devices for Linux

0.1.1 · active · verified Thu Apr 16

uart-devices is a Python library for interacting with UART (Universal Asynchronous Receiver-Transmitter) devices on Linux systems. It provides an asynchronous, structured way to define and communicate with serial devices, abstracting away some of the lower-level details. The current version is 0.1.1, with initial development starting in April 2024 and a low release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize and interact with a UART device using `uart-devices`. It defines a simple device class, opens the port asynchronously, simulates a brief interaction, and then closes it. It includes common troubleshooting tips for permission and device path errors.

import asyncio
import logging
import os
from uart_devices import UARTDevice

logging.basicConfig(level=logging.INFO)

# Set UART_DEVICE_PORT environment variable before running, e.g.:
# export UART_DEVICE_PORT=/dev/ttyUSB0
# For demonstration, we use a common default like /dev/ttyS0 if not set.
# Ensure this port exists and you have permissions.
UART_PORT = os.environ.get("UART_DEVICE_PORT", "/dev/ttyS0")

class MySimpleUARTDevice(UARTDevice):
    port = UART_PORT
    baudrate = 115200 # Common baud rate

    async def _async_on_open(self) -> None:
        logging.info(f"UART device {self.port} opened successfully.")

    async def _async_on_close(self) -> None:
        logging.info(f"UART device {self.port} closed.")

    async def _async_read_data(self, data: bytes) -> None:
        """Called when data is received."""
        logging.info(f"Received data: {data.hex()}")

async def main():
    device = MySimpleUARTDevice()
    print(f"Attempting to interact with UART device on port: {device.port}...")
    try:
        await device.open()
        # In a real application, you would perform async read/write operations here.
        # For this example, we just keep it open briefly and then close.
        await asyncio.sleep(0.5)
        await device.close()
        print(f"Successfully interacted with and closed device on {device.port}.")
    except Exception as e:
        logging.error(f"Failed to interact with UART device on {device.port}: {type(e).__name__}: {e}")
        print("\n--- Troubleshooting Tips ---")
        print("1. Check permissions: Ensure your user is in the 'dialout' or 'uucp' group. (e.g., `sudo usermod -a -G dialout $USER` then log out and back in).")
        print("2. Verify device path: Ensure `UART_DEVICE_PORT` (e.g., /dev/ttyUSB0, /dev/ttyS0) is correct and the device is connected.")
        print("3. Check baud rate: Ensure 115200 matches your device's baud rate.")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →