Control NI DAQ Device with Python and NI DAQmx

Updated Mar 16, 2019

Reported In

Driver

  • NI-DAQmx

Programming Language

  • Python

Issue Details

I have a USB DAQ system from National Instruments and I want to:
  • Acquire data from my hardware using Python code to do data processing
  • Use the NI-DAQmx C functions in my Python application

Solution

The best way to do this is to use the NI-DAQmx Python API provided by National Instruments (nidaqmx).

Install Python and the NI-DAQmx Python API:
  1. Download Python
  2. Install python
  3. Go to ez_setup.py source code page here
  4. Copy everything and paste it in a new notepad file
  5. Save the notepad file in an easy to access location with the name ez_setup.py
  6. Open a windows command prompt as Administrator
  7. Navigate to the directory where you saved the ez_setup.py file using the command cd
  8. Once in the directory, type ez_install.py and press enter
  9. Go to the script folder of the python installation folder. For example, C:\Program Files (x86)\Python36-32\Scripts
  10. Go to computer properties, then select Advanced system settings
  11. In the Advanced tab click on Environment Variables.. 
  12. Under system variable, select PATH and click on Edit
  13. In Variable value, at the end, add a semicolon and path of script folder as above. For example, add ;C:\Program Files (x86)\Python36-32\Scripts
  14. Also add address of the python folder to PATH of environment variable. For example, add at the end ;C:\Program Files (x86)\Python36-32
 
Figure 2. Editing Environment Variables
  1. Open a windows command prompt as an Administrator
  2. Type easy_install nidaqmx. It will download and install the nidaqmx python API and other dependencies
  3. If you see an error message asking you to install Visual C++ Build Tools, then please download it from here, and install it. Repeat step 17 again

Test Python environment with nidaqmx:
  1. Open IDLE(Python)
  2. Type import nidaqmx and press enter
  3. Type with nidaqmx.Task() as task: and press enter
  4. Type task.ai_channels.add_ai_voltage_chan("Dev1/ai0") press enter. Note that Dev1 is the name of the device connected to computer. You can find this name in NI-MAX. You'll get an error if this name doesn't match. ai0 is the channel number which you want to use to acquire data. Make sure that this is the correct channel name.
  5. Type task.read()and press enter
  6. You should be able to see the value read from AI0 input of Dev1 device.

Figure 3. Testing nidaqmx Python API

Additional Information

The NI-DAQmx driver can be downloaded from the NI website, and then the NI-DAQmx Python Documentation explains how to install the Python support. The documentation for the nidaqmx Python package is hosted on the NI Github Sit. It includes all of the nidaqmx Python example programs, and the function definitions.


If you have already installed Python and the NI-DAQmx Python API but you believe the installation procedure did not go correctly, then you can do a re-installation.

Re-installation procedure:
  1. Open Python Interpreter or Python IDLE
  2. Type import os press enter
  3. Type os.getcwd() and press enter
  4. It gives you the directory where python was installed, take a note of this path
  5. Go to uninstall a program and uninstall Python and Python Launcher
Figure 1. Uninstall existing Python installation
  1. Once the uninstallation is finished delete the folder which you have noted in step 1.3
  2. Now we can proceed for fresh installation

WAS THIS ARTICLE HELPFUL?

Not Helpful