Skip to main content

Serial library with native sync and async support for Windows, Linux, macOS, and other platforms

Project description

Introduction

Serialx is a no-compromise serial communication library for Python targeting common platforms such as Linux (POSIX), macOS, and Windows. It provides both synchronous and native asynchronous APIs for all platforms.

For more information, visit serialx's documentation: https://puddly.github.io/serialx/

Installation

pip install serialx

For drop-in import compatibility (serial, serial_asyncio, serial_asyncio_fast) in environments where existing code cannot be migrated:

pip install serialx-compat

Usage

Serialx features a familiar synchronous API:

import serialx

with serialx.serial_for_url("/dev/serial/by-id/port", baudrate=115200) as serial:
    data = serial.readexactly(5)
    serial.write(b"test")

    serial.set_modem_pins(rts=True, dtr=True)
    pins = serial.get_modem_pins()
    assert pins.rts is serialx.PinState.HIGH
    assert pins.dtr is serialx.PinState.HIGH

An async equivalent of the synchronous API:

import asyncio
import serialx

async def main():
    async with serialx.async_serial_for_url(
        "/dev/serial/by-id/port", baudrate=115200,
    ) as serial:
        data = await serial.readexactly(5)
        serial.write(b"test")
        await serial.flush()

        await serial.set_modem_pins(rts=True, dtr=True)
        pins = await serial.get_modem_pins()
        assert pins.rts is serialx.PinState.HIGH

A (StreamReader, StreamWriter) pair is also available for code already wired up to the asyncio streams API:

import asyncio
import serialx

async def main():
    reader, writer = await serialx.open_serial_connection(
        "/dev/serial/by-id/port", baudrate=115200,
    )

    try:
        data = await reader.readexactly(5)
        writer.write(b"test")
        await writer.drain()
    finally:
        writer.close()
        await writer.wait_closed()

And a low-level asynchronous serial transport for protocol-style consumers:

import asyncio
import serialx

async def main():
    loop = asyncio.get_running_loop()
    protocol = YourProtocol()

    transport, protocol = await serialx.create_serial_connection(
        loop,
        lambda: protocol,
        url="/dev/serial/by-id/port",
        baudrate=115200,
    )

    await transport.set_modem_pins(rts=True, dtr=True)

ESPHome serial proxy

Serialx can communicate with serial devices exposed by ESPHome.

It can either create the API instance directly, for simplicity:

from serialx import open_serial_connection

reader, writer = await open_serial_connection(
    url="esphome://192.168.1.42:6053/?port_name=Zigbee&key=...",
    baudrate=115200,
)

Or reuse an existing API instance, for efficiency:

from aioesphomeapi import APIClient
from serialx import open_serial_connection
from serialx.platforms.serial_esphome import ESPHomeSerialTransport

# An external API instance
api = APIClient(address="192.168.1.42", port=6053, key="...", password=None)
await api.connect(login=True)

reader, writer = await open_serial_connection(
    url=None,
    transport_cls=ESPHomeSerialTransport,
    api=api,
    port_name="Zigbee",
    baudrate=115200,
)

Development

All development dependencies are listed in pyproject.toml. To install them, use:

uv pip install '.[dev]'

On macOS and Windows, a Rust toolchain is required to build the native serial port enumeration extension. Install Rust via rustup.

Set up pre-commit hooks with pre-commit install. Your code will then be type checked and auto-formatted when you run git commit. You can do this on-demand with pre-commit run.

Serialx relies on automated testing. CI runs tests using both socat virtual PTYs (Linux/macOS) and socket-based serial pairs. To also test with physical adapter pairs, pass CLI flags to pytest:

pytest --adapter-pair=/dev/serial/by-id/left1:/dev/serial/by-id/right1 \
       --adapter-pair=/dev/serial/by-id/left2:/dev/serial/by-id/right2

By default, tests run in parallel. You can disable this by passing -n 0 to pytest.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

