Python Resources for NI Hardware and Software

Publish Date: Aug 20, 2019 | 14 Ratings | 3.86 out of 5 | Print


The Python programming language has grown in popularity across virtually all industries and is becoming increasingly used in test and measurement applications. What has helped make Python appropriate for the test and measurement space is its ability to integrate with NI hardware and software to leverage its scripting strengths with NI's rich measurement expertise.

When seeking to efficiently develop a test and measurement solution, there are many times where it makes sense to use Python alongside NI's hardware and software platforms. While it is feasible to build an entire measurement application solely with Python, it is often advantageous to use multiple tools together to benefit from their respective strengths. Follow the linked tutorials below to learn more about how using NI tools with Python can be a great solution for automated test.

Table of Contents

  1. Python APIs for NI Hardware
  2. Calling NI Hardware DLLs Using ctypes
  3. Integrate Python into LabVIEW
  4. Test Architecture Development
  5. Next Steps

1. Python APIs for NI Hardware

Open source developers and National Instruments have made this process even easier for the Python community to integrate with NI hardware by creating modules that abstract the lower-level ctypes function calls into simplified APIs .The following Python modules were created by NI and generous open source developers to provide a direct solution for hardware integration. The following links lead to downloads for some of the Python wrappers that have been created for NI software drivers.


Automating PXI Modular Instruments

NI offers more than 600 different PXI modules ranging from DC to mmWave. Because PXI is an open industry standard, nearly 1,500 products are available from more than 70 different instrument vendors. With standard processing and control functions designated to a controller, PXI instruments need to contain only the actual instrumentation circuitry, which provides effective performance in a small footprint. Combined with a chassis and controller, PXI systems feature high-throughput data movement using PCI Express bus interfaces and sub-nanosecond synchronization with integrated timing and triggering.

nimi-python is a collection of Python modules that provide an interface to the underlying NI modular instruments' drivers. Currently, NI offers Python support through this package for PXI oscilloscopes, waveform generators, DMMs, SMUs, power supplies, and switches. These instruments use the following drivers:

  • NI-SCOPE (Python module: niscope)
  • NI-FGEN (Python module: nifgen)
  • NI-DMM (Python module: nidmm)
  • NI-DCPower (Python module: nidcpower)
  • NI-SWITCH (Python module: niswitch)
  • NI-ModInst (Python module: nimodinst)


Learn more about nimi-python

Acquiring Data from Sensors with NI CompactDAQ (NI-DAQmx)

CompactDAQ is a portable, rugged DAQ platform that integrates connectivity and signal conditioning into modular I/O for directly interfacing to any sensor or signal. From in-vehicle data logging to benchtop research, the breadth of bus, chassis, controller, and I/O conditioning options combined with the customizable nature of LabVIEW software provide the best solution to meet the needs of any medium-channel-count application.

NI-DAQmx is the driver software you use to communicate with and control your NI data acquisition (DAQ) device. The nidaqmx package contains an API for interacting with the NI-DAQmx driver, implemented in Python. This package was created and is supported by NI. The package is implemented as a complex, highly object-oriented wrapper around the NI-DAQmx C API using the ctypes Python library.


Automating Measurements with VirtualBench

VirtualBench plays a key role in reducing the cost and footprint of test and measurement systems by consolidating five of the most commonly used instruments into one device without compromising the performance of each instrument. Combined with a modern software experience and simple programming interface, VirtualBench creates new efficiencies for engineers interacting with benchtop test equipment or developing low-cost automated test systems.

The VirtualBench hardware family currently consists of three models most easily designated by oscilloscope analog bandwidth: 100, 350, and 500 MHz. These models allow the VirtualBench family to serve a wide range of applications and price points in academic labs, hardware characterization/debug benches, and automated test systems.

pyVirtualBench is a python library that provides an interface to the underlying VirtualBench driver by wrapping the C API.

Note: pyVirtualBench is a third-party wrapper and is not directly supported by NI.



Communicate Between Real-Time Processors and FPGAs

The National Instruments FPGA Interface Python API is used for communication between processor and FPGA within NI reconfigurable I/O (RIO) hardware such as NI CompactRIO, NI Single-Board RIO, NI FlexRIO, and NI R Series multifunction RIO.

With the FPGA Interface Python API, developers can use LabVIEW FPGA to program the FPGA within NI hardware and communicate to it from Python running on a host computer. This gives engineers and scientists with Python expertise the ability to take advantage of compiled LabVIEW FPGA bitfiles, also the option to reuse existing Python code.


Learn more about FPGA Interface Python API

Acquire and Process Images/Vision

NI Vision is a National Instruments image/vision processing library. It is available for multiple platforms including Windows and the CompactRIO. This project makes the functionality of NI Vision available in the Python programming language.

Note: nivision is a third-party wrapper and is not directly supported by NI.


Learn more about nivision

Hardware-in-the-Loop Simulation with CAN, LIN, and FlexRay

NI-XNET is the driver software technology behind NI-XNET CAN, LIN, and FlexRay PCI, PXI, and NI C Series interfaces. It is a set of driver software and APIs for NI LabVIEW, NI LabWindows/CVI, and C/C++ on Windows and LabVIEW Real-Time OSs. NI-XNET CAN, LIN, and FlexRay interfaces are optimized for applications requiring real-time, high-speed manipulation of hundreds of CAN frames and signals, such as hardware-in-the-loop simulation, rapid control prototyping, bus monitoring, and automation control.

The NI-XNET platform combines a series of high-performance CAN, LIN, and FlexRay interfaces with the NI-XNET API, which is a common set of easy-to-use functions for reading and writing CAN, LIN, and FlexRay frames and signals in many different platforms including PXI, PCI, NI CompactDAQ, and NI CompactRIO.


Learn more about nixnet

Automating Traditional, Benchtop Instruments (NI-VISA)

The Virtual Instrument Software Architecture (VISA) is a standard for configuring, programming, and troubleshooting instrumentation systems comprising GPIB, VXI, PXI, Serial, Ethernet, and/or USB interfaces. VISA provides the programming interface between the hardware and development environments such as LabVIEW, LabWindows/CVI, and Measurement Studio for Microsoft Visual Studio. NI-VISA is the National Instruments implementation of the VISA I/O standard. NI-VISA includes software libraries, interactive utilities such as NI I/O Trace and the VISA Interactive Control, and configuration programs through Measurement & Automation Explorer for all your development needs. NI-VISA is standard across the National Instruments product line. With NI-VISA, you can feel confident that your software development will not become obsolete as your instrumentation interface hardware needs evolve into the future.

PyVISA is a Python package that enables you to control all kinds of measurement devices independently of the interface (e.g. GPIB, RS232, USB, Ethernet). As an example, reading self-identification from a Keithley Multimeter with GPIB is as easy as three lines of Python code.

Note: PyVISA is a third-party wrapper and is not directly supported by NI.



Back to Top

2. Calling NI Hardware DLLs Using ctypes

While useful Python modules with intuitive APIs exist for hardware communication with many devices, developers frequently rely on C APIs provided with most hardware drivers to integrate their Python code with hardware. The same methodology can be applied to using National Instruments hardware with Python via the ctypes module.

The linked tutorial provides insight into using the ctypes Python module to call into NI hardware drivers through the driver's C API. This solution provides an intuitive way to integrate Python scripts with most hardware components.

Python calling NI drivers' C DLLs using ctypes



In addition to using the ctypes module to access a hardware driver's C API, LabVIEW has the ability to generate DLLs composed of hardware API functions that can be called from Python. This same procedure allows developers to abstract a sequence of hardware interactions into a single function call stored in the DLL file. In addition to calling the C API directly, the following linked tutorial acts as a guide for generating and then calling a DLL from Python.

Python calling LabVIEW


Back to Top

3. Integrate Python into LabVIEW

LabVIEW simplifies hardware integration so that you can rapidly acquire and visualize data sets from virtually any I/O device, whether by NI or a third-party. Combined with a graphical programming syntax that reduces programming time, LabVIEW streamlines complex system design with tools and IP at the forefront of today’s technology.

LabVIEW 2018 introduces the Python Node, which provides native capability to intuitively call a Python script from a LabVIEW Block Diagram.  It uses LabVIEW primitives, providing a way to interoperate between the languages with low latency. 

For more advanced usage with Python, including class method support, debugging, deployment of the Python interpreter and package dependencies, we recommend the Python Integration Toolkit for LabVIEW by Enthought. With fast two-way communication between environments, your LabVIEW project can benefit from thousands of mature, well-tested software packages in the Python ecosystem. Quickly and efficiently access scientific and engineering tools for signal processing, machine learning, image analysis, data import/export, and more. With only minimal coding on the Python side, this extraordinarily simple interface provides access to all of Python’s capabilities.

Learn more about the Python Integration Toolkit for LabVIEW by Enthought


Back to Top

4. Test Architecture Development

With the Python programming language, it is possible to create test architectures with complex features like database logging, requirements documentation generation, or multi-language test step integration. However, the cost of building, debugging, and maintaining the internal test executive framework can often distract test engineers from focusing on building optimized test steps and sequences. NI TestStand, a test executive with a full feature set, can be paired with Python scripts to allow engineers to focus their attention on developing test steps that plug back into the TestStand framework.

Starting with TestStand 2019, the included Python Adapter offers an integrated, configuration-based experience for calling Python code modules from your test sequences. The adapter offers support for the following features:

  • Execute Python scripts on disk by calling a function defined in a module or class, creating a class instance, and getting/setting member or static attributes defined in a module or class.
  • Execute Python scripts in Python versions 2.7 or 3.6+.
  • Execute Python scripts out-of-process in the CPython Interpreter.
  • Convert data between Python and TestStand variables.
  • Store and reuse Python objects in a TestStand object reference variable.
  • Use multiple interpreter session to execute Python scripts in parallel, including using Python 2.7 and 3.6 simultaenously.

Refer to Calling Scripting Languages from NI TestStand for more information.


Back to Top

5. Next Steps


Back to Top

Bookmark & Share


Rate this document

Answered Your Question?
Yes No