How can I acquire data in the background while writing data with a NI DAQ?

21 次查看(过去 30 天)
Hi All,
I am interested in using a NI DAQ device for two main uses:
1) Send digital outputs at certain times to turn external solenoids on or off
2) Read voltage inputs in the background while I'm sending the digital outputs.
The code that I wrote works perfectly well to send the digital outputs, but fails when I try to read any voltage inputs. My suspicion is that matlab is getting confused as to which channels I'm using to record and which I am using to write, but I am not sure how (or if) I can specify which channels to read from and which to write from.
Below is the code that I have been working with:
%% Section 1: Establish device
d_list=daqlist("ni");
dq=daq("ni");
% We are going to use "Dev1" (USB-6211)
daqinfo=d_list{1, "DeviceInfo"};
dq.Rate=1000;
%% Section 2: Add inputs and outputs
ch0=addoutput(dq, "Dev1","port1/line0","Digital");
ch1=addoutput(dq, "Dev1","port1/line1","Digital");
ch2=addoutput(dq, "Dev1","port1/line2","Digital");
ch3=addoutput(dq, "Dev1","port1/line3","Digital");
[ch4,idx]=addinput(dq,"Dev1","ai0","Voltage");
% Establish Function for continuous background acquisition (function is defined below)
dq.ScansAvailableFcn = @(src,evt) reportWhenEqualsOrExceedsOneV(src, evt);
dq.ScansAvailableFcnCount = "auto";
% Start bacground acquisition
start(dq, "continuous");
%% Section 3: Write digital outputs (this is a simpler example of the code I'm running)
log=[];
for ii=1:20
write(dq,[0 0 1 0]) % Write tone 1
pause(.250)
write(dq,[0 1 0 0]) % Write tone 2
pause(.250)
write(dq,[0 0 0 0]) % End tones
log(ii)=ii;
end
% End background acquisition
stop(dq)
%% Section 4: Function section
function reportWhenEqualsOrExceedsOneV(src, ~)
[data, timestamps, ~] = read(src, src.ScansAvailableFcnCount, "OutputFormat", "Matrix");
if any(data >= 1.0)
% disp('Detected lick exceeding 1V')
timelog=timestamps;
datalog=data;
end
end
  3 个评论
Deryn LeDuke
Deryn LeDuke 2025-2-4,15:31
Hey Nubia and Arnaud,
I was able to find a workaround to establish a parallel processing stream via MATLAB which collects NIDAQ data, but that does take up a lot of memory. The workaround that I did which is a little silly is to ask MATLAB to trigger a secondary collection script that I wrote via python, which collects serial monitor and camera information simultaneously with NIDAQ collection. I use physical voltage signals to sync these two data streams (one shared input between serial monitor and NIDAQ and camera triggers for frames). Because python uses serial communication, I can send an output, trigger the experiment, and then quickly reopen the connection to recieve inputs.
Other than using I2C communication (which would require a significant change to my circuitry, but might work for your circumstance), using this dual-language/parallel processing approach has worked well.
Hope this helps! I am happy to clarify if needed.

请先登录,再进行评论。

回答(0 个)

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by