DAQ cannot generate signal in GUI callback function.

2 visualizaciones (últimos 30 días)
Bohan Wu
Bohan Wu el 9 de Ag. de 2019
Respondida: Geoff Hayes el 13 de Ag. de 2019
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!

Respuestas (1)

Geoff Hayes
Geoff Hayes el 13 de Ag. de 2019
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.

Categorías

Más información sobre Data Acquisition Toolbox Supported Hardware en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by