Using timers in a class
Mostrar comentarios más antiguos
Hi all, I have the code below that is a simple 2 second timer that clears a flag to allow the program to exit the while loop and it works fine.
waitFlag = 1;
num = 0;
t = timer('TimerFcn', 'waitFlag=0;',...
'StartDelay', 2, ...
'ExecutionMode', 'singleShot');
start(t);
while (waitFlag) && (num == 0)
end
disp('Timer works');
Take that same concept and put it inside a class function and it doesn't work.
function ret = waitForSerial(obj)
waitFlag = 1;
t = timer('TimerFcn', 'waitFlag=0;',...
'StartDelay', 2, 'ExecutionMode', 'singleShot');
start(t);
% Wait for response to come in
% waitFlag becomes 0 when timer expires
% While is broken if timer expires or if serial data comes in
while (waitFlag) && (obj.comPort.NumBytesAvailable == 0)
end
disp("Done waiting");
if obj.comPort.NumBytesAvailable > 0
stop(t);
ret = obj.interpretMsg();
else
stop(t);
commsErr(obj, "COMs timeout");
ret = -2;
return
end
delete(t);
end
What happens is I guess the timer doesn't start, it doesn't trigger or it doesn't clear the waitFlag because the program never exits the while loop. If there is some special behaviour regarding timers/callbacks within classes, any help would be greatly appreciated.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Code Execution en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!