DAQ cannot generate signal in GUI callback function.

3 ビュー (過去 30 日間)
Bohan Wu
Bohan Wu 2019 年 8 月 9 日
回答済み: Geoff Hayes 2019 年 8 月 13 日
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 Center および File ExchangeData 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