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), install:

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

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&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.4.0.tar.gz (528.2 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.4.0-py3-none-any.whl (73.4 kB view details)

Uploaded Python 3

serialx-1.4.0-cp310-abi3-win_arm64.whl (193.3 kB view details)

Uploaded CPython 3.10+Windows ARM64

serialx-1.4.0-cp310-abi3-win_amd64.whl (197.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

serialx-1.4.0-cp310-abi3-win32.whl (191.0 kB view details)

Uploaded CPython 3.10+Windows x86

serialx-1.4.0-cp310-abi3-macosx_11_0_arm64.whl (290.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

serialx-1.4.0-cp310-abi3-macosx_10_12_x86_64.whl (292.6 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for serialx-1.4.0.tar.gz
Algorithm Hash digest
SHA256 9d7c215bdb576f5402e2087b281070e320c4bbab9bd2f05e809e86a5f854e8c5
MD5 b80770f00b2851e6c0c933489a1bdb24
BLAKE2b-256 90fb95854163174daaca048928ad142910aef87964b1556ea86008bcfeb48047

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 73.4 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.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e1219691ee3bcda22d0d740ebb301cdb618319cf6537adbfafefeb886fee25ee
MD5 24ece9ee20434bef63db76bdc205262f
BLAKE2b-256 729835f6879bd7a7e63867f94e50d08eb2fbd9dee2d265a916fded574a71c47c

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.4.0-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 193.3 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.4.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 4a6efdbdde7ebdeabcdb1bacbc98c44940c63441067dddc1a04fe8a271aea7c2
MD5 83771d3cca8a58bbed45bbb9f8308f36
BLAKE2b-256 4fec3831053225932d6a13978230b3225f8bfeff1270c6b0878e905275809c7a

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.4.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 197.0 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.4.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 db63bc01609692708e8471aae83b8de28e4b7014dd026e60ed102fc36608b84b
MD5 9a28729d2a0fe1f9b7f2687c97ab81a4
BLAKE2b-256 28781fba95fb84fbfe757c5b526cdcb13e30e5e7a7619e83b60073842499d329

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: serialx-1.4.0-cp310-abi3-win32.whl
  • Upload date:
  • Size: 191.0 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.4.0-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 23ede2b8bb62cf6a51edf3d3139038f77caff03652911222d6c1d5b115c44e92
MD5 bf5821d933e4aad56fccb09f3f132a80
BLAKE2b-256 de97acc644816ffa919f5ac24b07f98506fd63c4ddc9a265b695f6aa730717e8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.4.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5a65c83610415ea1e05729ff0042df9b55286e0452bcbf0439d0a1395372e9dd
MD5 fc8865ce748a3a50249816c44b086f03
BLAKE2b-256 637c1460be36f141ebf375edda6567b8c2803a674b16b113b4484a52d43c6c29

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for serialx-1.4.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e84f4b450889f173bcfb384e333d2ed5acf6717911a54af21cfadf73261b6323
MD5 0c7856933bcc0dc1521d107b23fa0c13
BLAKE2b-256 ee5db5005f6fb0bcd2807ff20c7a0dd2596e6ea2d916cc617e35670154e468ad

See more details on using hashes here.

Provenance

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