📄 uart2.m
字号:
function varargout = uart2(varargin)
% UART2 M-file for uart2.fig
% UART2, by itself, creates a new UART2 or raises the existing
% singleton*.
%
% H = UART2 returns the handle to a new UART2 or the handle to
% the existing singleton*.
%
% UART2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UART2.M with the given input arguments.
%
% UART2('Property','Value',...) creates a new UART2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before uart2_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to uart2_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
% Copyright 2002-2003 The MathWorks, Inc.
% Edit the above text to modify the response to help uart2
% Last Modified by GUIDE v2.5 27-Feb-2008 16:26:39
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @uart2_OpeningFcn, ...
'gui_OutputFcn', @uart2_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 uart2 is made visible.
function uart2_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 uart2 (see VARARGIN)
% Choose default command line output for uart2
handles.output = hObject;
%全局变量声明,初始化;
handles.ComP = serial('COM1'); %对象指针
handles.ComState = 'closed'; %对象存在状态:打开,关闭;
handles.ComRData(1,:) = [0,0]; %接收到的数据;无限缓冲;每帧2个字符;
handles.ComRCount = 0; %接收到的帧数;
handles.ComTData(1,:) = [0,0]; %待发送的数据;无限缓冲;
handles.ComTCount = 0; %待发送的帧数;
handles.x=0;
handles.y=0;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes uart2 wait for user response (see UIRESUME)
% uiwait(handles.uart2);
% --- Outputs from this function are returned to the command line.
function varargout = uart2_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 UOpen.
function UOpen_Callback(hObject, eventdata, handles)
% hObject handle to UOpen (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
state = get(handles.UOpen,'string');
fprintf('%s\n',state);
if strcmp(state, 'Open')
handles.ComP.BytesAvailableFcnMode = 'byte';
handles.ComP.BytesAvailableFcnCount=2;
handles.ComP.BytesAvailableFcn = {@record,hObject,handles};
fopen(handles.ComP);
handles.ComState = 'open';
%fprintf(handles.ComP,'\n');
set(handles.UOpen,'string', 'Close');
else
fclose(handles.ComP);
handles.ComState = 'closed';
set(handles.UOpen,'string', 'Open');
end
guidata(hObject, handles);
%串口复位程序
% --- Executes on button press in UReset.
function UReset_Callback(hObject, eventdata, handles)
% hObject handle to UReset (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp(get(handles.ComP,'Status'),'open')
fclose(handles.ComP);
end
fopen(handles.ComP);
handles.ComState = 'open';
fprintf('UART OPEN,\n');
fprintf(handles.ComP,'UART OPEN\n');
set(handles.UOpen,'string', 'Close');
% --- Executes during object deletion, before destroying properties.
function uart2_DeleteFcn(hObject, eventdata, handles)
% hObject handle to uart2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%发送:二进制数据,同步发送;
% --- Executes on button press in USend.
function USend_Callback(hObject, eventdata, handles)
% hObject handle to USend (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
sdata=strread(get(handles.SData,'string'));
fwrite(handles.ComP, sdata, 'uint8');
handles.ComTData=sdata;
guidata(hObject,handles);
% --- Executes on button press in URec.
function URec_Callback(hObject, eventdata, handles)
% hObject handle to URec (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
num = handles.ComP.BytesAvailable;
fprintf('%d\n',num);
if num<0
rdata=fread(handles.ComP,num,'uint8');
set(handles.RData,'string',num2str(rdata));
fprintf('%d;',rdata);
end
handles.ComRCount = handles.ComRCount+1;
set(handles.RData,'string',num2str(handles.ComRCount));
guidata(hObject, handles);
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (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
function SData_Callback(hObject, eventdata, handles)
% hObject handle to SData (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of SData as text
% str2double(get(hObject,'String')) returns contents of SData as a double
% --- Executes during object creation, after setting all properties.
function SData_CreateFcn(hObject, eventdata, handles)
% hObject handle to SData (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit 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
% --- Executes when user attempts to close uart2.
function uart2_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to uart2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
if strcmp(get(handles.ComP,'Status'),'open')
fclose(handles.ComP);
end
delete (handles.ComP);
clear handles.ComP;
guidata(hObject, handles);
delete(hObject);
%?怎样获得GUI的数据结构
function record(obj, event, hObject,handles)
name = get(obj, 'Name');
fprintf(name);
fprintf('\n');
%接收数据
rdata=fread(obj,2,'uint8');
fprintf('%d;',rdata);
fprintf('\n');
%更改GUI数据;GUI的数据没有传进来也没有传出去;
%num=get(handles.RData,'string');
%handles.ComRCount = handles.ComRCount+2;
%guidata(hObject, handles);
%绘图程序
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -