function varargout = comtest2(varargin)
% COMTEST2 MATLAB code for comtest2.fig
% COMTEST2, by itself, creates a new COMTEST2 or raises the existing
% singleton*.
%
% H = COMTEST2 returns the handle to a new COMTEST2 or the handle to
% the existing singleton*.
%
% COMTEST2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in COMTEST2.M with the given input arguments.
%
% COMTEST2('Property','Value',...) creates a new COMTEST2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before comtest2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to comtest2_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help comtest2
% Last Modified by GUIDE v2.5 15-Dec-2010 16:18:59
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @comtest2_OpeningFcn, ...
'gui_OutputFcn', @comtest2_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
% End initialization code - DO NOT EDIT
% --- Executes just before comtest2 is made visible.
function comtest2_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 comtest2 (see VARARGIN)
% Choose default command line output for comtest2
handles.output = hObject;
com = get(handles.popupmenu5,'value');
com = strcat('COM',num2str(com));
handles.serial = serial(com);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes comtest2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = comtest2_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;
.......省略了下拉菜单的函数回调函数.......
% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles) %这是打开串口的开关,打开后开始读取数据,并使能callback函数
% hObject handle to togglebutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton1
sta = get(hObject,'value');
switch sta
case 0
try
fclose(handles.serial);
delete(handles.serial);
guidata(hObject,handles);
catch
msgbox('出错,串口不存在,或其他软件使用该串口','警告','warn');
set(handles.togglebutton1,'value',1);
end
case 1
try
com = get(handles.popupmenu5,'value');
com = strcat('COM',num2str(com));
br = 1200*get(handles.popupmenu1,'value');
sbs = get(handles.popupmenu2,'value');
parit = get(handles.popupmenu3,'value');
switch parit
case 1
strpar = 'odd';
case 2
strpar = 'even';
case 3
strpar = 'none';
end
datbit = 5+get(handles.popupmenu4,'value');
handles.serial = serial(com,'baudrate',br,'parity',strpar,'databits',datbit,'stopbits',sbs);
set(handles.serial,'InputBufferSize',16);
set(handles.serial,'BytesAvailableFcnMode','byte');
set(handles.serial,'BytesAvailableFcncount',16);
guidata(hObject,handles);
set(handles.serial,'BytesAvailableFcn',{@mycom,handles});
fopen(handles.serial);
catch
msgbox('出错,串口不存在,或其他软件使用该串口','警告','warn');
set(handles.togglebutton1,'value',0);
end
end
function mycom(hObject,eventdata,handles) %这是串口读取16个数据后执行的callback函数
newd = fread(handles.serial);
axes(handles.axes1);
plot(newd);
% --- Executes on selection change in popupmenu5.
function popupmenu5_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu5 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu5
% --- Executes during object creation, after setting all properties.
function popupmenu5_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
MATLAB GUI串口助手
4星 · 超过85%的资源 需积分: 47 174 浏览量
2017-09-23
22:53:38
上传
评论 18
收藏 6KB RAR 举报
AlwaysSun
- 粉丝: 182
- 资源: 30
最新资源
- wav2lip-gan.pth 资源
- 广告点击率预测中常用的criteo数据集
- 三菱PLC例程源码fx2nplc与三台丹佛斯变频器458通讯定位.定尺源程序
- 三菱PLC例程源码FX2NPLC与安川G7变频器通讯(原创)有注释,MEMOBUS的写方式
- 三菱PLC例程源码FX2NPLC与A700变频器RUT通讯
- 小智搜搜-必备搜索工具,搜索快启更胜一筹
- 三菱PLC例程源码FX2NPLC和变频器,F930通讯控制的程序有注解拉丝机程序
- 三菱PLC例程源码FX2NPLC+2AD+4ADTC+2DA+电阻尺+编码器+PID温控
- 安装和配置指引,通俗易懂
- 三菱PLC例程源码FX2NPLC+2AD+4ADTC+2DA+电阻尺+编码器+PID温控完整注释程式+UG330人机
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页