Skip to main content
scope_background.py
"""Example: Oscilloscope background daemon.

Demonstrates using InstroScope's background daemon to continuously measure
Vrms, Vpp, and Frequency on channel 1 while printing results in a main loop.

"""

from instro.lib.publishers import NominalCorePublisher
from instro.scope import (
    AcquisitionMode,
    Coupling,
    InstroScope,
    ScopeMeasurementType,
    TriggerMode,
    TriggerSlope,
    TriggerType,
)
from instro.scope.drivers import Keysight1200X

VISA_RESOURCE = "<visa_resource>"  # Replace with your instrument's VISA resource string.
DATASET_RID = "<dataset_rid>"  # Replace with your dataset RID.

scope = InstroScope(
    name="scope",
    driver=Keysight1200X(VISA_RESOURCE),
    num_channels=2,
    publishers=[NominalCorePublisher(DATASET_RID)],
)

with scope:
    # --- Configure channel 1 ---
    scope.set_coupling(Coupling.DC, channel=1)
    scope.set_probe_attenuation(1.0, channel=1)
    scope.set_vertical_scale(1.0, channel=1)  # 1 V/div
    scope.set_vertical_offset(0, channel=1)

    # --- Configure timebase ---
    scope.set_horizontal_scale(0.01)  # 10 ms/div

    # --- Configure acquisition ---
    scope.set_acquisition_mode(AcquisitionMode.NORMAL)

    # --- Configure trigger ---
    scope.set_trigger_source(channel=1)
    scope.set_trigger_type(TriggerType.EDGE)
    scope.set_trigger_slope(TriggerSlope.RISING)
    scope.set_trigger_level(0)  # 0 V
    scope.set_trigger_mode(TriggerMode.NORMAL)

    # --- Register background measurements ---
    scope.add_background_daemon_function(scope.measure, ScopeMeasurementType.VRMS, channel=1)
    scope.add_background_daemon_function(scope.measure, ScopeMeasurementType.VPP, channel=1)
    scope.add_background_daemon_function(scope.measure, ScopeMeasurementType.FREQUENCY, channel=1)

    # --- Start continuous acquisition on the instrument ---
    scope.run()

    # --- Start the background daemon ---
    scope.start()

    # --- Main loop: read back measurements and print ---
    while True:
        try:
            vrms = scope.get_channel("scope.ch1.vrms", length=1, wait_for_new_samples=True)
            vpp = scope.get_channel("scope.ch1.vpp", length=1)
            freq = scope.get_channel("scope.ch1.frequency", length=1)

            print(f"Vrms: {vrms.latest}  Vpp: {vpp.latest}  Frequency: {freq.latest}")

        except KeyboardInterrupt:
            break

    print("Done")