Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Python Solution to Multi Channel Analog Reads using USB 6008

Solution for the Python people here. ..as support is a bit vague to non-existent Smiley Mad
 
Nidaq  with USB 6008
Reading more that 1 channel with the NIDAQ library in Python was a problem as the docs are really bad. Anyway I found a solution to do muli channel reads
My example  is with 3 channels so it could be easily adapted for more. 
 
I kept the max_num_samples deliberately small to see what was happening 🙂 so scale up
 
Enjoy  Smiley Happy
 
 
 
        # initialize variables
        taskHandle = TaskHandle(0)
        max_num_samples =9
        data = numpy.zeros((max_num_samples,),dtype=numpy.float64)   
        # now, on with the program
        CHK(nidaq.DAQmxCreateTask("",ctypes.byref(taskHandle)))
        CHK(nidaq.DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0:2","",          
                                           DAQmx_Val_Diff,            #DAQmx_Val_Diff,   #DAQmx_Val_RSE,       #DAQmx_Val_Cfg_Default,
                                           float64(-10.0),float64(10.0),
                                           DAQmx_Val_Volts,
                                           None))
       
               
        CHK(nidaq.DAQmxCfgSampClkTiming(taskHandle,"",float64(1000.0), DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,uInt64(max_num_samples)))
                  #DAQmxCfgSampClkTiming(taskHandle,"",sampleRate,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,sampsPerChan);      
        CHK(nidaq.DAQmxStartTask(taskHandle))
        read = int32()
       
        CHK(nidaq.DAQmxReadAnalogF64(taskHandle,
                                     -1,
                                     float64(1.0),    #Timeout in seconds
                                     DAQmx_Val_GroupByScanNumber,       #DAQmx_Val_GroupByChannel,    #DAQmx_Val_GroupByScanNumber
                                     data.ctypes.data,
                                     max_num_samples,
                                     ctypes.byref(read),None))
       
       
        print "Acquired %d points"%(read.value)
       
        if taskHandle.value != 0:
            nidaq.DAQmxStopTask(taskHandle)
            nidaq.DAQmxClearTask(taskHandle)
       
        return data
Message 1 of 5
(9,038 Views)

Hi could you help me please, after defining the channel how you can read and save the data for each inputs?

DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,

"Dev1/ai8:9","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));

I need to read and save the inputs separately ,for example ai8 ,ai9 inputs in the SQL DB

0 Kudos
Message 2 of 5
(7,076 Views)

Hi darya,

 

for C/C++ programming of DAQmx you might consider to have a look at this tutorial: Here

 

Best regards,

 

Benjamin

0 Kudos
Message 3 of 5
(7,025 Views)

Hy i try to use a USB 6008 with 8 channel but it doesn't work i used your model but it works for only one channel. when i change the Group BY Channel with Group by Scan Number it returns me 0 . And when i want to save the data i can save only from one channel. Can you help me?

My code is :

from PyDAQmx import *
import numpy
from time import *
from xlwt import Workbook
import csv
# Declaration of variable passed by reference
taskHandle = TaskHandle()
read = int32()
data = numpy.zeros((2000,), dtype=numpy.float64)
#sampling rate
sampleRate = float64(100.0)
samplesPerChan = uInt64(100)

#specifiy the channels
chan = "Dev1/ai0:1"
clockSource = "OnboardClock"
print chan
print clockSource

try:
# DAQmx Configure Code
DAQmxCreateTask("",byref(taskHandle))
DAQmxCreateAIVoltageChan(taskHandle,chan,"",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,None)
DAQmxCfgSampClkTiming(taskHandle,clockSource,sampleRate,DAQmx_Val_Rising,DAQmx_Val_ContSamps,samplesPerChan)

# DAQmx Start Code
DAQmxStartTask(taskHandle)

# DAQmx Read Code
DAQmxReadAnalogF64(taskHandle,-1,float64(1.0),DAQmx_Val_GroupByScanNumber,data,2000,byref(read),None)

print "Acquired %d points"%read.value
except DAQError as err:
print "DAQmx Error: %s"%err
finally:
if taskHandle:
# DAQmx Stop Code
DAQmxStopTask(taskHandle)
DAQmxClearTask(taskHandle)
timestamp=strftime("%Y-%m-%d %H:%M:%S")
ofile=open('eggs.csv', 'wb') # as csvfile:
spamwriter = csv.writer(ofile, delimiter=',',quotechar='"', quoting=csv.QUOTE_MINIMAL)
print data
for x in data:
spamwriter.writerow([x]+[timestamp])
ofile.close()

 

0 Kudos
Message 4 of 5
(5,740 Views)

Hello Stefan,

I wrote this about 8 years ago so please forgive me as I'm working from memory 🙂

 

for 8 channels you need (on Device 1)
CHK(nidaq.DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0:7","",

leave the Groupby as is 

I would also check that you are really sending data on all channels.

Hope it helps

 

Best regards 

 

Daleflow

0 Kudos
Message 5 of 5
(5,735 Views)