Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: Update VISA examples to use the new session management API #446

Merged
merged 7 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
# MEASUREMENTLINK_NISWITCH_SIMULATE=1
# MEASUREMENTLINK_NISWITCH_TOPOLOGY=2567/Independent

#----------------------------------------------------------------------
# VISA Example Measurement Options
#----------------------------------------------------------------------

# To enable simulation with the VISA example measurements, uncomment the
# following line.

# MEASUREMENTLINK_VISA_DMM_SIMULATE=1

#----------------------------------------------------------------------
# NI gRPC Device Server Configuration
#----------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion .env.simulation
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# This is a sample ni-measurementlink-service configuration file that enables
# simulated devices for NI modular instrument drivers.
# simulated devices for NI modular instrument drivers and VISA example
# measurements.
#
# To use it:
# - Copy this file to your service's directory or one of its parent directories
Expand Down Expand Up @@ -31,3 +32,5 @@ MEASUREMENTLINK_NISCOPE_MODEL=5162 (4CH)

MEASUREMENTLINK_NISWITCH_SIMULATE=1
MEASUREMENTLINK_NISWITCH_TOPOLOGY=2567/Independent

MEASUREMENTLINK_VISA_DMM_SIMULATE=1
11 changes: 10 additions & 1 deletion examples/nivisa_dmm_measurement/_visa_dmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@
_RESOLUTION_DIGITS_TO_VALUE = {"3.5": 0.001, "4.5": 0.0001, "5.5": 1e-5, "6.5": 1e-6}

# Supported NI-VISA DMM instrument IDs, both real and simulated, can be added here
_SUPPORTED_INSTRUMENT_IDS = ["Waveform Generator Simulator", "34401"]
_SUPPORTED_INSTRUMENT_IDS = [
# Keysight/Agilent/HP 34401A
"34401",
"34410",
"34411",
"L4411",
# NI Instrument Simulator v2.0
dixonjoel marked this conversation as resolved.
Show resolved Hide resolved
"Instrument Simulator", # single instrument
"Waveform Generator Simulator", # multi-instrument
]


class Function(Enum):
Expand Down
48 changes: 23 additions & 25 deletions examples/nivisa_dmm_measurement/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@
import logging
import pathlib
import sys
from typing import Any, Tuple
from typing import Tuple

import click
import ni_measurementlink_service as nims
from _constants import USE_SIMULATION
from _helpers import (
ServiceOptions,
configure_logging,
create_session_management_client,
get_service_options,
use_simulation_option,
verbosity_option,
)
from _helpers import configure_logging, verbosity_option
from _visa_dmm import INSTRUMENT_TYPE_VISA_DMM, Function, Session
from decouple import AutoConfig
from ni_measurementlink_service.session_management import SessionInformation

script_or_exe = sys.executable if getattr(sys, "frozen", False) else __file__
service_directory = pathlib.Path(script_or_exe).resolve().parent
Expand All @@ -25,7 +19,10 @@
version="0.1.0.0",
ui_file_paths=[service_directory / "NIVisaDmmMeasurement.measui"],
)
service_options = ServiceOptions()

# Search for the `.env` file starting with the current directory.
_config = AutoConfig(str(pathlib.Path.cwd()))
_VISA_DMM_SIMULATE: bool = _config("MEASUREMENTLINK_VISA_DMM_SIMULATE", default=False, cast=bool)


@measurement_service.register_measurement
Expand Down Expand Up @@ -53,31 +50,32 @@ def measure(
resolution_digits,
)

session_management_client = create_session_management_client(measurement_service)

with session_management_client.reserve_session(
context=measurement_service.context.pin_map_context,
pin_or_relay_names=[pin_name],
) as reservation:
with Session(
reservation.session_info.resource_name,
simulate=service_options.use_simulation,
) as session:
with measurement_service.context.reserve_session(pin_name) as reservation:
with reservation.create_session(_create_session, INSTRUMENT_TYPE_VISA_DMM) as session_info:
session = session_info.session
session.configure_measurement_digits(measurement_type, range, resolution_digits)
measured_value = session.read()

logging.info("Completed measurement: measured_value=%g", measured_value)
return (measured_value,)


def _create_session(session_info: SessionInformation) -> Session:
# When this measurement is called from outside of TestStand (session_exists
# == False), reset the instrument to a known state. In TestStand,
# ProcessSetup resets the instrument.
return Session(
session_info.resource_name,
reset_device=not session_info.session_exists,
simulate=_VISA_DMM_SIMULATE,
)


@click.command
@verbosity_option
@use_simulation_option(default=USE_SIMULATION)
def main(verbosity: int, **kwargs: Any) -> None:
def main(verbosity: int) -> None:
"""Perform a DMM measurement using NI-VISA and an NI Instrument Simulator v2.0."""
configure_logging(verbosity)
global service_options
service_options = get_service_options(**kwargs)

with measurement_service.host_service():
input("Press enter to close the measurement service.\n")
Expand Down
10 changes: 9 additions & 1 deletion examples/nivisa_dmm_measurement/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ PyVISA = "^1.13.0"
PyVISA-sim = "^0.5.1"
click = ">=7.1.2, !=8.1.4" # mypy fails with click 8.1.4: https://github.com/pallets/click/issues/2558
grpcio = "*"
python-decouple = ">=3.8"
bkeryan marked this conversation as resolved.
Show resolved Hide resolved

[tool.poetry.group.dev.dependencies]
ni-python-styleguide = ">=0.4.1"
Expand All @@ -24,4 +25,11 @@ requires = ["poetry-core>=1.2.0"]
build-backend = "poetry.core.masonry.api"

[tool.mypy]
disallow_untyped_defs = true
disallow_untyped_defs = true

[[tool.mypy.overrides]]
module = [
# https://github.com/HBNetwork/python-decouple/issues/122 - Add support for type stubs
"decouple.*",
]
ignore_missing_imports = true
Loading
Loading