NIDAQ continuous and background acquisition

60 views (last 30 days)
Hello everybody,
I'm trying to acquire and store data from a CompactDAQ from National Instruments. The configuration of the device itselt and the visualization of the data is pretty straightforward, but I'm having trouble in store this data in a matrix that I will be able to access after the acquisition is over.
I'm connecting the device as follows:
s=daq.createSession('ni');
s.addAnalogInputChannel('cDAQ1Mod2', 0, 'voltage');
s.Rate=2000;
s.DurationInSeconds = 60;
to plot the data in real time I use the following lines of code:
lh = s.addlistener('DataAvailable', @plotData);
function plotData(src,event)
plot(event.TimeStamps, event.Data)
end
and then I start it in background (I need to acquire data in background in order to perform other tasks at the same time):
s.startBackground();
This seems to work fine for the real time problem, but I'm having trouble to define a callback function that will store all data acquired into a matrix or structure. I'm sure that this is a quite basic problem.
I want to thank you all in advance for your time.
  4 Comments
Walter Roberson
Walter Roberson on 15 Jun 2018
Or put in a brief pause() in the while loop so that there is an opportunity for background code to run and events to be processed.

Sign in to comment.

Accepted Answer

Chirag Gupta
Chirag Gupta on 26 Apr 2011
Hi Nuno,
There are number of ways to achieve this:
1) Use some global data
function acquireData
global data
s = daq.createSession('ni');
s.addAnalogInputChannel('cDAQ1Mod1',0,'voltage')
s.Rate = 2000
s.DurationInSeconds = 5
lh = s.addlistener('DataAvailable',@plotData);
s.startBackground();
% Do something
while(~s.IsDone)
end
close(gcf);
plot(data); % plot global data
function plotData(src,event)
persistent tempData;
global data
if(isempty(tempData))
tempData = [];
end
plot(event.TimeStamps, event.Data)
tempData = [tempData;event.Data];
data = tempData;
end
2) Use NotifyWhenDataAvailableExceeds property (This decides when 'DataAvailable' is called. So if you don't want a live plot then this is suitable. Just set this to s.Rate * s.DurationInSeconds.
and have a callback for DataAvailable as:
function collectData(src,event)
time = event.TimeStamps;
data = event.Data;
plot(time,data)
save log.mat data time
disp(Background Acquisition complete);
end
This should save the data to a matfile.
3) Write Data to a file:
function acquireData
s = daq.createSession('ni');
s.addAnalogInputChannel('cDAQ1Mod1',0,'voltage')
s.Rate = 2000
s.DurationInSeconds = 5
fid = fopen('log.csv','w');
lh = s.addlistener('DataAvailable',@(src,event)saveData(fid,event));
s.startBackground();
while(~s.IsDone)
end
function saveData(fid,event)
time = event.TimeStamps;
data = event.Data;
plot(time,data)
fprintf(fid,'%f,%f\n',time,data)
end
  5 Comments
Alexandre Piccini
Alexandre Piccini on 26 Sep 2018
Hey,
I come from the future just to thank you for this. Just what I was looking for :)
Greetings

Sign in to comment.

More Answers (4)

Nuno Martins
Nuno Martins on 27 Apr 2011
Thank you very much Chirag Gupta, it worked like a charm. You saved me a lot of time.
  2 Comments
Walter Roberson
Walter Roberson on 26 Sep 2018
You need to add one more 'end' to this, either right after the plot(data) or at the very end. For your code structure, right after the plot(data) would make the most sense.

Sign in to comment.


jenni
jenni on 18 Feb 2013
Is it possible to add a timer function to this code? I want a function to be executed once in every 2 sec?

Csaba
Csaba on 18 Jul 2011
Hi Chirag Gupta!
Do you know how to get the same result with a NI DAQ-6052E PCI acquisition card? I tried your code, but since I could not get any CompactDAQ device detected:
>> d=daq.getDevices
d =
No data acquisition devices available.
Click here for a list of known vendors.
Click here for troubleshooting tips.
??? Error: File: F20110718_AIAO_test_v15.m Line: 7 Column: 1
Function definitions are not permitted in this context.
the following line did not worked:
s.addAnalogInputChannel('cDAQ1Mod1',0,'voltage')
Maybe my device is not meant to work with sessions. It would be a shame, because the event detection would be very useful for me.
Indeed, my goal is to acquire 4 different signals on 4 different analog input through the NI BNC-2110 and the NI DAQ-6052E PCI card, plot them "live" as well as output "live" 3 different voltages (each would be a given function of the input voltages) on 3 different analog output.
Thank you very much! Kind regards,
Csaba
  1 Comment
Chirag Gupta
Chirag Gupta on 18 Jul 2011
As of MATLAB R2011a, only Compact DAQ devices are supported with the Session based architecture described above. You can try the R2011b prerelease (download from the MathWorks website) that supports other NI boards with the new architecture.
Otherwise, you can look at this demo:
"Continuous Acquisition Using Analog Input"

Sign in to comment.


Dagmar Fraser
Dagmar Fraser on 18 Oct 2011
OOh top work - been looking for something like this for literal years!
I had to change
% Do something
while(~s.IsDone)
end
to
s.IsDone % will report 0
s.wait() % rather than while
s.IsDone % will report 1
  1 Comment
Christos
Christos on 28 Mar 2012
hi! i would like to ask if it is possible to use the following code:
s.addAnalogInputChannel('Dev1','ctr0','EdgeCount'), in order to get the Edge count value from NI card usb 6221?
Thanks for your help

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!