function varargout = untitled(varargin)
% UNTITLED M-file for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_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 untitled
% Last Modified by GUIDE v2.5 07-Sep-2013 13:47:05
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_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 untitled is made visible.
function untitled_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 untitled (see VARARGIN)
% 初始化
set(handles.caiji,'UserData',0);
setappdata(handles.save,'ApplicationData',0);
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_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 on.
function on_Callback(hObject, eventdata, handles)
% hObject handle to on (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc;
s1 = instrfind('Type','serial','Port','COM1','Tag',''); % 查看端口是否存在
s2 = instrfind('Type','serial','Port','COM2','Tag','');
s3 = instrfind('Type','serial','Port','COM3','Tag',''); % 查看端口是否存在
s4 = instrfind('Type','serial','Port','COM4','Tag','');
% 获取参数值
if (isempty(s1) && get(handles.port,'Value')==1 ) % 如果端口1存在,存创建端口
s = serial('COM1'); % 创建串口号
if(get(handles.botelv,'Value')==1) % 设置串口参数
s.BaudRate = 1200;
elseif(get(handles.botelv,'Value')==2)
s.BaudRate = 2400;
elseif(get(handles.botelv,'Value')==3)
s.BaudRate = 4800;
elseif(get(handles.botelv,'Value')==4)
s.BaudRate = 9600;
elseif(get(handles.botelv,'Value')==5)
s.BaudRate = 19200;
elseif(get(handles.botelv,'Value')==6)
s.BaudRate = 38400;
elseif(get(handles.botelv,'Value')==7)
s.BaudRate = 76800;
elseif(get(handles.botelv,'Value')==8)
s.BaudRate = 115200;
end
if(get(handles.jiaoyanw,'Value')==1)
s.Parity = 'none'; % 无校验位
elseif(get(handles.jiaoyanw,'Value')==2)
s.Parity = 'odd'; % 偶校验
elseif(get(handles.jiaoyanw,'Value')==3)
s.Parity = 'even'; % 奇校验
end
if(get(handles.dataw,'Value')==1) % 数据位
s.DataBits = 8;
elseif(get(handles.dataw,'Value')==2)
s.DataBits = 7;
elseif(get(handles.dataw,'Value')==3)
s.DataBits = 6;
elseif(get(handles.dataw,'Value')==4)
s.DataBits = 5;
end
if(get(handles.stopw,'Value')==1) % 停止位
s.StopBits = 1;
elseif(get(handles.stopw,'Value')==2)
s.StopBits = 1.5;
elseif(get(handles.stopw,'Value')==3)
s.StopBits = 2;
end
s.Timeout = 0.1;
s.InputBufferSize = 3072;
s.OutputBufferSize = 3072;
s.ReadAsyncMode = 'continuous';
handles.sbuff = s;
guidata(hObject, handles);
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcnCount = 10;
s.BytesAvailableFcn = {@recive_data, handles};
fopen(s);
set(handles.caiji,'Enable','On');
set(handles.off,'Enable','On');
set(handles.on,'Enable','Off');
set(handles.edit1,'String','打开成功!');
set(handles.exit,'Enable','Off');
set(handles.edit1,'backgroundcolor',[0 1 0]);
elseif (isempty(s2) && get(handles.port,'Value')==2 ) % 如果端口2存在,存创建端口
s = serial('COM2'); % 创建串口号
if(get(handles.botelv,'Value')==1) % 设置串口参数
s.BaudRate = 1200;
elseif(get(handles.botelv,'Value')==2)
s.BaudRate = 2400;
elseif(get(handles.botelv,'Value')==3)
s.BaudRate = 4800;
elseif(get(handles.botelv,'Value')==4)
s.BaudRate = 9600;
elseif(get(handles.botelv,'Value')==5)
s.BaudRate = 19200;
elseif(get(handles.botelv,'Value')==6)
s.BaudRate = 38400;
elseif(get(handles.botelv,'Value')==7)
s.BaudRate = 76800;
elseif(get(handles.botelv,'Value')==8)
s.BaudRate = 115200;
end
if(get(handles.jiaoyanw,'Value')==1)
s.Parity = 'none'; % 无校验位
elseif(get(handles.jiaoyanw,'Value')==2)
s.Parity = 'odd'; % 偶校验
elseif(get(handles.jiaoyanw,'Value')==3)
s.Parity = 'even'; % 奇校验
end
if(get(handles.dataw,'Value')==1) % 数据位
s.DataBits = 8;
elseif(get(handles.dataw,'Value')==2)
s.DataBits = 7;
elseif(get(handles.dataw,'Value')==3)
s.DataBits = 6;
elseif(get(handles.dataw,'Value')==4)
s.DataBits = 5;
end
if(get(handles.stopw,'Value')==1) % 停止位
s.StopBits = 1;
elseif(get(handles.stopw,'Value')==2)
s.StopBits = 1.5;
elseif(get(handles.stopw,'Value')==3)
s.StopBits = 2;
end
s.Timeout = 0.1;
s.InputBufferSize = 3072;
s.OutputBufferSize = 3072;
s.ReadAsyncMode = 'continuous';
handles.sbuff = s;
guidata(hObject, handles);
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcnCount = 10;
s.BytesAvailableFcn = {@recive_data, handles};
fopen(s);
set(handles.caiji,'Enable','On');
set(handles.off,'Enable','On');
set(handles.on,'Enable','Off');
set(handles.exit,'Enable','Off');
set(handles.edit1,'String','打开成功!');
set(handles.edit1,'backgroundcolor',[0 1 0]);
elseif (isempty(s3) && get(handles.port,'Value')==3 ) % 如果端口3存在,存创建端口
s = serial('COM3'); % 创建串口号
if(get(handles.botelv,'Value')==1) % 设置串口参数
s.BaudRate = 1200;
else
matlab简单的串口通信程序
需积分: 42 90 浏览量
2018-07-05
21:17:54
上传
评论 10
收藏 13KB RAR 举报
qq_38346431
- 粉丝: 2
- 资源: 21
最新资源
- yolov5,SSD 可能使用到的一些代码
- 介绍离散性制造行业的MES系统流程
- 基于IDEA-CCNL/Randeng-Pegasus-238M-Summary-Chines微调的中文文本摘要任务源码+数据集
- 微信小程序源码 车源宝 二手车交易平台 源码下载
- 微信小程序源码 实现 城市切换 demo 根据城市首字母排序城市 选择城市 源码下载
- 2024新版计算机编译原理期末速成全套视频教程(视频+配套资料)
- VMware7.0虚拟机硬盘无法编辑,无法连接到Profile-Driven Storage Service
- arm64内核的mongo镜像
- 基于stm32f103c单片机+MPU6050+0.96英寸OLED显示屏双柄遥控器硬件(原理图+PCB)工程文件.zip
- 整理的关于少儿编程的学习路径,以及如何在小升初,初升高和大学充分的利用起来编程经验的优势
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