How Can I Set the Sample Rate for a DAQmx Task on Python?

Updated Mar 1, 2019

Reported In

Hardware

  • CompactDAQ Chassis

Driver

  • NI-DAQmx

Programming Language

  • Python

Issue Details

I am using Python to control my DAQ device. I can create a task and connect to the device, but I want to configure the timing options.
How can I set the sample rate and sampling mode for my task?

Solution

You can configure the timing properties of the nidaqmx.task class as shown in the reference document. One way to do it is using the cfg_samp_clk_timing method. To do this, follow these steps while writing your script:
  • Import the required timing constants using the following lines. More information can be found on the Constants section on the reference.
from nidaqmx.constants import Edge
from nidaqmx.constants import AcquisitionType
 
  • Create your task and add channels to it. (This example will use a digital input task)
with nidaqmx.Task() as digitalInputTask: 
        digitalInputTask.di_channels.add_di_chan("cDAQ1Mod3/port0/line0", line_grouping=LineGrouping.CHAN_PER_LINE)
 
  • Use cfg_samp_clk_timing to configure the timing options
digitalInputTask.timing.cfg_samp_clk_timing(1000, source="", active_edge=Edge.RISING, sample_mode=AcquisitionType.FINITE, samps_per_chan=5)

The parameters for this method are the following:
 
  • rate (float) – Specifies the sampling rate in samples per channel per second.
  • source (Optional[str]) – Specifies the source terminal of the Sample Clock. Leave this input unspecified to use the default onboard clock of the device.
  • active_edge  – Specifies on which edges of Sample Clock pulses to acquire or generate samples.
  • sample_mode – Specifies if the task acquires or generates samples continuously or if it acquires or generates a finite number of samples.
  • samps_per_chan (Optional[long]) – Specifies the number of samples to acquire or generate for each channel when using FINITE_SAMPLES

Your code should be similar to this:
import nidaqmx
from nidaqmx.constants import LineGrouping
from nidaqmx.constants import Edge
from nidaqmx.constants import AcquisitionType 

with nidaqmx.Task() as digitalInputTask: 
        digitalInputTask.di_channels.add_di_chan("cDAQ1Mod3/port0/line0", line_grouping=LineGrouping.CHAN_PER_LINE)
        digitalInputTask.timing.cfg_samp_clk_timing(100, source="", active_edge=Edge.RISING, sample_mode=AcquisitionType.FINITE, samps_per_chan=50)
        data = digitalInputTask.read(50);
        print(data)






 

Additional Information

For information on how to get started using your NI-DAQ device with Python, consult the following article.
The full reference for the Python API can be found in this link.
 

WAS THIS ARTICLE HELPFUL?

Not Helpful