Metadata-Version: 2.5
Name: uarthub
Version: 0.0.2
Summary: Share one serial port with multiple people over TCP — pure Python, zero config
Project-URL: Homepage, https://github.com/Junbo-Zheng/uarthub
Project-URL: Issues, https://github.com/Junbo-Zheng/uarthub/issues
Author-email: Junbo Zheng <3273070@qq.com>
License: Apache-2.0
License-File: LICENSE
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: >=3.10
Requires-Dist: pyserial>=3.5
Provides-Extra: dev
Requires-Dist: build; extra == 'dev'
Requires-Dist: pytest-cov; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: twine; extra == 'dev'
Description-Content-Type: text/markdown

[![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](LICENSE)

# uarthub

Share one serial port with multiple people over TCP — pure Python, zero config.

A serial device can only be opened by one process at a time, which makes collaborative debugging
awkward: whoever grabbed the port first owns it, and everyone else waits. `uarthub` solves this
by owning the device once and bridging it to any number of TCP clients. Everyone sees the same
serial output, anyone can type commands, and clients join
and leave freely without disturbing each other.

![uarthub architecture](assets/architecture.svg)

Serial device path is whatever your system exposes — `/dev/ttyACM*`, `/dev/ttyUSB*`,
`/dev/ttyS*`, a pty, anything pyserial can open. `uarthubd` is the only process that
holds the device; clients talk to it over TCP and never touch the port directly.

## Features

- **Multi-client broadcast** — serial output goes to every client (and to
  the host's own stdout)
- **Everyone can send** — input from any client is forwarded verbatim to the device
- **Join/leave anytime** — a client disconnecting (Ctrl-C in `uarthub`, the intended way)
  disturbs the others or the device connection
- **Pure Python, zero config** — a single command, the only dependency is
  [pyserial](https://pypi.org/project/pyserial/)

## Install

```bash
pip install uarthub          # or: pipx install uarthub
```

Or run straight from a checkout, no install needed:

```bash
./main.py --help
```

## Usage

The one-command flow — just run `uarthub` on the machine with the device attached; it starts a
background daemon itself if none is running, and that daemon exits by itself once every client
has disconnected:

```bash
uarthub                       # auto-starts a server on /dev/ttyACM1 if needed
uarthub /dev/ttyACM3         # shorthand: a /dev/ path as the positional arg
uarthub --device /dev/ttyACM0 --baud 115200   # which device the server should open
uarthub --device /dev/ttyUSB0                 # any serial path works (ttyACM* / ttyUSB* / ttyS*)
```

An explicit daemon never exits on its own and is the right choice for a shared lab machine:

```bash
uarthubd /dev/ttyACM1 921600 --port 7000      # manual server, stays up
uarthubd /dev/ttyACM1 921600 --auto-exit      # exits 2s after the last client leaves
```

Anyone — on the same machine or over the network — connects from their own terminal:

```bash
uarthub <hub-host>          # or: uarthub <hub-host> -p 7000
```

Daemon arguments:

| Argument | Default | Description |
|----------|---------|-------------|
| `device` | `/dev/ttyACM1` | serial device path |
| `baud`   | `921600` | baud rate |
| `--host` | `0.0.0.0` | TCP listen address |
| `-p, --port` | `7000` | TCP listen port |

Connected clients immediately start receiving serial output and can type commands. **Ctrl-C exits the client**;
the daemon and other clients are unaffected. Multiple people can be connected at the same time;
when two people type simultaneously the bytes simply interleave, so agree on who drives.

`telnet <hub-host> 7000` or `nc <hub-host> 7000` work too — `uarthub` is just a client that
exits the way you expect.

> [!NOTE]
> The hub process is the *only* process that opens the device. Stop any existing terminal or
> logger (e.g. `screen`/`minicom`) that holds the port before starting `uarthub`.

## Development

```bash
pip install -e ".[dev]"   # pytest, ruff, build, twine
pytest                    # runs from src/ via pythonpath, no install needed
ruff check src tests main.py
```

The test suite exercises the full broadcast/send/disconnect behavior against a pty-backed fake
device — no real hardware required.

## How it works

`uarthub` opens the serial device with pyserial and runs a small asyncio server. Serial output
is offered to every connected client; input from any client is written to the device. One owner,
N observers.

## License

This project is licensed under the Apache License, Version 2.0.

See
[LICENSE](LICENSE) or <https://www.apache.org/licenses/LICENSE-2.0> for the
full text.
