How to use GUI data in other functions ?

3 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Geoff Hayes
Geoff Hayes il 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 Commenti
Arun Sharma
Arun Sharma il 24 Nov 2015
I have one more question, in the same thing i am trying to develop. What i wants whenever user tries to open this software, my first gui will open and then second gui will open, in which it will ask for com port which we want to open, on selecting the com port, the user press the select button, then this gui will open that particular com port, and close itself and now gui1 will come in front once again. Where all this packet transfer will take place. Now the problem is that, i am selecting and open com port in gui2, so can i pass this information to gui1.
Please help.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by