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.0.tar.gz (570.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.8.0-py3-none-any.whl (64.6 kB view details)

Uploaded Python 3

serialx-1.8.0-cp310-abi3-win_arm64.whl (184.4 kB view details)

Uploaded CPython 3.10+Windows ARM64

serialx-1.8.0-cp310-abi3-win_amd64.whl (188.1 kB view details)

Uploaded CPython 3.10+Windows x86-64

serialx-1.8.0-cp310-abi3-win32.whl (182.0 kB view details)

Uploaded CPython 3.10+Windows x86

serialx-1.8.0-cp310-abi3-macosx_11_0_arm64.whl (280.6 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

serialx-1.8.0-cp310-abi3-macosx_10_12_x86_64.whl (283.0 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: serialx-1.8.0.tar.gz
  • Upload date:
  • Size: 570.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.8.0.tar.gz
Algorithm Hash digest
SHA256 957843ec6d1fe91454e182884c29b614fb6d93209668f44197bcbb12d373129b
MD5 dbbc7885474ebb47c11e72de62497f12
BLAKE2b-256 841a9f0a4782cc08eeb6705960fc465cf3a70298774d63c3f86e22b84ddc9465

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.8.0-py3-none-any.whl
  • Upload date:
  • Size: 64.6 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 650f12a33d7ddf4068c6a234c9ae1b015d92ab65b714b08def90274f555c35ab
MD5 5f7dad8bd77dde037508a7a57d0a2937
BLAKE2b-256 58795292b2fd3c6e1c5bca04d04c646cc560a0d2a98590180361da1c4643fc40

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.8.0-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 184.4 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.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 43419e09356c2da10fce7892f5930ce989785f621d3229364c9736771ddd80dd
MD5 9beddc7d7331d4c97be94a794388003a
BLAKE2b-256 de4a65e4de353a30fa6b5078b4bee4fb722653203e72777107ac175572d48f2f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.8.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 188.1 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.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 174127ed4307cc81c6dfaa9f6875f3f6075156348412aa1d31e4ee34fd64caf1
MD5 43e9784ecccdb20a7654780ed3c1203e
BLAKE2b-256 48e2b5ff96e4f56a7d59dc2286f87a3e9c1ef619d4db05a10cfceb8ca38a2145

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.8.0-cp310-abi3-win32.whl
  • Upload date:
  • Size: 182.0 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.0-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 c58b2dd3ed90e4a543d870afd60f0fe257888d2336b32fca7d3b32a55d4749f4
MD5 cfda7612f23b83f0f8569ecfc60a8ffc
BLAKE2b-256 3f9b6f13c8be34bc4f46189ebe572585d7e3d8481f7edd035a88bd20a838633e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.8.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b10770a949ac79583974853eaca887763b8ed1b03c249ce03401cd2c4664f084
MD5 41da2a6e053a88ecf01e4c5a7263be1d
BLAKE2b-256 fbd53fb88d6ddccf8685dd16f5bb4548857d0081a90724422950915c09069361

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.8.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 426a6586b81d1d8c80074b79852fcbc512e45dee2e677e44034cf977f6cfe7d0
MD5 6fae040978c2ca6c3249f9c6f65f4b6f
BLAKE2b-256 7ab9a949cf2a51e038dc55e903931a5e2177cfdbcc38a4fef0614bdd1292f94c

See more details on using hashes here.

Provenance

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