Skip to main content

Serial library with native async support for Windows and POSIX

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.

Installation

pip install serialx

For drop-in import compatibility (serial, serial_asyncio, serial_asyncio_fast) without runtime patching, install:

pip install serialx-compat

Usage

Serialx features a pyserial and pyserial-asyncio compatibility layer for easy testing. As early as possible, run serialx.patch_pyserial() and it will provide API-compatible replacements.

import serialx
serialx.patch_pyserial()

# These will now use serialx
import serial
import serial_asyncio

Serialx features a familiar synchronous API:

import serialx

with serialx.Serial("/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

A high-level asynchronous serial (reader, writer) pair:

import asyncio
import contextlib

import serialx

async def main():
	reader, writer = await serialx.open_serial_connection("/dev/serial/by-id/port", baudrate=115200)

	with contextlib.closing(writer):
	    data = await reader.readexactly(5)
	    writer.write(b"test")
	    await writer.drain()

And a low-level asynchronous serial transport:

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&noise_psk=...",
    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, noise_psk="...", 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.2.0.tar.gz (510.4 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.2.0-py3-none-any.whl (56.0 kB view details)

Uploaded Python 3

serialx-1.2.0-cp310-abi3-win_arm64.whl (175.0 kB view details)

Uploaded CPython 3.10+Windows ARM64

serialx-1.2.0-cp310-abi3-win_amd64.whl (179.5 kB view details)

Uploaded CPython 3.10+Windows x86-64

serialx-1.2.0-cp310-abi3-win32.whl (173.3 kB view details)

Uploaded CPython 3.10+Windows x86

serialx-1.2.0-cp310-abi3-macosx_11_0_arm64.whl (274.6 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

serialx-1.2.0-cp310-abi3-macosx_10_12_x86_64.whl (279.0 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for serialx-1.2.0.tar.gz
Algorithm Hash digest
SHA256 d2353de3bb55230a6797e00c6dad41760585569f898b95a2866165825ae55cee
MD5 0e16bbbf0df4f639829fd436491f8b13
BLAKE2b-256 258717501bd4b49de13949fb09dbba32adfae8beae0677374015f44e6eb6bce5

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 56.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for serialx-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04be83485177eae911e61ae88d3368501b6053d9b10bbf2fd09e21580d426f01
MD5 dba2fe0dc2a396eada998e29909b1a89
BLAKE2b-256 8c06342ca02c7b047c843b11bb864f2ba9386d85dbad21f332d402e7df1ec9ff

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.2.0-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 175.0 kB
  • Tags: CPython 3.10+, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for serialx-1.2.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 23cc60a17072f58b719c06b33ef3ea5ded783c336870e2908f0be6939098638f
MD5 a48f46302aa9710a704938dc5a02129e
BLAKE2b-256 b13954240a7f44464f7e69855e4370cd6f88cc80f9a9c0dbf8c8694c1e649bce

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.2.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 179.5 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for serialx-1.2.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b0d7e01f3cb70971a19bbbe599c0bd9f5946a64f511f6fd6feea8e26266cac49
MD5 819add932b77508f29895a5a747726d3
BLAKE2b-256 8af5bdcf0edb77aca98d796524cd95f51620b88996e2ade0eb8eaa04c3d9e5ed

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.2.0-cp310-abi3-win32.whl
  • Upload date:
  • Size: 173.3 kB
  • Tags: CPython 3.10+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for serialx-1.2.0-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 9e39133dffc4dc10192e6f1a71ea36aeb6c15127fe174cff1ec64b6ae95af384
MD5 4ef6e9529ebca55e01b02e7416173ad4
BLAKE2b-256 b9018c8d9ead45290a42bdf871f402443a65dd0956b63445b5d1850cfe4a1f35

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.2.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d0922cec6c74f02e08075461bad9b890bfe3fed4ada236349cb3c03c21dba8ba
MD5 db4af706426a687def36c807adbb801b
BLAKE2b-256 449f97e7c5976832620ccf5a49eddba2f1562a5db0d7989c6d032fc59e85e48b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.2.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4b66593440fd5770246080ed34d60290b3f3ff34f8cbbf84349a3bf05a8894ee
MD5 56854506f5b17e7c5499b3d67fed398f
BLAKE2b-256 eeb4a62938f77b064681379402cedb1b66b2278ab9545acd3e134153ef69e44c

See more details on using hashes here.

Provenance

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