PDA

View Full Version : Continuous DAQ with NI USB 6008 and DAQmx


mhead10
09-11-2012, 02:59 PM
Does anyone have any experience with continuous data acquisition using a NI-DAQ using the ctypes module to access the NI-DAQ family of libraries for NI instruments? I'm trying to use the "cookbook article" from here (http://physics.oregonstate.edu/~hetheriw/whiki/py/sys/16.php) from SciPy which translates a sample C program for analog signal acquisition into Python.

I changed it to sample two channels continuosly by setting Dev1/ai0" to Dev1/ai0:1".

I also then changed the clock timing to continuous samples DAQmx_Val_ContSamps.

I can obtain finite samples fine, however, if I change to "ContSamps", then my data values are read as zero.

If I go back to "FiniteSamps", and then use a timer, I typically only get one or two repeated data arrays, and then nothing else happens. Typically nothing else will happen after the "after stark task" print statement.

I will include all of my code in the hopes that others can benefit off of it because I think this would be a common task, and I have not found any similar complete code elsewhere.

import ctypes
import numpy
nidaq = ctypes.windll.nicaiu # load the DLL
viz.go()

int32 = ctypes.c_long
uInt32 = ctypes.c_ulong
uInt64 = ctypes.c_ulonglong
float64 = ctypes.c_double
TaskHandle = uInt32
read = int32()

# constants
DAQmx_Val_Cfg_Default = int32(-1)
DAQmx_Val_Volts = 10348
DAQmx_Val_Rising = 10280
DAQmx_Val_FiniteSamps = 10178
DAQmx_Val_ContSamps = 10123
DAQmx_Val_GroupByChannel = 0
DAQmx_Val_GroupByScanNumber= 1 #interleaved

# initialize variables
taskHandle = TaskHandle(0)
max_num_samples = 2
data = numpy.zeros((max_num_samples,),dtype=numpy.float64 )

print 'before error check'
def CHK(err):
if err < 0:
buf_size = 100
buf = ctypes.create_string_buffer('\000' * buf_size)
nidaq.DAQmxGetErrorString(err,ctypes.byref(buf),bu f_size)
raise RuntimeError('nidaq call failed with error %d: %s'%(err,repr(buf.value)))

#def obtain_value():
#Main Program
print 'entered main program'
CHK(nidaq.DAQmxCreateTask("",ctypes.byref(taskHandle)))
print 'after create task'
CHK(nidaq.DAQmxCreateAIVoltageChan(taskHandle,"Dev2/ai0:1","",DAQmx_Val_Cfg_Default,float64(-13.0),float64(13.0),DAQmx_Val_Volts,None))
print 'after create voltage'
CHK(nidaq.DAQmxCfgSampClkTiming(taskHandle,"",float64(2),DAQmx_Val_Rising,DAQmx_Val_ContSamps,u Int64(max_num_samples))); #(samples/sec/channel) #ContSamps
print 'after clk'
CHK(nidaq.DAQmxStartTask(taskHandle))
print 'after start task'
CHK(nidaq.DAQmxReadAnalogF64(taskHandle,-1,float64(-1),DAQmx_Val_GroupByScanNumber,data.ctypes.data,ma x_num_samples,ctypes.byref(read),None))
print 'after read'
print data
if taskHandle.value != 0:
nidaq.DAQmxStopTask(taskHandle)
nidaq.DAQmxClearTask(taskHandle)
print 'after main program'

#vizact.ontimer(1,obtain_value)

print 'end of loop'

mhead10
09-13-2012, 10:49 AM
If I run a while loop (to obtain my DAQ data), the maximum number of times the loop will run is 4. After 4 times, "after start task" is displayed, but then nothing else. Therefore, it seems every time the loops gets stuck on the read function.

Any ideas?

I continue to move around what's inside of the function obtain_value, as well as change # of samples and sampling frequency.

Here is the code I'm referring to:

def obtain_value():
print 'entered main program'
CHK(nidaq.DAQmxCreateTask("",ctypes.byref(taskHandle)))
print ' after create task'
CHK(nidaq.DAQmxCreateAIVoltageChan(taskHandle,"Dev2/ai0:1","",DAQmx_Val_Cfg_Default,float64(-13.0),float64(13.0),DAQmx_Val_Volts,None))
print ' after create voltage'
CHK(nidaq.DAQmxCfgSampClkTiming(taskHandle,"",float64(50),DAQmx_Val_Rising,DAQmx_Val_FiniteSamp s,uInt64(max_num_samples))); #(samples/sec/channel) #ContSamps
print ' after clk'
CHK(nidaq.DAQmxStartTask(taskHandle))
print ' after start task'
CHK(nidaq.DAQmxReadAnalogF64(taskHandle,-1,float64(-1),DAQmx_Val_GroupByChannel,data.ctypes.data,max_n um_samples,ctypes.byref(read),None))
print ' after read'
print data
if taskHandle.value != 0:
nidaq.DAQmxStopTask(taskHandle)
nidaq.DAQmxClearTask(taskHandle)
print ' end'

i = 0
while i < 5:
i += 1
print i
obtain_value()

mhead10
09-19-2012, 10:29 AM
I found I could obtain continuous sampling after I downloaded the newest NI MAX driver from NI. Furthermore, I discovered NI I/O (previously called NI Spy). This is an invaluable tool for obtaining real-time information of what your DAQ is doing.