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.1.tar.gz (565.2 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.1-py3-none-any.whl (63.7 kB view details)

Uploaded Python 3

serialx-1.7.1-cp310-abi3-win_arm64.whl (183.5 kB view details)

Uploaded CPython 3.10+Windows ARM64

serialx-1.7.1-cp310-abi3-win_amd64.whl (187.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

serialx-1.7.1-cp310-abi3-win32.whl (181.2 kB view details)

Uploaded CPython 3.10+Windows x86

serialx-1.7.1-cp310-abi3-macosx_11_0_arm64.whl (279.7 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

serialx-1.7.1-cp310-abi3-macosx_10_12_x86_64.whl (282.1 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: serialx-1.7.1.tar.gz
  • Upload date:
  • Size: 565.2 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.1.tar.gz
Algorithm Hash digest
SHA256 42bc044647c344b4036cec64473db0216c732e6b6d045a0115e863dcdbdb76e0
MD5 ae8787debc9929ad471e882836bef58d
BLAKE2b-256 b0460e888b57f883eb54b6366cc899bafcd2b91a9f4c5e2ff5b6a03c89310184

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: serialx-1.7.1-py3-none-any.whl
  • Upload date:
  • Size: 63.7 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2f090ae25bd4ac557d387b3f8e22062e78948cfa3478da1691928f81f31303e2
MD5 0e26a1a78d51de69a10d6282d4d8c97b
BLAKE2b-256 69655ce61f6a26e75d00b2156d5f3065ec47917a4eecd1cb1bcd42e4a1b7022e

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.1-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.1-cp310-abi3-win_arm64.whl.

File metadata

  • Download URL: serialx-1.7.1-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 183.5 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.1-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 89312d5a8becd63dc4c5844c057aa5e049736093ffdd809fb6085163be123a82
MD5 8afb71508f21e7d6faddd2cbc3bb467a
BLAKE2b-256 11f8767b8ece09241e9aa2900e36cce11ad36fc068bd32615492dcc2fef50a5c

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.1-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.1-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: serialx-1.7.1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 187.2 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.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4d2ebb45cfd0739f8271e9c860a021dba097773bab49109f3671f25beec60658
MD5 9668f3b3e239a70766c24d18ed3bc0d3
BLAKE2b-256 242d5d0a40cd119e46b7c3a0ea518a5bb280d1dd593189322b8c890a27e7210f

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.1-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.1-cp310-abi3-win32.whl.

File metadata

  • Download URL: serialx-1.7.1-cp310-abi3-win32.whl
  • Upload date:
  • Size: 181.2 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.1-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 68bdc171a82bf0ea077d5ad0de901451d4e35de382aeeb988b97154657951a47
MD5 0da54c6e66894bb64c0229b30b7b4eeb
BLAKE2b-256 95a47cdc4b00a4d8fe302cef11b94ce45463611744b10ff0bcdce23fce95951a

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.1-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.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for serialx-1.7.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 32830bcf6083a32d45fb4fe24043b48128b925352cb4057e6c27cc4f415b4d07
MD5 8cd72a19ea9e21afaec3c968ca89332d
BLAKE2b-256 8e3557da281dee7566c44d98ba680efe64d523416b7c17776b0f18f904534b55

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.1-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.1-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for serialx-1.7.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 627acee48b6326e032ee5161a6b7b72d750677119b282131ff826e699b1e22b9
MD5 88fe26dc6e4933297d01338e217ef63e
BLAKE2b-256 457998debf51d71c445c7b9a24e5ae2a2d6475a414414eee99daf43519325014

See more details on using hashes here.

Provenance

The following attestation bundles were made for serialx-1.7.1-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