Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DAQ using callback function #59

Open
JE-Suh opened this issue Mar 25, 2020 · 1 comment
Open

DAQ using callback function #59

JE-Suh opened this issue Mar 25, 2020 · 1 comment

Comments

@JE-Suh
Copy link

JE-Suh commented Mar 25, 2020

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
Copy link
Author

JE-Suh commented Mar 25, 2020

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant