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 commentaires

Sean de Wolski
Sean de Wolski le 15 Août 2016
What's the full error message?
Daniel
Daniel le 15 Août 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

Connectez-vous pour commenter.

 Réponse acceptée

Sean de Wolski
Sean de Wolski le 15 Août 2016
Modifié(e) : Sean de Wolski le 15 Août 2016

0 votes

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 commentaires

Daniel
Daniel le 15 Août 2016
Thanks!!! it seems i tried every posible combination except that. So 3 more questions:
1. Do i need to pass handles as well?
2. It seems that at the start it jumps immediately to the function and not wait for the period i set to be over, why is that?(after the first time it returns every period as i expect it)
3.How do i reset the timer? i.e. i want the timer to jump to the function, BUT if i reset it in another place before period is over, i want the period to reset and jump to the function only if a reset had not been made
Daniel
Daniel le 16 Août 2016
Update: so it worked, but now i try to call a call back function from the timer_function. The code in the function:
function timer_X(src,evt,handles)
XXX_callback(hObject, eventdata, handles)
end
the error:
Error while evaluating TimerFcn for timer 'timer-22'
Undefined function or variable 'hObject'
so i tried
function timer_X(hObject,evt,handles)
XXX_callback(hObject, eventdata, handles)
end
no the error: Error while evaluating TimerFcn for timer 'timer-23' Undefined function or variable 'eventdata'
so :) i tried:
function timer_X(hObject,eventdata,handles)
XXX_callback(hObject, eventdata, handles)
end
and now.... i got the error:
Error while evaluating TimerFcn for timer 'timer-24'
Not enough input arguments
any idea?
Sean de Wolski
Sean de Wolski le 16 Août 2016
Modifié(e) : Sean de Wolski le 16 Août 2016
For a delay after it starts, use the timer's 'StartDelay' property.
hObject is not defined in the timer callback. Typically, hObject is the handle to the calling object so if you want to call the callback for pushbutton1, you would call it as
function timercallback(thistimer,evt,handles)
pushbutton1_callback(handles.pushbutton1,eventdata,handles)
Explicitly creating "hObject".
Sean de Wolski
Sean de Wolski le 16 Août 2016
Out of curiosity, how complex is the user interface you're building?
Daniel
Daniel le 17 Août 2016
Modifié(e) : Daniel le 17 Août 2016
I cant seem to get it to work. Here is what i have:
In the starting func if have the following:
handles.timer1 = timer(...
'ExecutionMode','FixedRate',...
'period',2,...
'StartDelay',2,...
'TimerFcn',{@my_timer,handles});
end %end of start fcn
function DATA_SAVE_Callback(hObject,eventdata,handles)
%some code
end
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'"
I don't think my program is too complex. I have several buttons to send info request from an external system. I have text boxes to show the incoming info. I have a state machine to chk the incoming message is ok (HEADERS, CRC) and direct the info to the correct place. I have several edit boxes where the user can enter a parameter that will be written to the system (and immediately after i do a read to chk everything is ok)
and now i want to save data to a file. for that i want to send info request messages in a serial mode, i.e. ask for data 1, after i get it (this is where the timer kicks in as a WATCHDOG timer) i ask for data 2 and so on. so i basically need the timer to tell me: OK, X time has passed, no answer, resend request.
but i am pretty stuck with operating the timer
Daniel
Daniel le 17 Août 2016
UPDATE: I moved the handles.timer1 deceleration to the function where i set up the serial communication, immediately after i establish the communication. now it seems to work... I apparently dont fully understand where all these handles "live" and why it didn't work while defining it in the openingFCN
Sean de Wolski
Sean de Wolski le 18 Août 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 le 22 Août 2016
Modifié(e) : Daniel le 22 Août 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?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Code Execution dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by