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

7 ビュー (過去 30 日間)
Edward Willey
Edward Willey 2017 年 8 月 15 日
回答済み: Louis Edelman 2018 年 3 月 6 日
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?
Example:
%%NI DAQ
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);
DAQ.startBackground;
pause(10);
while 1
%No data is capture here however short or long the loop
end

回答 (2 件)

Nicholas Jackson
Nicholas Jackson 2018 年 2 月 21 日
Hi Edward, I'm having a similar issue. Did you ever get this resolved? Thanks

Louis Edelman
Louis Edelman 2018 年 3 月 6 日
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!

Translated by