09-13-2012 12:14 PM
Hello,
I'm using Python to obtain continuous data from my USB 6008 DAQ. I can sucessfully obtain finite samples, but when I change "DAQmx_Val_FiniteSamps" to "DAQmx_Val_ContSamps" in the "DAQmxCfgSampClkTiming" function, my data is read as zeros instead of the actual voltage it should be reading.
Therefore, I have changed back to "DAQmx_Val_FiniteSamps" and tried obtaining continuous data from a while loop. Unfortunately, I can only obtain a maximum of 4 loops worth of voltage values before my program hangs on trying to read data further.
I have altered what functions to loop as well as the sample rate, number of samples to obtain, and the number of channels to obtain data from with no sucess yet.
Below is my example code. Thanks in advance or any help.
import ctypes import numpy import viztask 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() # the 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),buf_size) raise RuntimeError('nidaq call failed with error %d: %s'%(err,repr(buf.value))) #Main Program #def obtain_value(): #used with while loop 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(1000),DAQmx_Val_Rising,DAQmx_Val_ContSamps,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_num_samples,ctypes.byref(read),None)) print ' after read' print data if taskHandle.value != 0: nidaq.DAQmxStopTask(taskHandle) nidaq.DAQmxClearTask(taskHandle) print ' end' #obtain data from a while loop
"""i = 0 while i < 5: i += 1 print i obtain_value()"""
09-14-2012 12:03 PM
Hello mhead10,
It looks like you are creating a task, creating a channel, setting up timing, reading the voltage, stopping the task, and closing the channel all inside the while loop. Ideally, only the read voltage shouls be inside the while loop. I would try moving the task, channel, and timing setup to before the while loop and moving the task and channel close to outside the while loop.
Also, what exactly does your program do when it hangs up? Does it give an error message, does just python freeze, or does your OS freeze?
Regards,
09-17-2012 05:10 PM - edited 09-17-2012 05:21 PM
PattyD,
I revised my code as you suggested. Unfortunately, when using "ContSamps" I get : "'Measurements: Task specified is invalid or does not exist.'" Therefore, I put all the code in the loop to avoid this error. I have yet to learn how to keep the loop as you suggested and not get the error.
As for what happens after the program runs, nothing besides seeing the print statements and the zero voltage values. No errors are given and the program isn't frozen because it still responds. I can continue to work in python an on my OS.
09-18-2012 10:36 AM
Hello mHead10,
Unfortunately, the DAQmx driver isn't supported by Python. Therefore, even if you've used the proper sequence of commands, it's possible that the code will still not execute as it hasn't been fully tested.
That being said, please see the following posts to see examples of progress others have made using Python: