Metadata-Version: 2.4
Name: riden
Version: 1.0.0
Summary: Asynchronous Python client for Riden RD60xx power supplies (USB serial and Bluetooth LE)
Keywords: riden,rd6006,rd6012,rd6018,rk6006,rk6012,rk6018,power-supply,modbus,bluetooth,serial
Author: Vyacheslav Mayorov
Author-email: Vyacheslav Mayorov <vyacheslav.mayorov@gmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: System :: Hardware
Classifier: Typing :: Typed
Requires-Dist: bleak>=3.1.0
Requires-Dist: tenacity>=9
Requires-Dist: tmodbus[async-serial]>=1.1,<2
Requires-Python: >=3.14
Project-URL: Homepage, https://github.com/acc15/riden
Project-URL: Repository, https://github.com/acc15/riden
Project-URL: Issues, https://github.com/acc15/riden/issues
Description-Content-Type: text/markdown

# riden

An asynchronous client for Riden laboratory power supplies (RD6006, RD6006P,
RD6006-Max, RD6012, RD6012P, RD6012H, RD6018, RD6018P, RD6024, RD6030, RK6006,
RK6006H, RK6012, RK6018, RK4206H, RK8009).

It speaks Modbus RTU over a regular serial port or Bluetooth Low Energy (BLE).

## Features

- asyncio API: `async with Riden.connect(...) as rd: ...`;
- Bluetooth LE via `bt://` URLs on Linux, macOS and Windows (`bleak`);
- Modbus RTU through `tmodbus`, including CRC/framing, timeouts and automatic
  reconnect;
- batched I/O instead of one transaction per field: any set of fields is read
  in one Modbus transaction, and written in one transaction per contiguous
  register block;
- automatic model detection and per-model voltage/current scaling, including
  the RD6012P switchable current range.

## Requirements

- Python 3.14 or newer;
- `tmodbus[async-serial]` for Modbus RTU (installed automatically);
- `bleak` for Bluetooth LE (installed automatically);
- Linux with BlueZ, macOS, or Windows for BLE; any OS supported by `serialx`
  for USB serial.

## Install

```sh
uv add riden
```

or with pip:

```sh
pip install riden
```

From source:

```sh
git clone https://github.com/acc15/riden
cd riden
uv sync
```

## Quick start

```python
import asyncio

from riden import Riden


async def main() -> None:
    async with Riden.connect("bt://E1:F9:C6:24:71:C8") as rd:
        print(await rd.status())

        # Several fields — a single device transaction.
        v, i, p = (await rd.read("v", "i", "p")).values()
        print(v, i, p)

        # Adjacent registers — a single write transaction.
        await rd.write(v_set=5.0, i_set=0.1)
        await rd.write(en=True)


if __name__ == "__main__":
    asyncio.run(main())
```

The address above is the BLE address, not the Classic SPP MAC. All Riden
modules advertise under the same name (`RK6006`), so the URL takes an address;
a device name cannot tell several supplies apart.

For USB serial, pass the device path:

```python
async with Riden.connect("/dev/ttyUSB0") as rd:
    ...
```

The USB port is opened at 115200 baud, the factory setting. If the baud rate
was changed in the device menu (9600, 19200, 38400, 57600), pass it
explicitly. `timeout` is the Modbus response timeout in seconds (default
`1.0`), and `delay` is the pause between transactions (default `0.02`):

```python
async with Riden.connect("/dev/ttyUSB0", baudrate=9600, timeout=0.5) as rd:
    ...
```

BLE has no baud rate; `baudrate` applies only to serial ports. The BLE
connection timeout is the `connect_timeout` argument of `Riden.connect`.

`auto_reconnect` (default `True`) enables tmodbus's automatic reconnect.
Set it to `False` for a strict one-shot session.

Construction parses the URL and builds the tmodbus client without I/O.
`async with` connects and reads the identity; a failed open closes the client
again. `close()` is idempotent.

USB works only when the USB interface is selected in the device menu.

Field names are case-insensitive, so `"V"` and `"v"` refer to the same field.

## Several supplies in one loop

All clients share the caller's asyncio loop; no threads are involved:

