NIDAQ continuous and background acquisition

15 visualizzazioni (ultimi 30 giorni)
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 Commenti
Jacob Graham
Jacob Graham il 15 Giu 2018
Hey Everyone,
Looks like I'm a few years late to the party. These solutions and comments have been very helpful, but I still have a problem. The while(~s.IsDone) loop doesn't appear to be working for me. When I run the code below, Matlab remains busy indefinitely while it's on the while(~s.IsDone) line. When I debug and step past it manually, the code that follows works. I'd like to use the command window while the session is running in the background which is why this is a problem.
Thanks for your help
J
function acquireData
clear global;
global data
s = daq.createSession('ni');
ch3 = addAnalogInputChannel(s,'cDAQ1Mod1', 'ai3', 'RTD');
ch3.RTDType = 'Pt3851';
ch3.RTDConfiguration = 'ThreeWire';
ch3.R0 = 100;
s.Rate = 100;
duration = input('Duration of Sampling Period?');
s.DurationInSeconds = duration;
time = (0.01:0.01:duration);
lh = s.addlistener('DataAvailable', @plotData);
startBackground(s);
while(~s.IsDone)
end
close(gcf);
plot(time,data);
delete(lh);
end
function plotData(src, event)
global tempData;
global data
if(isempty(tempData))
tempData = [];
end
plot(event.TimeStamps, event.Data)
tempData = [tempData;event.Data];
data = tempData;
end
Walter Roberson
Walter Roberson il 15 Giu 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.

Accedi per commentare.

Risposta accettata

Chirag Gupta
Chirag Gupta il 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 Commenti
Tim Helps
Tim Helps il 8 Mar 2018
Hi Chriag,
Thanks for these solutions!
However, when I used them I noticed one problem with your tempData variable - since it's persistent, the data will be there still after the acquireData function is completed. If the acquireData function is run again, the previous data will still be there and tempData (and thus data) will continue to grow.
I've fixed this in my code by making it a global rather than persistent variable, and using: clearvars -global at the start of the acquireData function.
If you have a better solution please let me know!
Many thanks, Tim
Alexandre Piccini
Alexandre Piccini il 26 Set 2018
Hey,
I come from the future just to thank you for this. Just what I was looking for :)
Greetings

Accedi per commentare.

Più risposte (4)

Nuno Martins
Nuno Martins il 27 Apr 2011
Thank you very much Chirag Gupta, it worked like a charm. You saved me a lot of time.
  2 Commenti
avinash pabbireddy
avinash pabbireddy il 11 Nov 2013
Modificato: Walter Roberson il 26 Set 2018
function nidaq9234
global data
s = daq.createSession('ni');
s.addAnalogInputChannel('cDAQ2Mod1',0,'voltage')
s.addAnalogInputChannel('cDAQ2Mod1', 1, 'Voltage');
s.Rate = 2000
s.DurationInSeconds = 20
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
i'm getting error while running the code
Error: File: nidaq9234.m Line: 26 Column: 1
The function "plotData" was closed
with an 'end', but at least one other function definition was not.
To avoid confusion when using nested functions,
it is illegal to use both conventions in the same file.
Walter Roberson
Walter Roberson il 26 Set 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.

Accedi per commentare.


jenni
jenni il 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 il 18 Lug 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 Commento
Chirag Gupta
Chirag Gupta il 18 Lug 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"

Accedi per commentare.


Dagmar Fraser
Dagmar Fraser il 18 Ott 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 Commento
Christos
Christos il 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

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by