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)
        await serial.write(b"test")

        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.8.1.tar.gz (695.0 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.8.1-py3-none-any.whl (66.7 kB view details)

Uploaded Python 3

serialx-1.8.1-cp310-abi3-win_arm64.whl (186.5 kB view details)

Uploaded CPython 3.10+Windows ARM64

serialx-1.8.1-cp310-abi3-win_amd64.whl (190.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

serialx-1.8.1-cp310-abi3-win32.whl (184.5 kB view details)

Uploaded CPython 3.10+Windows x86

serialx-1.8.1-cp310-abi3-macosx_11_0_arm64.whl (282.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

serialx-1.8.1-cp310-abi3-macosx_10_12_x86_64.whl (285.4 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for serialx-1.8.1.tar.gz
Algorithm Hash digest
SHA256 ab0ff3c65b41eb08ea2400bec424e8882eaf0f31c56c4f6930d19f9ffe8e6ea6
MD5 32caefb442c3c9abcc93fc4f6defc523
BLAKE2b-256 0ac71fad1ffa6522334628a22710a23b31a393d28a26bd3ab60ad53da1f9eab8

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.8.1-py3-none-any.whl
  • Upload date:
  • Size: 66.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.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f46c01c012fb6b513e1f40449bc5d71c5980865fc01e3eb457a2164671cf189
MD5 30291ddca0f6d13af075159b4cb28e5c
BLAKE2b-256 7d82dd17aa90d43ce6c608720e5ab7a80543323829ca50fae6b6d2ff190f5fa1

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.8.1-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 186.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.8.1-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 ad07d7eb4e3034399dbf4e3e975b050d950e9c57011820ed66f4c833f10fda70
MD5 a162c77e3c0bd1fb39d64fabf93148e5
BLAKE2b-256 291772c081b17ecd1b3b8d7688ea301784caa6cf2edeaf02cd4d6a17bbdc3450

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.8.1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 190.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.8.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 923b8c69806865466ad587779b64d79831876d7f8af7e0b6079d37d21da40008
MD5 c418f262d0fa6046d1b7d6688ea9a67e
BLAKE2b-256 865da892139a4e1245b90ab221c36466666841eec63dfecdbece26593948497b

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.8.1-cp310-abi3-win32.whl
  • Upload date:
  • Size: 184.5 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.8.1-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 63f45f2f87df5c89774fa166c101993b0e043c25d22c5f95b3bfc82a1cc44527
MD5 e92b023b6850896896dde5aa9db148b6
BLAKE2b-256 7435115f7b3c7ca3b62e557c1265db73da13034fbdfac2dfbb17c5e5aa436cab

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.8.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 07e3caea63f12658e1c884a307c5a14e51360f614525f1153c5fcfe22dd225af
MD5 74daad0babfb1f7da7074d2769e1b980
BLAKE2b-256 fc6ac862e046829f4670131f6111baeae96bfed2db3ebaaa3fc8f9313513fbd1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.8.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d978cceb620740d7f40191c3a62733b54115a848ff8cc97232e153cea2188ec4
MD5 783eac8bdaab0cffe5f71bb95f5f4665
BLAKE2b-256 3fb02d1b152ac121ffe614c20d85cb52cdda64f952d55c55a0cb3b188b820be4

See more details on using hashes here.

Provenance

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