Function to stop the running serial communication of comport

1 次查看(过去 30 天)
Hello,
The serial communication using comport is working fine and the main lines of code are
s1 = open_com_port(com_port);
gui_update_freq_divisor = 20;
flushBytesThreshold = 500;
k = 1;
pulse_count=0;period(20)=0;results(100)=0;max1=0;
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
% update gui not so often
if (~mod(sampleCounter,gui_update_freq_divisor))
for i=1:1:20
if (data(i)==4095)
results(i)=1;
else
results(i)=0;
end
end
max1=max(results);
axes(rawax_gyr)
set(raw1_gyr,'XData',[get(raw1_gyr,'Xdata') currtime]);
set(raw1_gyr,'YData',[get(raw1_gyr,'YData') data]);
% Slide x-axis of the smaller axes
set(rawax_gyr,'XLim',[currtime(1)-10 currtime(1)])
set(rawax_gyr,'YLim',[-10 5000])
k = 0;i=1;
%drawnow
end
sampleCounter = sampleCounter + 1;
k = k + 1;
end
%pause(0.0001);
end
delete(s1)
close(fh)
end
But what are the lines(command) needed to stop the serial communication of COMPORT.
I am planning to implement this a GUI and for start button the serial communication starts but can I know what are the needed lines in the pushbutton call back fucntion so that it stops the communication when stop button is pressed .
  4 个评论
Gova ReDDy
Gova ReDDy 2014-3-16
编辑:Gova ReDDy 2014-3-16
I tired to stop the communication using delete(open_com_port(12)) in the stopbutton function but it is 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',[100 400 50 20],'Callback',@stopbutton_Callback);
Exit=uicontrol('Style','pushbutton','String', 'Exit', 'Position',[20 20 50 20],'Callback',@Exitbutton1_Callback);
function Exitbutton1_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 = 'idm30_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);
delete(open_com_port(12))
function textbox1_Callback(hObject, eventdata, handles)
function textbox1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
Can someone explain the way to stop the serial data streaming of comport.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2014-3-16
  2 个评论
Gova ReDDy
Gova ReDDy 2014-3-16
Thanks for the information,this will help me in improving the GUI but I really didn't get how to stop the serial communication.
I improved the GUI a bit more but got the problem mentioned here . Can you please help me out in solving this issue.
Walter Roberson
Walter Roberson 2014-3-16
You are trying to set a quit flag in one routine and you are trying to read the quit flag in another routine. The FAQ talks about how to share information between routines.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by