MATLAB Answers

How to use GUI data in other functions ?

11 views (last 30 days)
Arun Sharma
Arun Sharma on 22 Nov 2015
Commented: Geoff Hayes on 24 Nov 2015
Hello! Everyone I am trying to make a GUI, which periodically sends some packet over Serial Port. I need to send this packet 10 times, and if there is no response from Serial Port the timeout will occur. I made a variable handles.TimeOutCounter in the Opening Function, with the intention that, i will increment it every time and reset it to zero whenever there is data from Serial Port. Currently i haven't implemented the Serial Reception CallBack function yet, so this function must transmit the data for 10 times, but it is sending data just one time and after that it gives some error.
#############################################################
_Error while evaluating TimerFcn for timer 'timer-1'
H must be the handle to a figure or figure descendent._
#############################################################
The code is as follow:
% --- Executes just before UltraSonicSensor is made visible.
function UltraSonicSensor_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to UltraSonicSensor (see VARARGIN)
% Choose default command line output for UltraSonicSensor
handles.output = hObject;
axes(handles.axes1)
[SensorImage, SensorMap] = imread('hc-sr04.bmp');
imshow(SensorImage, SensorMap)
axis off
axis image
% Delete any opened Ports
delete(instrfind);
% Create Serial Object at 9600 BaudRate
handles.serial = serial('COM1','BaudRate', 9600);
handles.serial.BytesAvailableFcnMode = 'terminator';
% Serial Reception Callback Funtion
handles.serial.BytesAvailableFcn = @Serial_Receive;
% Open Serial Port
fopen(handles.serial);
% Counter Variable
handles.TimeOutCounter = 1;
% Delete any previously defined timer
delete(timerfind);
% Starts a 1 second timer
handles.timer = timer;
handles.timer.TimerFcn = {@Timer_1Second, handles};
handles.timer.StartDelay = 5;
handles.timer.ExecutionMode = 'FixedRate';
start(handles.timer);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes UltraSonicSensor wait for user response (see UIRESUME)
% uiwait(handles.GUI1);
% --- Outputs from this function are returned to the command line.
function varargout = UltraSonicSensor_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% Function gets called after 1 second
function Timer_1Second(hObject, evendata, handles)
handles.output = hObject;
% Construct Packet to Send
My_Address = 14; % 0x0E
Trans_Header = 44; % 0x2C
Trans_Destination = 36; % 0x24
Trans_Source = My_Address;
Trans_Length = 2;
Trans_OpCode = 1;
Trans_Data = 1;
data = [Trans_Header,Trans_Destination,Trans_Source,Trans_Length ...
,Trans_OpCode,Trans_Data];
% Computer Checksum
Trans_Checksum = Compute_Checksum(data,6);
% Append Checksum at the end of the Packet
data(end+1) = Trans_Checksum;
% Check whether the Serial Port is Open
if strcmp(get(handles.serial,'Status'),'open')
% Send data over Serial Port
fwrite(handles.serial,data,'uchar');
% Increment Counter Variable
handles.TimeOutCounter = handles.TimeOutCounter + 1;
% Debugging Purpose, Print the value on Serial Port
fprintf('Value is = %d\n ',handles.TimeOutCounter);
end
if handles.TimeOutCounter > 10
% Timeout Error, No Data receive in desired time.
stop(handles.timer);
fclose(handles.serial);
delete(instrfind);
end
% Update handles structure
guidata(hObject, handles);
Hope you all understands my intention, what i want to do. Please help me, i am stuck in this problem last few days.

Accepted Answer

Geoff Hayes
Geoff Hayes on 22 Nov 2015
Arun - in this case, using the MATLAB debugger would be a good way to figure out what is happening. If you put a breakpoint at the first line of the Timer_1Second function then you could run the application and start stepping through the code when the debugger pauses at this line. What would you find interesting are the inputs
hObject - is the timer object and NOT the figure/GUI handle (as the callback fires for the timer)
eventdata - is a struct with Type and Data fields
handles - is a copy of the handles structure at the time that you created the callback
Since hObject is the timer object, then the line of code that is generating the error message is
guidata(hObject, handles);
So what you need to do is use the handle to the figure/GUI which you can get from the handles structure...but there will still be a problem. handles is a copy of the handles structure at the time that the callback is initialized so it will never have the TimeOutCounter field assigned to it.
What you can do is pass the figure handle into the callback and use that to get the handles structure whenever the timer function callback is called. For example, try making the following change in your UltraSonicSensor_OpeningFcn
handles.timer.TimerFcn = {@Timer_1Second, hObject};
Since the above occurs in UltraSonicSensor_OpeningFcn, the hObject is the handle to the figure. Now, in your timer function callback, do the following (note the signature change with hGui)
function Timer_1Second(hObject, evendata, hGui)
% get the updated handles structure
handles = guidata(hGui);
% Construct Packet to Send
% etc.
% Update handles structure
guidata(hGui, handles);
So every time the timer callback fires, we get the latest updated handles structure using guidata and hGui. We then do as before and then do the update using both hGui and handles. (Note that I am unsure why you assign hObject to handles.output on each call to this function. It shouldn't be necessary unless there is a reason why you want to do this.)
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!