serialx-1.7.0.tar.gz (546.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

serialx-1.7.0-py3-none-any.whl (63.4 kB view details)

Uploaded Python 3

serialx-1.7.0-cp310-abi3-win_arm64.whl (183.2 kB view details)

Uploaded CPython 3.10+Windows ARM64

serialx-1.7.0-cp310-abi3-win_amd64.whl (186.9 kB view details)

Uploaded CPython 3.10+Windows x86-64

serialx-1.7.0-cp310-abi3-win32.whl (180.9 kB view details)

Uploaded CPython 3.10+Windows x86

serialx-1.7.0-cp310-abi3-macosx_11_0_arm64.whl (279.5 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

serialx-1.7.0-cp310-abi3-macosx_10_12_x86_64.whl (281.9 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file serialx-1.7.0.tar.gz.

File metadata

  • Download URL: serialx-1.7.0.tar.gz
  • Upload date:
  • Size: 546.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for serialx-1.7.0.tar.gz
Algorithm Hash digest
SHA256 e90947d2de6ccae278bc4d9b8c652736ad17aa37697b121e219dbc09a0ad7344
MD5 4b84f52d1fcb86bd28de32110d3f853b
BLAKE2b-256 57c245fb85ab9585088125a11c631e3cb48b30870fe3b9099c4c15969952d305

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.0.tar.gz:

Publisher: publish-to-pypi.yml on puddly/serialx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serialx-1.7.0-py3-none-any.whl.

File metadata

  • Download URL: serialx-1.7.0-py3-none-any.whl
  • Upload date:
  • Size: 63.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for serialx-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 57a870e3508a6ed80435c3d1f72997fba0365b3dc7217dbf14cd3e1f18a8baab
MD5 46dc09daf641abdd6da6f05c07dae1e2
BLAKE2b-256 ca03fd89d3801e689d0a398c66c77a8574a7fe426e569027e890155d0c6e8fc7

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on puddly/serialx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serialx-1.7.0-cp310-abi3-win_arm64.whl.

File metadata

  • Download URL: serialx-1.7.0-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 183.2 kB
  • Tags: CPython 3.10+, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for serialx-1.7.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 33e921d65fc329df8b5802e48bbb2f9faab7643c6640f5863b619239a2a42503
MD5 9b1da2f6f14b480fe92691dabe18101f
BLAKE2b-256 b973856ee512ee7de010ee257a114b73e590ae77b315324a61d3d11d57101374

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.0-cp310-abi3-win_arm64.whl:

Publisher: publish-to-pypi.yml on puddly/serialx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serialx-1.7.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: serialx-1.7.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 186.9 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for serialx-1.7.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 878ad1083c87bbbc7fdcc90b35a59bd4c1658715f2fd6ef065c71f0bbf862f93
MD5 f3d5e2f77c00c30cc1331696f8052cfb
BLAKE2b-256 320fc1c8d62dea29797d8a309ab9d68d406e4938ae11c8d2d7a03d3d5380524f

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.0-cp310-abi3-win_amd64.whl:

Publisher: publish-to-pypi.yml on puddly/serialx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serialx-1.7.0-cp310-abi3-win32.whl.

File metadata

  • Download URL: serialx-1.7.0-cp310-abi3-win32.whl
  • Upload date:
  • Size: 180.9 kB
  • Tags: CPython 3.10+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for serialx-1.7.0-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 88df27f4619506631f7388fb6fc7a615d8adf0ceeb1d9b193814239cd0db1555
MD5 80bf6bc7b980ac50cf07f636b435a0b1
BLAKE2b-256 a93c907b5315879af6d38cd498ca5765e688cd5504346ef255bac7a619453b4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.0-cp310-abi3-win32.whl:

Publisher: publish-to-pypi.yml on puddly/serialx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serialx-1.7.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for serialx-1.7.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bfb8c371c3de1c2e7b1a8e6dbf8c93a78def277fe11f054a526bb6b951ec0dfe
MD5 8431ed5aea78a2362e2bf8d2f6256213
BLAKE2b-256 ad635724e0a15c458747ea88979c781094b7de643b39e59eaabae537714817b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.0-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: publish-to-pypi.yml on puddly/serialx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serialx-1.7.0-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for serialx-1.7.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 618889822751a9e77fb532c1893c2a7e4955f87195ae115425ee79f4336c57fb
MD5 f730b4c86007f9ce1d96a0ff50bde862
BLAKE2b-256 6c81e37a0701e83ef3af4d57ba83fcbe1783fed1ca1c4642a8e4a0611a75c548

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.0-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: publish-to-pypi.yml on puddly/serialx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page