function varargout = COM(varargin)
% COM MATLAB code for COM.fig
% COM, by itself, creates a new COM or raises the existing
% singleton*.
%
% H = COM returns the handle to a new COM or the handle to
% the existing singleton*.
%
% COM('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in COM.M with the given input arguments.
%
% COM('Property','Value',...) creates a new COM or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before COM_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to COM_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 COM
% Last Modified by GUIDE v2.5 05-Mar-2019 17:28:05
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @COM_OpeningFcn, ...
'gui_OutputFcn', @COM_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 COM is made visible.
function COM_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 COM (see VARARGIN)
% Choose default command line output for COM
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes COM wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = COM_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 selection change in ListMenu_COM_Port.
function ListMenu_COM_Port_Callback(hObject, eventdata, handles)
% hObject handle to ListMenu_COM_Port (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 选择端口号
str = get(hObject,'String');
val = get(hObject,'Value');
% 设置端口号
switch str{val}
case 'COM1' %选择COM端口
fprintf('Select COM1\n');
handles.device_port_current = serial('COM1');
case 'COM2'
fprintf('Select COM2\n');
handles.device_port_current = serial('COM2');
case 'COM3'
fprintf('Select COM3\n');
handles.device_port_current = serial('COM3');
case 'COM4'
fprintf('Select COM4\n');
handles.device_port_current = serial('COM4');
case 'COM5'
fprintf('Select COM5\n');
handles.device_port_current = serial('COM5');
case 'COM6'
fprintf('Select COM6\n');
handles.device_port_current = serial('COM6');
case 'COM7' %选择COM端口
fprintf('Select COM7\n');
handles.device_port_current = serial('COM7');
case 'COM8'
fprintf('Select COM8\n');
handles.device_port_current = serial('COM8');
case 'COM9'
fprintf('Select COM9\n');
handles.device_port_current = serial('COM9');
case 'COM10'
fprintf('Select COM10\n');
handles.device_port_current = serial('COM10');
case 'COM11'
fprintf('Select COM11\n');
handles.device_port_current = serial('COM11');
case 'COM12'
fprintf('Select COM12\n');
handles.device_port_current = serial('COM12');
case 'COM13'
fprintf('Select COM13\n');
handles.device_port_current = serial('COM13');
end
guidata(hObject,handles);%保存配置
% Hints: contents = cellstr(get(hObject,'String')) returns ListMenu_COM_Port contents as cell array
% contents{get(hObject,'Value')} returns selected item from ListMenu_COM_Port
% --- Executes during object creation, after setting all properties.
function ListMenu_COM_Port_CreateFcn(hObject, eventdata, handles)
% hObject handle to ListMenu_COM_Port (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
% --- Executes on button press in OpenSerialPort.
function OpenSerialPort_Callback(hObject, eventdata, handles)
% hObject handle to OpenSerialPort (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 打开并配置串口
set(handles.device_port_current,'BaudRate',115200,'Parity','none','stopbits',1,'databits',8,'FlowControl','none');%配置串口波特率相关属性
handles.device_port_current.BytesAvailableFcnCount = 512; %每接收512个数据就进入接收回调函数(类似于CCS里的ISR函数)
handles.device_port_current.BytesAvailableFcnMode = 'byte'; %配置接收数据模式
handles.device_port_current.BytesAvailableFcn = {@RecieveBytesAvailableFcn,handles}; %配置接收回调函数
handles.output = hObject;
guidata(hObject, handles); %保存配置
%open the serial port
fopen(handles.device_port_current); %打开串口
set(handles.OpenSerialPort,'enable','off'); %打开串口后,“打开串口”按钮变灰,无法点击该“打开串口”按钮
set(handles.ListMenu_COM_Port,'enable','off'); %打开串口后,“COM端口”下拉菜单变灰,锁定打开的COM串口
set(handles.CloseSerialPort,'enable','on'); %打开串口后,“关闭串口”按钮启动,此时可以点击此按钮,来关闭刚打开的串口
CurrentPortName = handles.device_port_current.name;
fprintf(CurrentPortName);
fprintf(' is opened!\n');
% --- Executes on button press in CloseSerialPort.
function CloseSerialPort_Callback(hObject, eventdata, handles)
% hObject handle to CloseSerialPort (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 关闭串口
% close the serial port
fclose(handles.device_port_current); %关闭串口
set(handles.OpenSerialPort,'enable','on'); %关闭串口后,“打开串口”按钮启动,此时可以点击此按钮,来打开的要开启的串口
set(handles.ListMenu_COM_Port,'enable','on'); %关闭串口后,“COM端口”下拉菜单启动,可选择要打开的COM串口
set(handles.CloseSerialPort,'enable','off'); %打开串口后,“关闭串口”按钮变灰,无法点击该“关闭串口”按钮
CurrentPortName = handles.device_port_current.name;
%closeSerial(CurrentPortName); %关闭串口
fprintf(CurrentPortName);
fprintf(' is closed!\n');
% --- Executes on button press in SendTest.
function SendTest_Callback(hObject, eventdata, handles)
% hObject handle to SendTest (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function RecieveBytesAvailableFcn(hObject, eventdata, handles)
[ReceiveData, ReceiveData_count] = fread(handles.device_port_current,512,'int8');
plotdata_count = ReceiveData_count/2; %接
Matlab 串口通信GUI
需积分: 46 200 浏览量
2019-03-17
20:16:54
上传
评论 7
收藏 27KB ZIP 举报
嘿嘿哈哈过日
- 粉丝: 11
- 资源: 3