```python
import asyncio

from riden import Riden


async def main() -> None:
    async with (
        Riden.connect("bt://<MAC1>") as master,
        Riden.connect("bt://<MAC2>") as follower1,
        Riden.connect("bt://<MAC3>") as follower2,
    ):
        while True:
            values = await master.read("v", "i")
            v, i = values["v"], values["i"]

            await asyncio.gather(
                follower1.write({"v_set": v, "i_set": i}),
                follower2.write({"v_set": v, "i_set": i}),
            )
            await asyncio.sleep(2)


if __name__ == "__main__":
    asyncio.run(main())
```

`async with` opens the clients sequentially, which also avoids BlueZ's
`InProgress` error from simultaneous discoveries. To connect several supplies
in parallel, scan once and pass the discovered `BLEDevice` objects directly:

```python
import asyncio

from bleak import BleakScanner
from riden import Riden

MAC1, MAC2, MAC3 = "E1:F9:C6:24:71:C8", "26:05:E9:A3:00:37", "AA:BB:CC:DD:EE:FF"


async def main() -> None:
    found = await BleakScanner.discover(timeout=5, return_adv=True)
    devices = [
        device for address, (device, _adv) in found.items()
        if address in {MAC1, MAC2, MAC3}
    ]

    rds = [Riden.connect(device) for device in devices]
    await asyncio.gather(*(rd.open() for rd in rds))
    try:
        ...
    finally:
        await asyncio.gather(*(rd.close() for rd in rds))


asyncio.run(main())
```

`BleTransport` is still exported for custom UUIDs or timeouts; `Riden.connect`
accepts a prebuilt transport as well.

## Example: voltage sweep + status

```python
import asyncio

from riden import Riden


async def main() -> None:
    async with Riden.connect("bt://E1:F9:C6:24:71:C8") as rd:
        try:
            await rd.write(v_set=0.0, i_set=0.1, en=True)

            for voltage in range(0, 60, 5):
                await rd.write(v_set=float(voltage))
                await asyncio.sleep(2)
                print(await rd.status())
        finally:
            await rd.write(v_set=0.0, en=False)


asyncio.run(main())
```

## Bluetooth LE

The BLE transport uses `bleak` and works on Linux, macOS and Windows. The
Riden module does not require pairing: power the supply on and connect. All
modules advertise as `RK6006`, so use the BLE address, not the name. On the
tested RK6006/RK6012 the BLE address is the Classic SPP MAC minus one; a BLE
scan (`bluetoothctl`, `BleakScanner`) shows the exact address. On macOS the
BLE address is a CoreBluetooth UUID rather than a MAC.

The `bt://` scheme is accepted. Everything after `://` is passed to bleak as the BLE address; there is no URL query parsing:

```python
async with Riden.connect(
    "bt://E1:F9:C6:24:71:C8", connect_timeout=5
) as rd:
    ...
```

`timeout`, `delay` and `connect_timeout` are regular arguments of
`Riden.connect`, as with any other port.

No keepalive is needed: both tested Riden BLE modules keep an idle link for at
least a minute. If a particular adapter drops an idle link, tmodbus's
automatic reconnect opens it again on the next transaction.

## RD6012P / RD6012PW current range

These models have two current ranges, switched on the front panel: 6 A with
0.1 mA resolution and 12 A with 1 mA resolution. The selected range is
reported in register 20 (`Field.I_RANGE`: `0` is 6 A, otherwise 12 A), and
the library scales every output current by the range that is actually
selected: `10000` for 6 A, `1000` for 12 A.

Reading a current also reads the range, in the same transaction:

```python
# I (11) and I_RANGE (20): one read, registers 11-20
i = (await rd.read("i"))["i"]
```

A write needs the range to encode the current, so the range is read first
and then the setpoint is written. If the caller supplies `i_range`, it is
used as-is, nothing is read, and the range register is written before the
current:

```python
# 1 read of I_RANGE, then 1 write of I_SET
await rd.write(i_set=0.5)

# No reads: I_RANGE is written first, then I_SET
await rd.write(i_set=0.5, i_range=0)
```

Every preset M0..M9 has its own range register, so a preset takes five
registers instead of four (V, I, OVP, OCP, I_RANGE):

