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)enddisp('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.
Best Answer