2013-02-27 56 views
0

我試圖讀出兩個通道同時如果一個USB 6211與Python。爲此,我試圖改變這一行閱讀兩個國家儀器的USB 6211與Python通道

CHK(nidaq.DAQmxCreateAIVoltageChan(
     taskHandle, 
     "Dev1/ai0", 
     "", 
     DAQmx_Val_Cfg_Default, 
     float64(-10.0), 
     float64(10.0), 
     DAQmx_Val_Volts, 
     None)) 

http://www.scipy.org/Cookbook/Data_Acquisition_with_NIDAQmx適應的例子

CHK(nidaq.DAQmxCreateAIVoltageChan(
    taskHandle, 
    "Dev1/ai0:1", 
    "", 
    DAQmx_Val_Cfg_Default, 
    float64(-10.0), 
    float64(10.0), 
    DAQmx_Val_Volts, 
    None)) 

但後來,我不斷收到該「NIDAQ調用失敗,出現錯誤-200229錯誤信息: '緩衝區太小,不適合讀取數據「。增加行CHK(nidaq.DAQmxCfgInputBuffer(taskHandle, uInt32(10000000)))或增加數據數組的長度並沒有幫助...

有人可以指向我的權利變量更改?

回答

2

我發現這裏的答案:http://www.physics.oregonstate.edu/~hetheriw/whiki/py/topics/ni/files/ni-daq_ctypes_multichannel_adc_usb_6008.txt

總之,nidaq.DAQmxReadAnalogF64()的參數需要額外的參數 「-1」 taskHandle後。該線應該看起來像這樣:

CHK(nidaq.DAQmxReadAnalogF64(taskHandle, -1,float64(1.0), 
    DAQmx_Val_GroupByScanNumber,#DAQmx_Val_GroupByChannel,#DAQmx_Val_GroupByScanNumber 
    data.ctypes.data,max_num_samples, 
    ctypes.byref(read),None)) 
1

這裏是我用來做一個對象與USB-6009的對象。注意:底部是調用過程的一個例子。

#------------------------------------------------------------------------------- 
# Name:  This is a object that takes data from the AtoD board 
# Purpose: 
# 
# Author:  Carl Houtman 
# 
# Created:  12/10/2012 
# Copyright: (c) Carl Houtman 2012 
# Licence:  none 
#------------------------------------------------------------------------------- 
from PyDAQmx import * 
import numpy 

class DAQInput: 

    def __init__(self, num_data, num_chan, channel, high, low): 
     """ This is init function that opens the channel""" 
     # Declare variables passed by reference 
     taskHandle = TaskHandle() 
     read = int32() 
     data = numpy.zeros((10000,),dtype=numpy.float64) 
     sumi = [0,0,0,0,0,0,0,0,0,0] 

     #Get the passed variables 
     self.num_data = num_data 
     self.channel = channel 
     self.high = high 
     self.low = low 
     self.num_chan = num_chan 

     # Create a task and configure a channel 
     DAQmxCreateTask(b"",byref(self.taskHandle)) 
     DAQmxCreateAIVoltageChan(self.taskHandle,self.channel,b"",DAQmx_Val_Cfg_Default, 
           self.low,self.high,DAQmx_Val_Volts,None) 
     # Start the task 
     DAQmxStartTask(self.taskHandle) 

    def getData(self): 
     """ This function gets the data from the board and calculates the average""" 
     DAQmxReadAnalogF64(self.taskHandle,self.num_data,10.0,DAQmx_Val_GroupByChannel, 
          self.data,10000,byref(self.read),None) 

     # Calculate the average of the values in data (could be several channels) 
     i = self.read.value 
     for j in range(self.num_chan): 
      self.sumi[j] = numpy.sum(self.data[j*i:(j+1)*i])/self.read.value 
     return self.sumi 

    def killTask(self): 
     """ This function kills the tasks""" 
     # If the task is still alive kill it 
     if self.taskHandle != 0: 
      DAQmxStopTask(self.taskHandle) 
      DAQmxClearTask(self.taskHandle) 

if __name__ == '__main__': 
    myDaq = DAQInput(100, 2, b"Dev1/ai0:1", 10.0, -10.0) 
    result = myDaq.getData() 
    print ("the average readings were {:.4f} and {:.4f} volts".format(result[0], result[1])) 
    myDaq.killTask()