# Using instro

Using instro with some more detail

This page discusses details about using `instro`.

## Instruments

### Two ways to get data

You can acquire measurements from an instrument in two ways. The difference is who triggers the reads and how often.

|  | Background acquisition | On-demand acquisition |
| --- | --- | --- |
| **How** | Call `start()`. A background daemon reads from the instrument at a fixed, configurable rate. Pull the latest N values with `get_channel()` when you need them. | Call read methods (`read_analog()`, `get_voltage()`, …) only when you want a measurement. |
| **Best for** | Apps where data should always be available and publishing. | Apps that need measurements only at specific times; lower system and bus load. DMMs are a good example. |

## open()/close() or `with` block

Instruments can also be open/closed automatically using a `with` block.

**Explicit open() / close()**

Best when the instrument’s lifetime spans many functions, or when you want to interleave setup and teardown across event handlers, fixtures, or REPL sessions.

```python
from instro.psu import InstroPSU
from instro.psu.drivers import SimulatedPSU

psu = InstroPSU(
    name="bench_psu",
    driver=SimulatedPSU("TCPIP0::127.0.0.1::5025::SOCKET"),
    num_channels=2,
)
psu.open()

psu.set_voltage(5.0, channel=1)
psu.set_current_limit(1.0, channel=1)
psu.output_enable(True, channel=1)

voltage = psu.get_voltage(channel=1)
print(f"V: {voltage.latest:.3f} V")

psu.output_enable(False, channel=1)
psu.close()
```

**with block**

Best for scripts and tests where the instrument is used inside a single scope. `close()` runs automatically on the way out, even if an exception is raised.

```python
from instro.psu import InstroPSU
from instro.psu.drivers import SimulatedPSU

with InstroPSU(
    name="bench_psu",
    driver=SimulatedPSU("TCPIP0::127.0.0.1::5025::SOCKET"),
    num_channels=2,
) as psu:
    psu.set_voltage(5.0, channel=1)
    psu.set_current_limit(1.0, channel=1)
    psu.output_enable(True, channel=1)

    voltage = psu.get_voltage(channel=1)
    print(f"V: {voltage.latest:.3f} V")
```
