Callback function for frequency input channel from NI DAQ using Data Acquisition Toolbox
9 次查看（过去 30 天）
Hello, I am trying to create a callback function for my NI 9422 DAQ which I'm using to read frequency input. I've added my DAQ and channels as below, and tried both with and without an external clock.
dq = daq("ni");
b1 = addinput(dq, "cDAQ2Mod2", "ctr1", "Frequency"); %input edge detection of pulses
b2 = addinput(dq, "cDAQ2Mod2", "ctr2", "Frequency"); %input edge detection of pulses
I then try to use the following callback function to display read outputs
dq.ScansAvailableFcn = @dispMyData;
pause(12) % pausing to see how well it works
% Callback function
function dispMyData(obj, ~)
data = read(obj, 1, "OutputFormat", "Matrix"); % This read works w/out a clock in a loop or code normally
From this code, my function dispMyData is never even called. How do I induce the callback? I've done similar code for an analog input, is the frequency different somehow?
Thank you for any help.
Suman Sahu 2023-5-15
While acquiring data using the Data Acquisition Toolbox, the ScansAvailableFcn property is assigned a callback function as a function handle which is triggered every time after the number of scans specified by the ScansAvailableFcnCount property is available from the input channels.
Dq.ScansAvailableFcn = @(src, evt)callback(src, evt, arg1, arg2,…argn);
Dq.ScansAvailableFcnCount = numberOfScansAfterWhichCallbackIsTriggered;
Try making following changes to your code:
dq.ScansAvailableFcn = @(src, evt)dispMyData(src, evt, dq);
dq.ScansAvailableFcnCount = 50;
function dispMyData(src, evt, dq)
data = read(dq, 1, "OutputFormat", "Matrix");
To learn more about data acquisition using DAT, refer the following documentation: Interface to data acquisition device - MATLAB
Hope it is helpful!