Interfacing National Instruments Data Acquisition Peripherals With Python

This How-To assumes you have a functioning Python development environment setup and you're looking to use a National Instruments data acquisition peripheral and interface it using Python to circumvent using LabVIEW. You should also be reasonably comfortable reading code by looking through documentation to learn a new API. This How To is also not a wholistic tutorial for every single NI peripheral (that would take years to write), but this should give you an idea of what is needed to get started for your particular peripheral.

Author: Spencer Pollard

The example presented here is done using the NI PCI-5122 Oscilloscope Device card but the principles here should be the same to interface any NI DAQ peripheral. The goal is to use this card to be able to capture waveform data to be used in the new data acquisition software for the REAL.

  • Downloading NI-DAQ™mx: This software provided by NI allows you to correctly see if your peripherals are being properly recognized by your operating system. This software also allows you determine the device's name and its channel numbers which are essential to using Python to interface the device and can be downloaded here. Once downloading the program and installing basically everything it will let you install, you'll be met with a screen similar to this one: Screenshot of NIMAX Once downloaded, you may need an NI account to sign in and properly use this software.

  • Determining Device Name and Channel Number: Now that NIMAX is installed, you can use this software to monitor the status of cards that are installed in your machine. Each device should show up under the "Devices and Interfaces" menu on the left side, if not, you may not have installed your card correctly. When selecting a card, it has a name which can be changed if you'd like. Keep note of what your device is named as this will be necessary when using Python. NIMAX will also let you use the "Test Panels" for your particular device which can better inform you of your device's name and it's channel names. For example, the PCI-5122 Oscilloscope in the InstrumentStudio gives me the device name as well as the different channels it has starting at 0 as shown below:
    Screenshot of InstrumentStudio showing Dev1-PCI-5122

  • Using Python: To use Python, you will need a few things:

    1. NIDAQMX Python Library - pip install as shown below
    2. Device Name - Shown in NIMAX
    3. Channel Identifier - Shown in Test Panels through NIMAX

    To interface NI peripherals using Python, you will need to pip install the nidaqmx package using the following terminal command.

    pip install nidaqmx

    Once this is installed, you're ready to write Python code. I highly recommend having the documentation for the nidaqmx library open as this is where all information about the library is and how to use it. The general structure for your programs that interface with the peripherals generally look like this
    	import nidaqmx as ni
    
    	with ni.Task() as task:
    		# add channels for your peripheral
    		# configure your card's timing for finite or continuous data collection
    		temp = task.read(number_of_samples_per_channel=sample)
    		# do something with data that you read in
    	
    For a specific example, look below where I have a python script written that continuously collects data from the PCI-5122 Oscilloscope Card. Download the actual script that the data acquisition system uses here
    	import nidaqmx as ni
                   
    	sample = 10000
    	sample_rate = 100000000
    	data = []
                   
    	with ni.Task() as task:
    	        # create an analog input voltage channel
    	        task.ai_channels.add_ai_voltage_chan(physical_channel="Dev1/0") (*)
    	        
    	        # configure the card's timing to allow for continuous data collection
    	        task.timing.cfg_samp_clk_timing(rate=sample_rate, sample_mode=ni.constants.AcquisitionType.CONTINUOUS, samps_per_chan=sample)
                   
    	        # Read in the data
    	        temp = task.read(number_of_samples_per_channel=sample)
    	        # place data from temp into data array
    	        for i in temp:
    	        	data.append(i)
                   
    	x = list(range(len(data)))
    	
    Notice on the syntax for describing the physical_channel on the line notated with the (*).

    [Device Name]/[Channel Identifier]

    The channel identifer will be either integers like 0,1,2,... or some cards have many different types of input which constitutes more complex channel names. For example, the "Test Panels" for the PCIe-6323 has channels with names like "ai31" or "_ai0_vs_calSrcHi".

    This is the general strategy for getting your NI peripheral to be interfaced with Python. Each device will be slightly different just because each data acquisition peripheral does something a little different and you'll have to do your own research to figure out what yours does to figure out what kind of channel you need to properly collect data.