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

Uploaded Python 3

serialx-1.2.2-cp310-abi3-win_arm64.whl (176.1 kB view details)

Uploaded CPython 3.10+Windows ARM64

serialx-1.2.2-cp310-abi3-win_amd64.whl (180.6 kB view details)

Uploaded CPython 3.10+Windows x86-64

serialx-1.2.2-cp310-abi3-win32.whl (174.4 kB view details)

Uploaded CPython 3.10+Windows x86

serialx-1.2.2-cp310-abi3-macosx_11_0_arm64.whl (275.7 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

serialx-1.2.2-cp310-abi3-macosx_10_12_x86_64.whl (280.1 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for serialx-1.2.2.tar.gz
Algorithm Hash digest
SHA256 a727bcd4f2c7c8c2fd068c0352ab84d8dc94dd1d4a738852b3f10b5e4b8f3111
MD5 46d121575a0f8c41ad6e6b46c5b43865
BLAKE2b-256 7a8ef826fc586b90083ce2c7b5d36196fe9d9cfdda256e030b41d052b165a2a6

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for serialx-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 28196d3030e1c962e51c4bb53c27881e07527c6259dbfe2665d97215a194224a
MD5 318cedcfbec591560345b63a2093e833
BLAKE2b-256 db490e5ad26a2f029718b7d3d2d764fde86d8f3d6f55344977c10e00f422452b

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for serialx-1.2.2-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 274c66de672cf96990b72d07798ab942d729e5e5b89fb50091055d03b560baa9
MD5 b46871c441d2d58e43d36891b74af0e5
BLAKE2b-256 64f84b925e2a548096f6eed4f06bc64436545101df0cabcd38a8ac4fa7e2cbb4

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for serialx-1.2.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ee4f95d05756bde7a2da8542cf76398a98e21c301d65fa22fb862fbc2536466e
MD5 790f73f9777712a1b5989fe3840231bb
BLAKE2b-256 247ce000dbd62d5ada136c82bd219acd160df029f7687a5d2945c0fb840b1b4f

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for serialx-1.2.2-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 e2a2992ef9cc7f379731969d914fd51235bca9a460df522200a05f1ce49731b3
MD5 21f92c315d5ccd70f097dbdf42274a36
BLAKE2b-256 8caf3f50f811d60e91886274017ba7e3a5fbd830a24be55178ff7059a6be220f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.2.2-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ebedd5d2b3a424fffd0cb51771783511612b22b0af3a48768832a1ba7e2e5096
MD5 28747fbe3414bc62321d6d7ccc451053
BLAKE2b-256 d9fa6b4f2ba6a796b835fb23706fa0423e46daafca8cb8b72f053daa961b3c30

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.2.2-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e631258dcec8ac8b94315067a701c178f94e46cb127d2507b83444585452063e
MD5 135f03278babfea889a9589185131557
BLAKE2b-256 517dc501d11fbfb732c7e19ec74f3e81ee34ad9bdd9a36c4c55573b9eac3f62c

See more details on using hashes here.

Provenance

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