How to use GUI data in other functions ?
Show older comments
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
More Answers (0)
Categories
Find more on Enterprise Deployment with MATLAB Production Server in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!