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.2.tar.gz (565.6 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.2-py3-none-any.whl (63.4 kB view details)

Uploaded Python 3

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

Uploaded CPython 3.10+Windows ARM64

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

Uploaded CPython 3.10+Windows x86-64

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

Uploaded CPython 3.10+Windows x86

serialx-1.7.2-cp310-abi3-macosx_11_0_arm64.whl (279.4 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

serialx-1.7.2-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.2.tar.gz.

File metadata

  • Download URL: serialx-1.7.2.tar.gz
  • Upload date:
  • Size: 565.6 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.2.tar.gz
Algorithm Hash digest
SHA256 29e3b7fae7bfa9253e5b299b847ebd9ed537357558eb3128eb4079a178a644dc
MD5 ff63e403aee7130d1d5265678f73c9f2
BLAKE2b-256 10adbb2566ff39c10d4683a38cbdd78f79d891d2c7c6dfd5a73664db9cd77da6

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.7.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0b5485a1a4b3e0b2f1f80fe84b7c40fb679d55265f7fb805912a7f52f6585e3d
MD5 f3eb2a2deffd543a00eeb2d107c85481
BLAKE2b-256 f641a8c19071bf60cbf6e0acff4dc3769cf2e705dd1e1a3d11438d82764f078e

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.7.2-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.2-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 717283d86c34ccac4faa782e6ab61d8c32645f1b0f7b01396c31e5e6b6df8118
MD5 917e7a4e74bca17579fe6e6158bb18ec
BLAKE2b-256 8d4973371735e7d4e9e32cb0dc11443df70de0cdd1f42b721b86f3a4f927db70

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.7.2-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.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ebd02aa8b4c293e8fba5268103e5bfd78767980f36faaba5d913877e482a8bc3
MD5 1cad4e14ca1bb4042050ac21f576b4ee
BLAKE2b-256 b24d9951ab6f99d5537d9e5fcc0192f6ac422ee8a6073013dc667921715edefe

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.7.2-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.2-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 48543a67830740b32174fcaa8636a70744fe5f575c4cc7812262f5ac7c10cae2
MD5 ddd7c360bbdaa4079b75156f507b6c3a
BLAKE2b-256 e08682355cdf34904f9c49e4e0a34df13f3feb5465714dd016ffe70f7b5c0505

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.7.2-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7b9fb35b7a9a954a1f4c8793aae1cf710d558682063216884aa74a42be1361ec
MD5 11be477913dcf7ae9b2850abd9086479
BLAKE2b-256 d37939084430e47e40d0411a226d702f8811056fa24123a4a79d9c8c29fd560d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.7.2-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 96fbcc2244d889b912fe5c80a06693881c8c0ffd188df7d286f88b639764cacd
MD5 7669a35bb2d9b211b2be025498fd2ad4
BLAKE2b-256 096164c18aa14229820b8561da82c28233ce3fae1fbd12672409d559e4f09c2b

See more details on using hashes here.

Provenance

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