Callback function for frequency input channel from NI DAQ using Data Acquisition Toolbox
6 次查看(过去 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;
start(dq, "continuous");
pause(12) % pausing to see how well it works
stop(dq)
% Callback function
function dispMyData(obj, ~)
data = read(obj, 1, "OutputFormat", "Matrix"); % This read works w/out a clock in a loop or code normally
disp(data)
end
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.
0 个评论
回答(1 个)
Suman Sahu
2023-5-15
Hi Marzena,
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;
start(dq, "continuous");
pause(12);
stop(dq);
function dispMyData(src, evt, dq)
data = read(dq, 1, "OutputFormat", "Matrix");
disp(data);
end
To learn more about data acquisition using DAT, refer the following documentation: Interface to data acquisition device - MATLAB
Hope it is helpful!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simultaneous and Synchronized Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!