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.3.tar.gz (570.3 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.3-py3-none-any.whl (64.4 kB view details)

Uploaded Python 3

serialx-1.7.3-cp310-abi3-win_arm64.whl (184.2 kB view details)

Uploaded CPython 3.10+Windows ARM64

serialx-1.7.3-cp310-abi3-win_amd64.whl (187.8 kB view details)

Uploaded CPython 3.10+Windows x86-64

serialx-1.7.3-cp310-abi3-win32.whl (181.8 kB view details)

Uploaded CPython 3.10+Windows x86

serialx-1.7.3-cp310-abi3-macosx_11_0_arm64.whl (280.4 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

serialx-1.7.3-cp310-abi3-macosx_10_12_x86_64.whl (282.8 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: serialx-1.7.3.tar.gz
  • Upload date:
  • Size: 570.3 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.3.tar.gz
Algorithm Hash digest
SHA256 a654dec3c033a408750b5923da6d0ef6790246c3e3f54594e345d43b197a1572
MD5 d777be18b5e930ac1a4280f14b9e5b63
BLAKE2b-256 f532b5ccc6750b61f9f7eada3019acf3225bc674b6f93db87f763ea716a4101c

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.7.3-py3-none-any.whl
  • Upload date:
  • Size: 64.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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f75315e3d05a002b8e33e8b0277ce1d4131d8e375c4117047f3a2caed17ae67b
MD5 630ff2dfe759967cdcc3cbb6738a54a9
BLAKE2b-256 055fac1fa6a73461bf7a5b18763905cc7abb876c60f57abb06bac6b40e6922ab

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.7.3-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 184.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.3-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 94c6b24aecf3c8244367d2d937d8aab028e8e29b3e6026eeb82c9440cfc2e42f
MD5 9a2e847d390d68d52b8801d76dc0f800
BLAKE2b-256 03e3086113e05c891f93cb47dbd47668697501070d0d31aa0d2ff97fe60de2da

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.7.3-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 187.8 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.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 da162dbcdca846778f098bdd382430f107990b9484f4037e7d26707dfd55a52b
MD5 bdeee9f9a47f81e2420ef1166744e333
BLAKE2b-256 223970f1f8cc369fbc702f830f9b8714181a2e75e724cb3c44f6b3a972c5bf68

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.7.3-cp310-abi3-win32.whl
  • Upload date:
  • Size: 181.8 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.3-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 e5da0362ac4f3694c04a66f89095551ca5ae3f07fea194217a25ce1d24c18ecb
MD5 295b3c13963c9f5b5321b99f5e30b8d3
BLAKE2b-256 1936b2158e18d36add43428910f329b94e610a0da8336182059b6f47590775af

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.7.3-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 33b60b8c0b97704497e6efa2c09cf8ac886d54e404942319a619b2cc1d41169a
MD5 759d50f92a5d71c5f76303d325d1c7b5
BLAKE2b-256 24ab2d49e4d4cadf56a584f7b9d7bda2ae935560dbd662f6d3babf0dcb407911

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.7.3-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 83d601906ef92ad4676d844c0e1f3676bc7a132493ebc86b22229ef4abfe058f
MD5 600e74a22c861723199dfa87d9513e3e
BLAKE2b-256 ef5e1767f9f6823d7b4141e84564d280ba7736d1f142ae2d349fe67f42dbcc05

See more details on using hashes here.

Provenance

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