GUI:Usage of start button runs for once and then MATLAB gets strucked

2 次查看(过去 30 天)
Hello,
I made a GUI in GUI builder and expected to work like this
start button : start streaming the data from comport should stop the streaming data
start button : should stop the data streaming from comport
Exit button : should close the GUI
Problem: 1)when the start button is clicked it is streaming the data and showing on the plot perfectly but when a exit was made and tried to start the data streaming again by clicking the start button it is showing
MATLAB:serial:fopen:opfailed in command window
and the MATLAB gets strucked and needs a manual restart.
2) Also clicking the stop button is not stopping the data streaming.
function varargout = start1(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @start1_OpeningFcn, ...
'gui_OutputFcn', @start1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function start1_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = start1_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
start=uicontrol('Style','pushbutton','String', 'start', 'Position',[20 400 50 20],'Callback',@startbutton_Callback);
stop=uicontrol('Style','pushbutton','String', 'stop', 'Position',[20 300 50 20],'Callback',@stopbutton_Callback);
Exit=uicontrol('Style','pushbutton','String', 'Exit', 'Position',[20 100 50 20],'Callback',@pushbutton1_Callback);
writetofile=uicontrol('Style','pushbutton','String', 'writetofile', 'Position',[20 200 50 20],'Callback',@writetofile_Callback);
function pushbutton1_Callback(hObject, eventdata)
handles = guidata(hObject);
s_quitgui = true;
display('Goodbye');
close(gcf);
function startbutton_Callback(hObject, eventdata)
handles = guidata(hObject);
axes(handles.axes2);
linewidth = 1;
raw1_gyr = line('Parent',handles.axes2,...
'XData',[],...
'YData',[],...
'Color',[0 0 1],...
'LineWidth',linewidth);
com_port = 12;
sampleCounter = 1;
dt = 5*(1e-2);
data_file_name = 'pulse_data.txt';
FID = [];
s_writetofile = false;
s_quitgui = false;
s1 = open_com_port(12);
gui_update_freq_divisor = 20;
flushBytesThreshold = 1000;
k = 1;
while ~s_quitgui
nb = s1.BytesAvailable;
if nb > flushBytesThreshold
error('serial data overflow')
end
if(nb)
currtime(k) = sampleCounter*dt;
[tline,count] = fgetl(s1);
data_tmp = str2num(char(tline));
data(k)=(data_tmp);
if s_writetofile
fprintf(FID,'%f %f \n',currtime,data);
end
if (~mod(sampleCounter,gui_update_freq_divisor))
axes(handles.axes2);
set(raw1_gyr,'XData',[get(raw1_gyr,'Xdata') currtime]);
set(raw1_gyr,'YData',[get(raw1_gyr,'YData') data]);
set(handles.axes2,'XLim',[currtime(1)-1 currtime(1)])
set(handles.axes2,'YLim',[-1 5000])
k = 0;
end
sampleCounter = sampleCounter + 1;
k = k + 1;
end
end
function stopbutton_Callback(hObject, eventdata)
handles = guidata(hObject);
function writetofile_Callback(hObject,eventdata)
s_writetofile = false;
data_file_name = 'pulse_data.txt';
if s_writetofile
fclose(FID);
s_writetofile = false;
set(writetofile,'String','Write to File',...
'BackgroundColor', defaultuibgcolor)
else
FID = fopen(data_file_name,'w');
s_writetofile = true;
set(writetofile,'String','Stop Writing',...
'BackgroundColor','red')
end
function textbox1_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function textbox1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
Here open_com_port is a fucntion for the comport settings.
can someone help me out in figuring the solution to these.
Thanks.
  4 个评论
Jan
Jan 2017-2-3
It was impossible to answer the question.
2) Also clicking the stop button is not stopping the data streaming.
But the callback of the stop button was:
function stopbutton_Callback(hObject, eventdata)
handles = guidata(hObject);
It is clear, that nothing happens here, because there is no code which tries to let anything happen.
The comport was opened, but not closed anywhere. So, VRM, please open a new thread and ask a specific question. Post your code and explain, what is not working as expected.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by