clade / PyDAQmx

Interface to National Instrument NIDAQmx driver
Other
133 stars 54 forks source link

DAQ using callback function #59

Open JE-Suh opened 4 years ago

JE-Suh commented 4 years ago

Hello, I'm trying to take data using callback method, and found PyDAQmx. I converted PyDAQmx 1.4.3 using 2to3 and installed to use it on python 3.7.6 (32bit), windows 10, NIDAQ USB-6212, NIDAQmx 19.6

I wrote code as below using PyDAQmx but it gives an error.

from PyDAQmx import *
from PyDAQmx.DAQmxCallBack import *

class MyList(list):
    pass

def EveryNCallback_py(taskHandle, eventType, nSamples, callbackData_ptr):
    callbackData = get_callbackData_from_id(callbackData_ptr)
    read = uInt32()

    nidaq.DAQmxReadAnalogF64(AItaskHandle,samples_per_chan,float64(-1),DAQmx_Val_GroupByScanNumber,AIdata.ctypes.data,Length,ctypes.byref(read),None)

    if(read>0):
        print('Acquired %d samples.' %read)
    return 0

EveryNCallback = pydaqmx.DAQmxEveryNSamplesEventCallbackPtr(EveryNCallback_py)

def some_func(num): 

    ... # some other code including import, declare variables like DAQmx_Val_Acquired_Into_Buffer, etc...

    AIdata = MyList()
    AIdata = numpy.zeros( ( max_num_samples, ), dtype=numpy.float64 )
    AIdata_dummy = numpy.zeros( ( max_num_samples, ), dtype=numpy.float64 )
    Length = AIdata.size
    samples_per_chan = round(max_num_samples/2)
    id_a = create_callbackdata_id(AIdata)

    AItaskHandle = TaskHandle( 0 )

    nidaq.DAQmxCreateTask("",ctypes.byref(AItaskHandle))
    nidaq.DAQmxCreateAIVoltageChan(AItaskHandle,b"Dev%d/ai0,Dev%d/ai2" %(num,num),"",DAQmx_Val_Diff,float64(-5.0),float64(5.0),DAQmx_Val_Volts,None)
    nidaq.DAQmxCfgSampClkTiming(AItaskHandle,"",float64(sampleRate),DAQmx_Val_Rising,DAQmx_Val_ContSamps,uInt64(max_num_samples))
    nidaq.DAQmxSetDigLvlPauseTrigSrc(AItaskHandle, b"/Dev%d/PFI0" %num)
    nidaq.DAQmxSetPauseTrigType(AItaskHandle, DAQmx_Val_DigLvl)
    nidaq.DAQmxSetDigLvlPauseTrigWhen(AItaskHandle, DAQmx_Val_High)

    DAQmxRegisterEveryNSamplesEvent(AItaskHandle,DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,id_a)
    nidaq.DAQmxStartTask( AItaskHandle )

And if I run this code, it gives me the following error.

Traceback (most recent call last):
  File ~~~, line 118, in some_func
    pydaqmx.DAQmxRegisterEveryNSamplesEvent(AItaskHandle,DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,id_a)
  File "<string>", line 2, in function
  File "C:\Users\JungEun.Suh\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pydaqmx-1.4.3-py3.7.egg\PyDAQmx\DAQmxFunctions.py", line 57, in mafunction
    error = f(*arg)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type

Do you have any idea what's wrong here? Please let me know if you need any further information.

Thank you in advance!

JE-Suh commented 4 years ago

I solved it by replacing DAQmxRegisterEveryNSamplesEvent(AItaskHandle,DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,id_a) by nidaq.DAQmxRegisterEveryNSamplesEvent(AItaskHandle,DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,id_a)

where nidaq = ctypes.windll.nicaiu (I installed NIDAQmx 19.6)