Data acquisition read "all" does not work properly

I use a National Instruments USB-6009 daq box for a long time now. For a new project I use Matlab R2025b instead of previous versions. In a loop I want to use the read function with the "all" option but no data is stored. If I interspere calls with a span of 1 everything works. Why? Here my code:
%% Prepare data acquisition
dq = daq('ni');
% Add a channel to the data acquisition object
Channel = addinput(dq, 'Dev1', 1:4, 'Voltage');
for i = 1 : 4
Channel(i).Range = [-10, 10];
Channel(i).TerminalConfig = 'SingleEnded';
end
dq.Rate = 250;
%% data acquisition loop
Results = zeros(5000, 5); Start = 0;
dq.flush;
start(dq, 'Duration', seconds(20));
while dq.Running
pause(0.2);
Span = dq.NumScansAvailable;
if Span == 0
Span = 1;
else
Span = "all";
end
[data, TimeStamps] = read(dq, Span, "OutputFormat", "Matrix");
% Store the acquired data in the Results array
if ~isempty(data)
Results(Start + 1 : Start + size(data, 1), :) = [TimeStamps, data];
Start = Start + size(data, 1);
end
end
% Stop the data acquisition
stop(dq);
Results = Results(1 : Start, :);
I have all updates installed for Matlab R2025b, the current version of the Data Acquisition Toolbox Support Package for National Instruments NI-DAQmx Devices and the appropriate NI-DAQmx driver. The code works but I do not understand why no data is retrieved with a permanent Span setting to "all".
Greetings Andreas

2 个评论

I presume you have verified that prior release still functions as before?
Is the problem that
Span = dq.NumScansAvailable;
is now never returning anything but "0" or that the "all" case never returns any data or does it also work if there is a single scan returned first? Does a single scan reset the NumScansAvailable value to zero?
Running in a loop dq.NumScansAvailable always returns 0 and the "all" case returns no data. Although dq.NumScansAvailable returns 0 a single scan returns data and the next call of dq.NumScansAvailable reports the correct number of data available, i.e., a read call with the "all" option returns all data available.
On the next call of dq.NumScansAvailable again I get 0 data available but single scan read returns data.
> I presume you have verified that prior release still functions as before?
Really hard to do. Last time it was on WinXP and I don't have these systems around here. It would take quite a long time to setup an old system again...

请先登录,再进行评论。

 采纳的回答

Google AI recognized the symptoms ... the whole conversation was much longer before all the pertinent details had been provided, but the end answer appears plausible...
This highly specific behavior—where NumScansAvailable stays locked at 0 and read(dq, "all") yields nothing unless you force a single-scan read first—is a classic symptom of USB buffer starvation and driver flushing mechanics unique to the legacy hardware architecture of the NI USB-6009. [1, 2, 3]
The National Instruments DAQmx driver defaults to requesting a 32 KB block of data from USB DAQ devices before it publishes anything to the OS/MATLAB memory buffer. The USB-6009 is a full-speed device with a small onboard FIFO buffer. If your scan rate is low or your loop executes too quickly, it takes a long time to hit that 32 KB threshold natively. [1]
When you execute a single-scan read (like read(dq, 1)), MATLAB forces a low-level driver command that requests a USB "short packet". This request manually flushes whatever data is currently sitting on the hardware's onboard FIFO across the USB bus and into the MATLAB buffer, suddenly making NumScansAvailable spike above 0. [1]
The cleanest ways to resolve this buffer lock include:
Fix 1: Adjust the ScansAvailableFcnCount (Recommended)
By default, the modern MATLAB daq interface waits for a large bulk packet before it registers scans as "available" to the buffer. You can force MATLAB to update its buffer pool much more frequently by configuring how often it processes incoming data packets. [1]
Change your setup script to explicitly minimize this count:
matlab
dq = daq("ni");
addinput(dq,
"Dev1", "ai0", "Voltage");
dq.Rate =
1000; % Example rate
dq.Continuous = true;
% FORCE MATLAB to update the buffer every 10 samples
% This actively requests short USB packets so NumScansAvailable updates
dq.ScansAvailableFcnCount = 10;
Good luck...

更多回答(0 个)

产品

版本

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by