MATLAB Answers

How to run a while or for loop in a script with NIDAQ Background operations?

3 views (last 30 days)
Edward Willey
Edward Willey on 15 Aug 2017
Answered: Louis Edelman on 6 Mar 2018
I have been attempting to create a script that simultaneously controls serial devices and captures data using NIDAQ Data Acquisition Toolbox. I found that when I start a for or while loop, the background acquisition ceases for the entire duration. Is there a reason for this sort of behavior?
DAQ = daq.createSession('ni');
LC_1 = addAnalogInputChannel(DAQ,'cDAQ2Mod1', 'ai0', 'Bridge' );
LC_2 = addAnalogInputChannel(DAQ,'cDAQ2Mod1', 'ai1', 'Bridge');
LC_1.BridgeMode = 'Full';
LC_2.BridgeMode = 'Full';
LC_1.ADCTimingMode = 'HighSpeed';
LC_2.ADCTimingMode = 'HighSpeed';
range = 0.05;
LC_1.Range = [-range , range];
LC_2.Range = [-range , range];
LC_1.NominalBridgeResistance = 350;
LC_2.NominalBridgeResistance = 350;
DAQ.NotifyWhenDataAvailableExceeds = 1;
DAQ.IsContinuous = true;
DAQ.Rate = 100;
lh = addlistener(DAQ, 'DataAvailable', @plotData);
while 1
%No data is capture here however short or long the loop

Answers (2)

Louis Edelman
Louis Edelman on 6 Mar 2018
You need to write a function that pulls the data out of the DataAvailable event. Instead of @PlotData use something like the script below with daq_trigger, daq_data, and daq_times as global variables in your script.
function background_pull(src, event) %BACKGROUND_PULL Extracts the data acquired in a background DAQ acquisition %and outputs it to the workspace with the session trigger time. % Detailed explanation goes here global daq_times daq_data daq_trigger daq_data = event.Data; % Extract data from structyure daq_trigger = datetime(event.TriggerTime, 'ConvertFrom', 'datenum', 'TimeZone', 'local'); % Extract the first sample time as the trigger time daq_trigger = datetime(daq_trigger, 'TimeZone', 'UTC'); % Convert to UTC to match Camera % Note that for the NI-6143 event.TimeStamps(1) == 0, however this isn't % true for all cards daq_times = event.TimeStamps; % Extract the frame time from the end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!