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

7 次查看(过去 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
  1 个评论
Nubia Mendes
Nubia Mendes 2022-3-16
Hi Deryn, have you been able to solve this? I'm have the exact same issue where I'd like to read force signals continuously and write voltage to the DAQ under certain conditions.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Acquisition Toolbox Supported Hardware 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by