Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Using NIDAQmx cfg_samp_clk_timing in python 3.6

Solved!
Go to solution

Hi,

I'm trying to use python with NIDAQmx and the NI USB DAQ6008 nodule. I want to sample one channel -  Ai0 at 5kHz samples per sec and I want to read 50 samples. I'm unclear how to format the timing line in the code. When I run the prog. I get the following error - AttributeError: 'AIChannelCollection' object has no attribute 'cfg_samp_clk_timing'

There's obviously something wrong with my formatting of the 'cfg_samp_clk_timing'. Could someone tell me what I'm doing wrong and how to correct this. Python is running in Anaconda 4.4.0 and my coding is in the Spyder GUI.

 

My code is as follows:

 

import nidaqmx
import numpy as np
import matplotlib.pyplot as plt

Samples_Per_Sec =5000  #max is 10k samples/sec if reading 1 channel, 5k if 2 ch. read etc.
Samples_Per_Ch_To_Read = 50

 
with nidaqmx.Task() as task:
    task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
    data = task.read(Samples_Per_Ch_To_Read )
    task.ai_channels.cfg_samp_clk_timing(Samples_Per_Sec,RISING,FINITE,Samples_Per_Ch_To_Read)

    print(data)

0 Kudos
Message 1 of 12
(11,313 Views)

Hi just to correct one thing from my post below. Even when I rearrange the lines setting up all the channel configuration before reading I still get the same error. -  AttributeError: 'AIChannelCollection' object has no attribute 'cfg_samp_clk_timing'

 

My code is as follows:

 

import nidaqmx
import numpy as np
import matplotlib.pyplot as plt

Samples_Per_Sec =5000  #max is 10k samples/sec if reading 1 channel, 5k if 2 ch. read etc.
Samples_Per_Ch_To_Read = 50

 
with nidaqmx.Task() as task:
    task.ai_channels.add_ai_voltage_chan("Dev1/ai0")    

    task.ai_channels.cfg_samp_clk_timing(Samples_Per_Sec,RISING,FINITE,Samples_Per_Ch_To_Read)

    data = task.read(Samples_Per_Ch_To_Read )

    print(data)

0 Kudos
Message 2 of 12
(11,293 Views)

Hi,

From checking the example code on the PyDAQmx pages, I think the issue if the method name itself. This is how it is referenced in the example code:

 

analog_input.CfgSampClkTiming("",10000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000)

 So for your system, it would look like: 

 

task.ai_channels.CfgSampClkTiming(" ",Samples_Per_Sec,RISING,FINITE,Samples_Per_Ch_To_Read)

 

From the info I could find, that would be the change to make.

 

https://pythonhosted.org/PyDAQmx/usage.html

 

Nic

--------
Nico
Systems Engineer

Certified TestStand Architect Certified LabVIEW Architect

0 Kudos
Message 3 of 12
(11,266 Views)

@Nico011: Parto is using the nidaqmx python package, not PyDAQmx.

 

@Parto: cfg_samp_clk_timing is a method of task.timing, not task.ai_channel. I think that should be task.timing.cfg_samp_clk_timing(...).

——
Brandon Streiff
ni.com/compactdaq · ni.com/daq
0 Kudos
Message 4 of 12
(11,260 Views)

You are right, apologies.

 

Nico

--------
Nico
Systems Engineer

Certified TestStand Architect Certified LabVIEW Architect

0 Kudos
Message 5 of 12
(11,252 Views)

Thanks Brandon,

Your welcome input has got me further along in  setting up the "cfg_samp_clk_timing" routine but I still have problems entering any parameters other than the sampling rate. The first screegrab - "spyder screengrab working prog." shows that I can get an output with just the sampling rate parameter included. But I also want to specify a) the source - on board clock of the device.

b) active edge to be the rising edge c) sample mode to be finite samples and d) sample count to be specifiable. The second screenshot is what I get when I try to enter these parameters. Again any help would be appreciated.

Pat 

0 Kudos
Message 6 of 12
(11,248 Views)

The documentation says the method signature is:

cfg_samp_clk_timing(rate, source=u'', active_edge=<Edge.RISING: 10280>, sample_mode=<AcquisitionType.FINITE: 10178>, samps_per_chan=1000)

 

In your case, you have:

task.timing.cfg_samp_clk_timing(Samples_Per_Sec, RISING, FINITE, number_of_samples_per_channel)

However, that has you trying to specify "RISING" (an integer) as the value for the second argument, "source" (a string). You'll either need to specify a "source" argument, or use keyword arguments:

task.timing.cfg_samp_clk_timing(Samples_Per_Sec, u'', RISING, FINITE, number_of_samples_per_channel)

task.timing.cfg_samp_clk_timing(Samples_Per_Sec, active_edge=RISING, sample_mode=FINITE, samps_per_chan=number_of_samples_per_channel)

However, since it seems that you're not changing the active edge or sample mode from the defaults, you can also omit them.

task.timing.cfg_samp_clk_timing(Samples_Per_Sec, samps_per_chan=number_of_samples_per_channel)

 

 

——
Brandon Streiff
ni.com/compactdaq · ni.com/daq
0 Kudos
Message 7 of 12
(11,239 Views)

Hi Brandon,

Thanks again for your help. I tried your suggestions. First using either of the below lines, one at a time 

task.timing.cfg_samp_clk_timing(Samples_Per_Sec, u'', RISING, FINITE, number_of_samples_per_channel)
task.timing.cfg_samp_clk_timing(Samples_Per_Sec, active_edge=RISING, sample_mode=FINITE, samps_per_chan=number_of_samples_per_channel)

results in exactly the same error which again is:

File "C:\Users\Pat\Anaconda3\lib\site-packages\nidaqmx\_task_modules\timing.py", line 2491, in cfg_samp_clk_timing
    self._handle, source, rate, active_edge.value, sample_mode.value,

AttributeError: 'int' object has no attribute 'value'

The problem seems to be with the "active_edge" and "sample_mode" parameters because I can use the following line without errors.

task.timing.cfg_samp_clk_timing(Samples_Per_Sec, u'', samps_per_chan=number_of_samples_per_channel) 

but as soon as I use one of these two parameters I get the error. Any other ideas????

Pat

 

 

0 Kudos
Message 8 of 12
(11,229 Views)

Hi Brandon,

Just tried one additional thing. if I just put in the parameter values for sample rate=10000, source= u'', active_edge = 10280, sample_mode= 10178, samps_ per_ chan=20 

I still get the same error.

 task.timing.cfg_samp_clk_timing(10000, u'',10280,10178,20) 
0 Kudos
Message 9 of 12
(11,228 Views)
Solution
Accepted by topic author Parto

Oh, I had missed that the active_edge and sample_mode arguments aren't supposed to be bare integers, they're Enums.

 

Try using Edge.RISING and AcquisitionType.FINITE. You may need to add a:

from nidaqmx.constants import AcquisitionType, Edge

 

(For reference, see the ai_multi_task_pxie_ref_clk.py example, which uses cfg_samp_clk_timing with sample_mode=AcquisitionType.CONTINUOUS.)

——
Brandon Streiff
ni.com/compactdaq · ni.com/daq
Message 10 of 12
(11,222 Views)