Generating Digital Outputs and Scanning Analog inputs via Data Acquisition Toolbox

7 views (last 30 days)
Hello All,
I am using an NI USB-6259 to collect two channels of analog data. Based upon some resulting analysis of the data gathered from those channels, I want to send a digital output along one of the DIO lines based upon some threshold.
I first worked out all of the analog input data collection and have it working fine. But the moment I add a digital output to the code I am faced with a number of errors.
%SETUP NI DEVICE
s=daq.createSession('ni');
ch1 = addAnalogInputChannel(s,'Dev1','ai0','Voltage');
ch1.TerminalConfig = 'SingleEnded';
ch2 = addAnalogInputChannel(s,'Dev1','ai8','Voltage');
ch2.TerminalConfig = 'SingleEnded';
%Offending line of code
ch3 = addDigitalChannel(s,'Dev1','port0/line0','OutputOnly');
s.rate=500000;
EndTime=10;
FrameTime=0.1;
s.DurationInSeconds = EndTime;
s.NotifyWhenDataAvailableExceeds = round(s.Rate*FrameTime);
s.IsContinuous = false;
lh = addlistener(s,'DataAvailable', @(src,event) DataAnalysis(src,event,));
%start ACQ
s.startBackground();
s.wait();
delete(lh);
Without ch3 this code works fine. Once I add an output channel the s.DurationInSeconds becomes a read-only register and I get hit with this error:
DurationInSeconds cannot be set when there are output channels in the session. You must
specify or change the amount of data queued to change the number of scans acquired.
The session has a number of other property values that I can't modify because they are read-only such as s.NumberOfScans and s.ScansQueued.
When I move the ch3 initialization to right before the listener call I get this error:
There must be data queued in the session before this function can be called.
The thing is I don't know what command to call or register to modify in order to queue the correct amount of data based upon my EndTime and FrameTime. It seems the registers I would modify in order to do this (s.DurationInSeconds or s.NumberofScans or s.ScansQueued) are all read only. What is the correct way to initialize an analog input scan while there is a digital output initialized?
Thanks much for your help
-Joe

Answers (1)

Mentari Putri
Mentari Putri on 12 Feb 2020
Hi Joe,
Did you find the solution of your problem? I've the same problem.

Community Treasure Hunt

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

Start Hunting!