# Power Supply (Owon) — control, configuration, tests, and quick demo

This guide covers using the Owon bench power supply via SCPI over serial with the framework.

- Library: `ecu_framework/power/owon_psu.py`
- Hardware test: `tests/hardware/test_owon_psu.py`
- quick demo script: `vendor/Owon/owon_psu_quick_demo.py`
- Configuration: `config/test_config.yaml` (`power_supply`), optionally merged from `config/owon_psu.yaml` or env `OWON_PSU_CONFIG`

## Install dependencies

```powershell
pip install -r .\requirements.txt
```

## Configure

You can keep PSU settings centrally or in a machine-specific YAML.

- Central: `config/test_config.yaml` → `power_supply` section
- Separate: `config/owon_psu.yaml` (or `OWON_PSU_CONFIG` env var)

Supported keys:

```yaml
power_supply:
  enabled: true
  port: COM4           # e.g., COM4 (Windows) or /dev/ttyUSB0 (Linux)
  baudrate: 115200
  timeout: 1.0
  eol: "\n"          # or "\r\n" if required
  parity: N            # N|E|O
  stopbits: 1          # 1|2
  xonxoff: false
  rtscts: false
  dsrdtr: false
  idn_substr: OWON
  do_set: false
  set_voltage: 5.0
  set_current: 0.1
```

The central config loader automatically merges `config/owon_psu.yaml` (or the path in `OWON_PSU_CONFIG`) into `power_supply`.

## Run the hardware test

Skips unless `power_supply.enabled` is true and `port` is set.

```powershell
pytest -k test_owon_psu_idn_and_optional_set -m hardware -q
```

What it does:
- Opens serial with your configured line params
- Queries `*IDN?` (checks `idn_substr` if provided)
- If `do_set` is true, sets voltage/current, enables output briefly, then disables

## Use the library programmatically

```python
from ecu_framework.power import OwonPSU, SerialParams

params = SerialParams(baudrate=115200, timeout=1.0)
with OwonPSU("COM4", params, eol="\n") as psu:
    print(psu.idn())
    psu.set_voltage(1, 5.0)
    psu.set_current(1, 0.1)
    psu.set_output(True)
    # ... measure, etc.
    psu.set_output(False)
```

Notes:
- Commands use newline-terminated writes; reads use `readline()`
- SCPI forms: `SOUR:VOLT`, `SOUR:CURR`, `MEAS:VOLT?`, `MEAS:CURR?`, `output 0/1`, `output?`

## quick demo script

The quick demo reads `OWON_PSU_CONFIG` or `config/owon_psu.yaml` and performs a small sequence.

```powershell
python .\vendor\Owon\owon_psu_quick_demo.py
```

It also scans ports with `*IDN?` using `scan_ports()`.

## Troubleshooting

- Empty `*IDN?` or timeouts:
  - Verify COM port and exclusivity (no other program holding it)
  - Try `eol: "\r\n"`
  - Adjust `parity` and `stopbits` per your device manual
- Windows COM > 9:
  - Most Python code accepts `COM10` directly; if needed in other tools, use `\\.\\COM10`
- Flow control:
  - Keep `xonxoff`, `rtscts`, `dsrdtr` false unless required

## Related files

- `ecu_framework/power/owon_psu.py` — PSU controller (pyserial)
- `tests/hardware/test_owon_psu.py` — Hardware test using central config
- `vendor/Owon/owon_psu_quick_demo.py` — Quick demo runner
- `config/owon_psu.example.yaml` — Example machine-specific YAML
