Info

This question is closed. Reopen it to edit or answer.

How can I prevent DAQ Session-Interface startBackground() from delaying DataAvailable events until end of capture when using NI SensorDaq?

1 view (last 30 days)
Hello,
I'm trying to record from a NI SensorDAQ using the Data Acquisition Toolbox session based interface. I need to collect the data in real-time while executing other code. I am able to record data from the sensor in the foreground. However, using startBackground() functionality, the code progresses but no DataAvailable events are thrown and no scans are acquired until the DurationInSeconds has been reached. Once this occurs, all data is flushed in the expected intervals to the DataAvaiable listener. Below is some example code to help describe the issue.
----------------------------------------------------------
function test()
clc
session = daq.createSession('ni');
session.addAnalogInputChannel('Dev1', 'ai0', 'Voltage');
session.Rate = 10;
session.DurationInSeconds = .5;
t = GetSecs;
session.addlistener('DataAvailable', @(x,y)saveData(x,y,t));
session.NotifyWhenDataAvailableExceeds = 1;
disp(session)
session.startBackground();
while(~session.IsDone)
disp(sprintf('Consumer: Scans Acq %i @ %f',session.ScansAcquired,GetSecs()-t))
pause(.1)
end
disp(sprintf('Consumer: Scans Acq %i @ %f',session.ScansAcquired,GetSecs()-t))
end
function saveData(src,event,t)
disp(sprintf('----Producer: Scans Acq %i @ %f',src.ScansAcquired,GetSecs()-t))
end
---------------------------------------------------------
Output:
Data acquisition session using National Instruments hardware:
Will run for 0.5 seconds (5 scans) at 10 scans/second.
Number of channels: 1
index Type Device Channel MeasurementType Range Name
----- ---- ------ ------- --------------- ---------------- ----
1 ai Dev1 ai0 Voltage (Diff) -20 to +20 Volts
Consumer: Scans Acq 0 @ 0.129536
Consumer: Scans Acq 0 @ 0.232983
Consumer: Scans Acq 0 @ 0.344027
Consumer: Scans Acq 0 @ 0.455027
Consumer: Scans Acq 0 @ 0.564001
----Producer: Scans Acq 1 @ 0.665240
----Producer: Scans Acq 2 @ 0.667769
----Producer: Scans Acq 3 @ 0.669138
----Producer: Scans Acq 4 @ 0.670374
----Producer: Scans Acq 5 @ 0.678697
Consumer: Scans Acq 5 @ 0.679171
Consumer: Scans Acq 5 @ 0.778010
--------------------------------------------
What I'm trying to show with this code is the "Consumer" in main loop polling the session object finding no scans for the full duration of the capture (.5 second). Then the "Producer" is triggered by the listener at the right data sizes, but not the right time. Each producer gets triggered with each new scan as expected, but they all occur between .66 and .68 instead of on time. After finishing, the Consumer sees the correct amount of scans.
How can I get the events to be thrown on-time?

Answers (0)

Community Treasure Hunt

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

Start Hunting!