| Preset | V   | I   | OVP | OCP | I_RANGE |
|--------|-----|-----|-----|-----|---------|
| M0     | 80  | 81  | 82  | 83  | 84      |
| M1     | 85  | 86  | 87  | 88  | 89      |
| ...    |     |     |     |     |         |
| M9     | 125 | 126 | 127 | 128 | 129     |

`M*_I` and `M*_OCP` are scaled by their own `M*_I_RANGE`, so a complete
preset is still one transaction and needs no reads:

```python
# One write of five registers starting at 85
await rd.write(m1_v=5.0, m1_i=1.0, m1_ovp=60.0, m1_ocp=1.0, m1_i_range=0)
```

On models without a selectable range, `M0_I_RANGE`..`M9_I_RANGE` are virtual:
reading them returns `False`, and writing them is accepted but changes
nothing. `I_RANGE` is a real register on all models; models without a range
switch report `0`.

The 12 A range also has its own output-current calibration pair,
`Field.CAL_I12_ZERO` / `Field.CAL_I12_SCALE` (registers 63 and 64).

One thing is not verified on hardware: whether the device rescales currents
already stored in a preset when the front-panel range changes. The library
always writes a preset's currents together with its range register, so a
freshly written preset is consistent.

## Performance

Reads and writes are batched differently, because Modbus can read a register
range with gaps in it, but it cannot write a sparse list of registers.

**`read()` always uses one transaction.** It computes the overall address
range of the requested fields and reads all of it at once, including any
registers between them, then decodes the fields:

```python
# V (10), I (11), P (12-13): 1 transaction, registers 10-13
v, i, p = (await rd.read("v", "i", "p")).values()

# V_SET (8) and EN (18): still 1 transaction, but registers 8-18 (11 registers)
await rd.read("v_set", "en")
```

Fields far apart make that one transaction longer, so grouping nearby fields
is preferable.

**`write()` uses one transaction per contiguous block.** It encodes and
validates all values first, then merges fields whose registers follow each
other without a gap into one block. Every gap starts a new block and a new
transaction. A value scaled by another field needs that field first: a missing
dependency is read in one transaction, while a dependency passed by the caller
is used as-is.

```python
# V_SET (8) and I_SET (9) are adjacent: 1 transaction
await rd.write(v_set=5.0, i_set=0.1)

# V_SET (8), I_SET (9) and EN (18): 2 transactions,
# registers 8-9, then register 18
await rd.write(v_set=5.0, i_set=0.1, en=True)
```

Since every value is validated before the first transaction, an invalid value
never leaves the device partially updated. A link failure between the
transactions still can: if the second write fails, the first one has
already been applied.

## Development

```sh
uv sync
uv build          # sdist + wheel in dist/
just check        # ty + ruff + pytest
```

### Releasing

Releases are prepared with `bump-my-version` (configured in `pyproject.toml`).
It bumps the version, runs `just check`, updates `uv.lock`, builds the
artifacts, commits and creates an annotated `v<version>` tag:

```sh
just bump patch           # bump, build, commit and tag
just bump minor           # minor (or major)
just bump minor --dry-run # verify without changing anything
just publish              # upload dist/ to PyPI
just publish --dry-run    # verify the upload without sending
git push --follow-tags    # bump-my-version does not push
```

The version part is required by `bump-my-version`: `just bump` without
`patch`/`minor`/`major` fails. `just publish` uploads the existing `dist/`
artifacts; `just bump` already builds them, or run `just build` explicitly.

Publishing tokens are read from `.env` (see `.env.example`). The working tree
must be clean: `bump-my-version` refuses to bump otherwise.

`just publish-test` uploads the current `dist/` to TestPyPI without bumping or
tagging. TestPyPI never accepts the same filename twice, even after the
release is deleted, so a repeated test needs a new version.

Scripts in `scripts/` are run through `uv run`:

```sh
uv run scripts/v_sweep.py
uv run scripts/link_test.py "bt://E1:F9:C6:24:71:C8"
```

`just --list` shows the remaining tasks.

## License

MIT — see [LICENSE](https://github.com/acc15/riden/blob/master/LICENSE).
