DAQ cannot generate signal in GUI callback function.

3 次查看(过去 30 天)
Hi,
Currently I have these code in a .m file and it works properly by continuing generating signal.
s = daq.createSession('ni');
addAnalogOutputChannel(s,'cDAQ1Mod2',0,'Voltage');
s.IsContinuous = true;
s.Rate = 10000;
data = linspace(-1,1,5000)';
lh = addlistener(s,'DataRequired', ...
@(src,event) src.queueOutputData(data));
queueOutputData(s,data)
startBackground(s);
However, when I copy these code into a pushbutton callback function in my GUI. No signal being generated.
s = daq.createSession('ni');
addAnalogOutputChannel(s,'cDAQ1Mod2',0,'Voltage');
s.IsContinuous = true;
s.Rate = 10000;
data = linspace(-1,1,5000)';
lh = addlistener(s,'DataRequired', ...
@(src,event) src.queueOutputData(data));
queueOutputData(s,data)
startBackground(s);
pause;
After I added 'pause' at the end as the above code, it starts works properly. But this is not the way I want to do. I hope anyone can help me by explaining the reason and want to know is any way that I can generate the continuous signal by using GUI and stop generating whenever user wants to.
Thanks so much!

回答(1 个)

Geoff Hayes
Geoff Hayes 2019-8-13
Bohan - I suspect that what is happening is that because you are creating local variables for the DAQ session, then when the callback completes, the local variables are destroyed and your DAQ session ends. That is why calling pause "fixes" the problem - the function doesn't complete and the local variables do not go out of scope. If you are using GUIDE, then save the DAQ session (and probably listener) to the handles structure so that they are maintained. You can then later end the DAQ session when needed by accessing it via this structure.

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by