Example Code

Python: Voltage - Continuous Input.py

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Driver

  • NI-DAQ™mx

Code and Documents

Attachment

Download All

Description

Overview 

NI offers various Python drivers for its hardware, such as NI-DAQmx. I thought it would be nice to create the Voltage - Continuous Input shipping example using Python. A demonstration video of a replicated version made using Python is shown below.

 

Continuous Input.gif

 

Description

The GUI is built using Tkinter and Matplotlib. I tried to emulate the LabVIEW shipping example as closely as possible. Once everything is positioned properly on the window, the user can start the acquisition. The GUI will poll the task until the required number of samples are available to read. The task will stop and be closed when the user presses the stop button. 

 

Hardware and Software Requirements

  • Python 3.6
  • Matplotlib
  • NI-DAQmx
  • NI-DAQmx Python Package
  • NI-DAQmx supported device or simulated device with an analog input channel. 

 

Steps to Implement or Execute Code

  1. You will need to install Python 3.6 or newer, the latest version of Matplotlib, and NI-DAQmx, and the NI-DAQmx Python Package
  2. Once these are installed, you can download the two attached files and place them in a folder next to each other.
  3. Run the Python application in an IDE or text editor of your choice. During development, I used Sublime Text. You can run Python scripts from the editor with the shortcut ctrl+b defined in the Tools dropdown menu of Sublime.
  4. Insure that you have a device in NI MAX that has an analog input channel and that it is listed in the Physical Channel entry box of the Python GUI.  
  5. Click Start Task to begin acquiring data.

 

Additional Information or References

Please feel free to suggest additional improvements and best practices. I would really appreciate any and all advice. My class architecture felt a little clumsy, but the different classes I had could easily be reused in other programs down the road. I also thought of several minor improvements that may or may not be made before the code is submitted for review:

  • There is no error handling for improper user inputs or NI-DAQmx exceptions.
  • The choice of physical channels is not a drop down of options available in NI MAX.
  • Additional settings and trigger options are missing that are present in the LabVIEW example.
  • The start and stop button should probably belong to the graph class. 
  • The stop button should be disabled unless the task is running. 

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
dushfire
Member
Member
on

Hello,

I am trying to build a script based off of this but having one issue. When I try to collect the data at smaller frequency (say 2 Hz), this script (and even LabVIEW for that matter) does not collect data at the desired frequency. I am using cDAQ-9185 and NI 9202 with DSUB module. Am I not able to understand something that is happening here? Why can't I collect the voltage data at 2Hz? 

 

Thank you,

Dushyant