Skip to content

Is there a "reset instrument to defaults from yaml file" function or similar? #83

@dougthor42

Description

@dougthor42

Is there a function/method that will reset a simulated instrument to the defaults defined in the yaml file?

What I'd like to be able to do is basically:

import pyvisa
rm = pyvisa.ResourceManager("myfile.yaml@sim")
instr = rm.OpenResource("ASRL1::INSTR")

instr.query("volt")  # 5, the default defined by myfile.yaml
instr.write("volt 10")
instr.query("volt")  # 10
instr.reset_to_defaults()  # magic function that I'm asking about
instr.query("volt")  # 5, the default defined by myfile.yaml

Does such a method exist? Closing the resource and/or the resource manager does not reset things (this might be related to #82).

Steps to Reproduce

Create this YAML file. I called it "doug_example.yaml":
---
spec: "1.1"

devices:

  MyDevice:
    eom:
      ASRL INSTR:
        q: "\r"
        r: "\r\n"
    error:
      response:
        command_error: "invalid command. Update the yaml file."
        query_error: "empty buffer"
    dialogues:
      - q: ";LC:DONE?"
        r: "done"
    properties:
      echo:
        default: "0"
        setter:
          q: "echo {}"
          r: "OK"
        getter:
          q: "echo?"
          r: "{}"

resources:
  "ASRL1::INSTR":
    device: "MyDevice"
Create this Python file. I called it "doug_example.py":
import pathlib

import pyvisa

VISA_ADDR = "ASRL1::INSTR"
SIM_BACKEND = str(pathlib.Path(__file__).parent / "doug_example.yaml@sim")


def create_instr() -> tuple[pyvisa.ResourceManager, pyvisa.Resource]:
    rm = pyvisa.ResourceManager(visa_library=SIM_BACKEND)

    instr = rm.open_resource(VISA_ADDR)
    instr.read_termination = "\r\n"
    instr.write_termination = "\r"

    print(f"ResourceManager session: {rm.session}")

    return rm, instr


rm, instr = create_instr()


want = "0"
got = instr.query("echo?")
correct = got == want
print(f"{got = }\t{want = }\t{correct = }")

want = "OK"
got = instr.query("echo 1")
correct = got == want
print(f"{got = }\t{want = }\t{correct = }")


want = "1"
got = instr.query("echo?")
correct = got == want
print(f"{got = }\t{want = }\t{correct = }")


# Magic reset command
print("Running reset")
rm.close()
print("Creating new resource manager and instrument")
rm, instr = create_instr()


want = "0"
got = instr.query("echo?")
correct = got == want
print(f"{got = }\t{want = }\t{correct = }")

Run python doug_example.py and get this output:

$ python doug_example.py 
ResourceManager session: 5282223
got = '0'       want = '0'      correct = True
got = 'OK'      want = 'OK'     correct = True
got = '1'       want = '1'      correct = True
Running reset
Creating new resource manager and instrument
ResourceManager session: 1299675
got = '1'       want = '0'      correct = False

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions