pyserial-asyncio-fast

raw JSON →
0.16 verified Fri May 01 auth: no python

Asyncio-based serial port I/O for Python, built on pyserial. Provides async wrappers for serial communication with support for timeouts and cancellation. Current version 0.16 (2024-04-09).

pip install pyserial-asyncio-fast
error ModuleNotFoundError: No module named 'serial_asyncio'
cause Installed the wrong package or used the old import path.
fix
Install pyserial-asyncio-fast and use 'import serial_asyncio_fast' (underscores).
error AttributeError: module 'serial_asyncio_fast' has no attribute 'create_serial_connection'
cause Function may be named 'open_serial_connection' in this version.
fix
Use 'serial_asyncio_fast.open_serial_connection' instead.
error NotImplementedError: Event loop is not running
cause Called async function outside of an asyncio event loop.
fix
Wrap the call in asyncio.run() or run within a running loop.
deprecated pyserial-asyncio (original) is deprecated. Use pyserial-asyncio-fast (faster, maintained).
fix pip install pyserial-asyncio-fast and change imports from 'serial_asyncio' to 'serial_asyncio_fast'.
gotcha open_serial_connection returns (asyncio.StreamReader, asyncio.StreamWriter), not a custom transport. Writers must be explicitly closed.
fix Always call writer.close() after use, ideally in a try/finally block.
gotcha The package uses 'serial_asyncio_fast' (with underscore), not 'serial-asyncio-fast' (hyphen). Common import error.
fix Use 'import serial_asyncio_fast' (underscores).

Open a serial connection and read/write asynchronously.

import asyncio
import serial_asyncio_fast

async def main():
    reader, writer = await serial_asyncio_fast.open_serial_connection(url='/dev/ttyUSB0', baudrate=115200)
    writer.write(b'hello')
    data = await reader.read(100)
    print(data)
    writer.close()

asyncio.run(main())