当前位置:首页 > matlab国外编程代写matlab GUI串口通信设计
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 \% 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)
共分享92篇相关文档