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 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