using a timer in GUI

4 visualizzazioni (ultimi 30 giorni)
Daniel
Daniel il 15 Ago 2016
Modificato: Daniel il 22 Ago 2016
Hi, I have a GUI to manage UART communication. I want to use a timer as follows: if i haven't got the answer for what i requested, in 4ms time ,then re-send the request (for example).
I tried several timer configuration and for each i get an error message:"error while evaluating timerfcn for timer 'timer-x' , where x is keep changing.
i tried: in openingFcn:
handles.timer1 - timer('executionmode',fixedrate','period',0.005,'timerfcn',@(src,event)NameOfFunc(src,event,handles));
in the desierd callback :
start(handles.timer1)
in the function
function NameOfFunc(src,event,handles)
DO SOMETHING
end
i get error (this time) error while evaluating timerfcn for timer 'timer-6' i also tried defining the timer as a global parameter
  2 Commenti
Sean de Wolski
Sean de Wolski il 15 Ago 2016
What's the full error message?
Daniel
Daniel il 15 Ago 2016
now i tried:
handles.timer1 = timer('executionmode',fixedrate','period',0.005,'timerfcn',@NameOfFun);
and:
function NameOfFun()
do something
end
error: error while evaluating TimerFcn for timer 'timer-14' Too many input arguments

Accedi per commentare.

Risposta accettata

Sean de Wolski
Sean de Wolski il 15 Ago 2016
Modificato: Sean de Wolski il 15 Ago 2016
function NameOfFun()
do something
end
Needs to take in the two inputs that come by default, src, evt.
function NameOfFun(src,evt)
do something
end
If you also want to pass in handles, like in your original, it needs to take in three inputs.
  8 Commenti
Sean de Wolski
Sean de Wolski il 18 Ago 2016
This error
function my_timer(mytimer,evt,handles) %as you suggested
DATA_SAVE_CallBack(handles.DATA_SAVE,eventdata,handles)
end
error:"error while evaluating TimerFCN...
undefind function or variable 'eventdata'"
is happening because the variable is named evt not eventdata.
The handles are stored as part of the figure's data. in order to update them after appending timer1 you need to run:
guidata(handles.figure1,handles)
to update the state. This is what happens in the OpeningFcn.
If your user interface is not that complex, and you're on R2016a, then give app designer a try. It manages data in a much clearer, easier, and more modern way.
>> appdesigner
Daniel
Daniel il 22 Ago 2016
Modificato: Daniel il 22 Ago 2016
Hi, Sorry for to late "bump". So everything was working fine. I used a timer as a watchdog timer, worked perfectly. Now i decided to restrict the timer to 3 operation only, i.e. send message request, if no answer, try 3 times( 3 timer periods) to resend request , if still no answer, ask for next message (message number 2). So i use stopfcn, and tasksto execute and now nothing works again.
function serial_com_Start
...code
handles.timer1 = timer(...
'taskstoexecute',3,...
'stopfcn'.{@param_request,hanles},...
'timerfcn',{@timerfunc,handles});
... code...
guidata(hObject,handles)
end
function timerfunc(~,~,handles)
fwrite(handles.serPIC,output1); %if no answer re send info
end
function param_request(~,~,handles)
fwrite(handles.serPIC,output2);
start(handles.timer1)
end
THE ERROR: error while evaluating StopFcn for timer 'timer-20' reference to non-existent field 'timer1'
from my understanding its the start timer i use in the last function. but how can i do it differently?

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Dialog Boxes in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by