function varargout = SerialPortCommunication(varargin)
% SERIALPORTCOMMUNICATION M-file for SerialPortCommunication.fig
% SERIALPORTCOMMUNICATION, by itself, creates a new SERIALPORTCOMMUNICATION or raises the existing
% singleton*.
%
% H = SERIALPORTCOMMUNICATION returns the handle to a new SERIALPORTCOMMUNICATION or the handle to
% the existing singleton*.
%
% SERIALPORTCOMMUNICATION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SERIALPORTCOMMUNICATION.M with the given input arguments.
%
% SERIALPORTCOMMUNICATION('Property','Value',...) creates a new SERIALPORTCOMMUNICATION or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before SerialPortCommunication_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to SerialPortCommunication_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 SerialPortCommunication
% Last Modified by GUIDE v2.5 05-Jun-2009 12:39:52
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @SerialPortCommunication_OpeningFcn, ...
'gui_OutputFcn', @SerialPortCommunication_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 when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (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
global number;
global o_SerialPort;
%--------------关闭串口-------------------------------
if strcmp(o_SerialPort.Status,'open')
fclose(o_SerialPort);
end
number=[];
clear number;
delete(hObject);
% --- Executes just before SerialPortCommunication is made visible.
function SerialPortCommunication_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 SerialPortCommunication (see VARARGIN)
% Choose default command line output for SerialPortCommunication
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes SerialPortCommunication wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = SerialPortCommunication_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 pb_OpenSerialPort.
function pb_OpenSerialPort_Callback(hObject, eventdata, handles)
% hObject handle to pb_OpenSerialPort (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%
global o_SerialPort;
%______________________________________________
%GUI全局变量
%按钮显示切换设置
set(hObject,'Visible','off');
set(handles.pb_CloseSerialPort,'Position',get(handles.pb_OpenSerialPort,'Position'));
set(handles.pb_CloseSerialPort,'Visible','on');
%---------------------串口初始化-----------------------
%%%COM端口初始化
int_Index_COM=get(handles.pop_SerialPort,'Value');
string_COM=get(handles.pop_SerialPort,'String');
string_Select_COM=string_COM{int_Index_COM};
o_SerialPort=serial(string_Select_COM);
%%%Baud初始化
int_Index_Baud=get(handles.pop_BaudRate,'Value');
string_Baud=get(handles.pop_BaudRate,'String');
string_Select_Baud=string_Baud{int_Index_Baud};
double_Baud=str2double(string_Select_Baud);
set(o_SerialPort,'BaudRate',double_Baud);
%%%设置数据长度
int_Index_DataBit=get(handles.pop_DataBit,'Value');
string_DataBit=get(handles.pop_DataBit,'String');
string_Select_DataBit=string_DataBit(int_Index_DataBit);
double_DataBit=str2double(string_Select_DataBit);
set(o_SerialPort,'DataBits',double_DataBit);
%%%设置停止位长度
int_Index_StopBits=get(handles.pop_StopBits,'Value');
string_StopBits=get(handles.pop_StopBits,'String');
string_Select_StopBits=string_StopBits(int_Index_StopBits);
double_StopBits=str2double(string_Select_StopBits);
set(o_SerialPort,'StopBits',double_StopBits);
%%%设置输入缓冲区大小为1M
set(o_SerialPort,'InputBufferSize',1024000);
%%%串口事件回调设置
o_SerialPort.BytesAvailableFcnMode='byte';
o_SerialPort.BytesAvailableFcnCount=10;
o_SerialPort.BytesAvailableFcn={@EveBytesAvailableFcn,handles};
% ----------------------打开串口-----------------------
fopen(o_SerialPort);
%----------------------发送按钮打开-----------------------
set(handles.pb_SendData,'Enable','on');
guidata(hObject, handles);
%BytesAvailableFcn事件回调函数
function EveBytesAvailableFcn( t,event,handles )
global o_SerialPort;
% number=h_o_SerialPort.BytesAvailable;
global number;
number=[number fread(o_SerialPort,10)'];
set(handles.edit_ReceiveData,'String',num2str(number));
% strNum=double2str(number);
% set(handles.edit_SendData,'String',number);
plot(handles.axes_main,1:length(number),number);
%发送按钮回调
% --- Executes on button press in pb_SendData.
function pb_SendData_Callback(hObject, eventdata, handles)
global o_SerialPort;
% hObject handle to pb_SendData (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
string_edit_SendData=get(handles.edit_SendData,'String');
if(get(handles.cb_CharOrHex,'Value')==get(handles.cb_CharOrHex,'Max'))
fprintf(o_SerialPort,string_edit_SendData);
else
double_edit_SendData=str2num(string_edit_SendData);
fwrite(o_SerialPort,double_edit_SendData);
end
% --- Executes on button press in pb_CloseSerialPort.
function pb_CloseSerialPort_Callback(hObject, eventdata, handles)
% hObject handle to pb_CloseSerialPort (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%---------------按钮显示切换设置-------------------
global o_SerialPort;
set(hObject,'Visible','off');
set(handles.pb_OpenSerialPort,'Visible','on');
%--------------关闭串口-------------------------------
if strcmp(o_SerialPort.Status,'open')
fclose(o_SerialPort);
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns calle
- 1
- 2
- 3
- 4
- 5
- 6
前往页