Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

USB6009 Python Control

Hello, 

 

I am attempting to use python (with ctypes) to control my USB-6009.  The goal is to write out an analog voltage and then read in the voltage through an analog input with ability to change the number of points averaged and the number of times this is repeated(sweeps) averaging the analog input array.  The issue is that as we increase the number of times the voltage ramp is repeated (sweeps) we get a time out error (nidaq call failed with error -200284: 'Some or all of the samples requested have not yet been acquired).  This has us confused because the sweeps are in a larger loop and the Daq function should be the same if there are 10 sweeps (works) or 1000 sweeps (crashes).  Any insight would be greatly appreciated.  I have included the code below for reference.  

 

import ctypes

import numpy

from time import *

from operator import add

nidaq = ctypes.windll.nicaiu # load the DLL

##############################

# Setup some typedefs and constants

# to correspond with values in

# C:\Program Files\National Instruments\NI-DAQ\DAQmx ANSI C Dev\include\NIDAQmx.h

# Scan Settings

aoDevice = "Dev2/ao0"

aiDevice = "Dev2/ai0"

NumAvgPts = 10

NumSweeps = 50

NumSpecPts = 100

filename = '12Feb15_CO2 Test_12.txt'

 

Readrate = 40000.0

Samplerate = 1000

StartVolt = 0.01

FinalVolt = 1.01

voltInc = (FinalVolt - StartVolt)/NumSpecPts

 

# the typedefs

int32 = ctypes.c_long

uInt32 = ctypes.c_ulong

uInt64 = ctypes.c_ulonglong

float64 = ctypes.c_double

TaskHandle = uInt32

# the constants

DAQmx_Val_Cfg_Default = int32(-1)

DAQmx_Val_Volts = 10348

DAQmx_Val_Rising = 10280

DAQmx_Val_FiniteSamps = 10178

DAQmx_Val_GroupByChannel = 0

##############################

 

def CHK_ao( err 😞

"""a simple error checking routine"""

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)))

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 generated warning %d: %s'%(err,repr(buf.value)))

 

def CHK_ai(err):

"""a simple error checking routine"""

if err < 0:

buf_size = NumAvgPts*10

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)))

def Analog_Output():

taskHandle = TaskHandle(0)

(nidaq.DAQmxCreateTask("",ctypes.byref(taskHandle )))

(nidaq.DAQmxCreateAOVoltageChan(taskHandle,

aoDevice,

"",

float64(0),

float64(5),

DAQmx_Val_Volts,

None))

'''

(nidaq.DAQmxCfgSampClkTiming(taskHandle,"",float64(Samplerate),

DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,

uInt64(NumAvgPts))); # means we could turn in this to continuously ramping and reading

'''

(nidaq.DAQmxStartTask(taskHandle))

 

(nidaq.DAQmxWriteAnalogScalarF64(taskHandle, True, float64(10.0), float64(CurrentVolt), None))

nidaq.DAQmxStopTask( taskHandle )

nidaq.DAQmxClearTask( taskHandle )

 

def Analog_Input():

global average

# initialize variables

taskHandle = TaskHandle(0)

data = numpy.zeros((NumAvgPts,),dtype=numpy.float64)

# now, on with the program

CHK_ai(nidaq.DAQmxCreateTask("",ctypes.byref(taskHandle)))

CHK_ai(nidaq.DAQmxCreateAIVoltageChan(taskHandle,aiDevice,"",

DAQmx_Val_Cfg_Default,

float64(-10.0),float64(10.0),

DAQmx_Val_Volts,None))

CHK_ai(nidaq.DAQmxCfgSampClkTiming(taskHandle,"",float64(Readrate),

DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,

uInt64(NumAvgPts)));

CHK_ai(nidaq.DAQmxStartTask(taskHandle))

read = int32()

CHK_ai(nidaq.DAQmxReadAnalogF64(taskHandle,NumAvgPts,float64(10.0),

DAQmx_Val_GroupByChannel,data.ctypes.data,

NumAvgPts,ctypes.byref(read),None))

 

#print "Acquired %d points"%(read.value)

if taskHandle.value != 0:

nidaq.DAQmxStopTask(taskHandle)

nidaq.DAQmxClearTask(taskHandle)

average = sum(data)/data.size

 

Thanks, 

Erin

0 Kudos
Message 1 of 2
(4,815 Views)
Hi Erin,
 
Are you receiving this error in the first execution or subsequent executions of your data acquisition loop? To note, the error you are receiving is essentially a timeout error. It could simply mean that you are not waiting long enough before sampling. You may want to consider adding a wait step or master/slave synchronization. For additional reference, the following forum thread addresses a similar issue:
 
NI Community Forums
 
Cheers,
Q
0 Kudos
Message 2 of 2
(4,769 Views)