Package: ni-measurementlink-service
MeasurementLink™ Support for PythonMeasurementLink™ Support for PythonIntroductionDependenciesDocumentationExamplesDeveloping Measurements: Quick StartInstallationDeveloping a minimal python measurementSteps to run/debug the measurement serviceStatic Registration of Python MeasurementsCreate a batch file that runs a python measurementCreate Executable for Python ScriptsAppendix: Managing Measurement as Python Package (Project)Create and Manage Python Measurement Package using PoetryCreate and Manage Python Measurement Package usingvenvCreate and Manage Python Measurement Package by directly installingni-measurementlink-serviceas a system-level packageIntroductionMeasurementLink Support for Python (ni-measurementlink-service) is a Python
framework that helps you create reusable measurement plug-ins using gRPC
services. Deploy your measurement plug-ins to perform interactive validation in
InstrumentStudio and automated testing in TestStand.DependenciesPython >= 3.8(3.9 recommended)grpcio >= 1.49.1, < 2.xprotobuf >= 4.21, < 5.xpywin32 >= 303 (Only for Windows)DocumentationMeasurementLink ManualAPI ReferenceSystem ConfigurationEnable Win32 Long PathsBy default, Windows has a path length limit of 260 characters. NI recommends enabling support for long paths when developing and deploying Python measurement services.There are three ways to do this:When installing Python using the Python for Windows installer, clickDisable path length limitat the end of the installation.Set theEnable Win32 long pathsgroup policy:Rungpedit.msc.ExpandComputer Configuration»Administrative Templates»All Settings.FindEnable Win32 long pathsin the list, double-click it, and set it toEnabled.In the Windows registry, setHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled(type:REG_DWORD) to 1. For more details, seeMaximum Path Length Limitation.ExamplesTheexamplesdirectory contains example measurements for MeasurementLink 2024 Q1 or later. If
you are using a previous version of MeasurementLink, download the appropriate examples:MeasurementLink 2023 Q1:measurementlink-python-examples-1.0.1.zipMeasurementLink 2023 Q2:measurementlink-python-examples-1.0.1.zipMeasurementLink 2023 Q3:measurementlink-python-examples-1.1.0.zipMeasurementLink 2023 Q4:measurementlink-python-examples-1.2.0.zipFor more information on setting up and running the example measurements, see the includedREADME.mdfile.For best results, use the example measurements corresponding to the version of MeasurementLink
that you are using. Newer examples may demonstrate features that are not available in older
versions of MeasurementLink.Developing Measurements: Quick StartThis section provides instructions to develop custom measurement services in Python using MeasurementLink Support for Python.InstallationMake sure the system has the recommended Python version is installed. Install MeasurementLink Support for Python usingpip.REM Activate the required virtual environment if any.
pip install ni-measurementlink-serviceCheck if you have installed the expected version of MeasurementLink Support for Python installed by running the below command:pip show ni-measurementlink-serviceDeveloping a minimal Python measurementInstall theni-measurementlink-generatorpackage.REM Activate the required virtual environment if any.
pip install ni-measurementlink-generatorRun theni-measurementlink-generatortool. Use command line arguments to specify thedisplay-nameand optionally theversion,measurement-type, andproduct-type.Runningni-measurementlink-generatorwithout optional arguments:ni-measurementlink-generator SampleMeasurement'SampleMeasurement' is the display name of your measurement service. Without the optional arguments,
the other arguments are generated for you based on the display name.Runningni-measurementlink-generatorwith optional arguments formeasurement-version,ui-file,service-class, anddescription-url:ni-measurementlink-generator SampleMeasurement --measurement-version 0.1.0.0 --ui-file MeasurementUI.measui --service-class SampleMeasurement_Python --description-url https://www.example.com/SampleMeasurement.htmlRunningni-measurementlink-generatorwith optional argument fordirectory-outni-measurementlink-generator SampleMeasurement --directory-out <new_path_for_created_files>If no output directory is specified, the files will
be placed in a new folder under the current directory
named after the display name without spaces.To customize the created measurement, provide metadata of the measurement's configuration (input parameters) and outputs (output parameters) inmeasurement.py.Use theconfiguration()decorator to provide metadata about the configurations.The order of the configuration decorator must match with the order of the parameters defined in the function signature.@foo_measurement_service.register_measurement#Display Names can not contains backslash or front slash.@foo_measurement_service.configuration("DisplayNameForInput1",DataType.String,"DefaultValueForInput1")@foo_measurement_service.configuration("DisplayNameForInput2",DataType.String,"DefaultValueForInput2")defmeasure(input_1,input_2):''' A simple Measurement method'''return["foo","bar"]Use theoutput()decorator to provide metadata about the output.The order of the output decorators from top to bottom must match the order of the values of the list returned by the function.@foo_measurement_service.register_measurement@foo_measurement_service.configuration("DisplayNameForInput1",nims.DataType.String,"DefaultValueForInput1")@foo_measurement_service.configuration("DisplayNameForInput2",nims.DataType.String,"DefaultValueForInput2")@foo_measurement_service.output("DisplayNameForOutput1",nims.DataType.String)@foo_measurement_service.output("DisplayNameForOutput2",nims.DataType.String)defmeasure(input_1,input_2):return["foo","bar"]Run/Debug the created measurement by following the steps discussed in the section"Steps to run/debug the measurement service".Steps to run/debug the measurement serviceStart the discovery service if not already started.(Optional) Activate related virtual environments. Measurement developers can skip this step if they are not using anyvirtual environmentsorpoetry-based projects..venv\scripts\activateAfter successful activation, you can see the name of the environment,(.venv)is added to the command prompt.If you face an access issue when trying to activate, retry after allowing scripts to run as Administrator by executing the below command in Windows PowerShell:Set-ExecutionPolicy RemoteSignedRun/Debugthe measurement Python file.To stop the running measurement service, pressEnterin the terminal to properly close the service.(Optional) After the usage of measurement, deactivate the virtual environment. Measurement developers can skip this step if they are not using anyvirtual environmentsorpoetry-based projects.deactivateStatic Registration of Python MeasurementsThe MeasurementLink discovery service provides a registry of other services, and can discover and activate other services on the system. These features allow the discovery service to distinguish, manage, and describe measurement services on the system.To statically register a measurement service with the MeasurementLink discovery service, do the following:Create astartup batch fileorexecutablefor the measurement service.Edit the measurement service's.serviceconfigfile and set thepathvalue to the filename of the startup batch file or executable.Copy the measurement service's directory (including the.serviceconfigfile and startup batch file) to a subdirectory ofC:\ProgramData\National Instruments\MeasurementLink\Services.NoteIf you are using a virtual environment, do not copy the.venvsubdirectory—the virtual environment must be re-created in the new location.Once your measurement service is statically registered, the MeasurementLink discovery service makes it visible in supported NI applications.Create a batch file that runs a Python measurementThe batch file used for static registration is responsible for starting the Python Scripts.Typical Batch File:"<path_to_python_exe>" "<path_to_measurement_file>"Examples to start the fictitious file namedfoo_measurement.py:Using the Python system distributionpython foo_measurement.pyUsing the virtual environmentREM Windows
.\.venv\Scripts\python.exe foo_measurement.py

REM Linux 
.venv/bin/python foo_measurement.pyCreate Executable for Python ScriptsTo create an executable from a measurement, you can use thepyinstallertooling. If you are using a Poetry project, addpyinstallerto itsdev-dependencies. When statically registering the service, install the EXE into a unique directory along with its .serviceconfig and UI files.Typical PyInstaller command to build executable:pyinstaller --onefile --console --paths .venv\Lib\site-packages measurement.pyTroubleshooting"File not found" or "No such file or directory" errors when copying or running a measurement serviceIf copying or running a measurement service produces "File not found" or "No such file or directory" errors, make sure toenable Win32 long paths. If you are unable to enable Win32 long paths, consider deploying the measurement service to a directory with a shorter path.Appendix: Managing Measurement as Python Package (Project)Measurement and its related files can be maintained as a Python package. The basic components of any Python measurement package are:Measurement Python module (.pyfile)This file contains all the details related to the measurement and also contains the logic for the measurement execution.This file is run to start the measurement as a service.UI FileUI file for the measurement. Types of supported UI files are:Measurement UI (.measui): created using theMeasurementLink UI Editorapplication.LabVIEW UI (.vi)The path of this file is configured byui_file_pathinmeasurement_infovariable definition in measurement Python module (.py).Python communities have different ways of managing Python packages and their dependencies. It is up to the measurement developer to decide how to maintain the package and dependencies. Measurement developers can choose from a few common approaches discussed below based on their requirements.Create and Manage Python Measurement Package using PoetryInstallpoetry(one-time setup)Make sure the system has the recommended Python version installed.Installpoetryusing the installation steps given inhttps://python-poetry.org/docs/#installation.Create a new Python project and addni-measurementlink-serviceas a dependency to the project.Open a command prompt, and change the working directory to the directory of your choice where you want to create the project.cd <path_of_directory_of_your_choice>Create a Python package (project) using thepoetry newcommand. Poetry will create boilerplate files and folders that are commonly needed for a Python project.poetry new <name_of_the_project>Add theni-measurementlink-servicepackage as a dependency using thepoetry addcommand.cd <name_of_the_project>
poetry add ni-measurementlink-serviceThe virtual environment will be auto-created by poetry.Create measurement modules as described in"Developing a minimal Python measurement"Any additional dependencies required by measurement can be added usingadd command.poetry add <dependency_package_name>For detailed info on managing projects using poetryrefer to the official documentation.Create and Manage Python Measurement Package usingvenvMake sure the system has the recommended Python version installed.Open a command prompt, and change the working directory to the directory of your choice where you want to create a project.cd <path_of_directory_of_your_choice>Create a virtual environment.REM This creates a virtual environment named .venv
python -m venv .venvActivate the virtual environment. After successful activation.venv\scripts\activate
REM Optionally upgrade the pip within the venv by executing the command
python -m pip install -U pipInstall theni-measurementlink-servicepackage into the virtual environment.pip install ni-measurementlink-serviceCreate measurement modules as described in"Developing a minimal Python measurement"Any additional dependencies required by measurement can be added pip install.pip install <dependency_package_name>For detailed info on managing projects with a virtual environment, refer to theofficial documentation.Create and Manage Python Measurement Package by directly installingni-measurementlink-serviceas a system-level packageMeasurement developers can also installni-measurementlink-serviceas a system package if necessary.Install theni-measurementlink-servicepackage from the command promptpip install ni-measurementlink-serviceCreate measurement modules as described in"Developing a minimal Python measurement"