MATLAB是一个十分强大的工具,用来制作串口上位机也是不错的选择。虽然一般的上位机是用的C#或者C++编写的,这两者具有强大的系统操作能力或便捷灵活的通信和控制开发能力。但是对于我这种偏硬小白,matlab的GUI界面就显得更加友好了一些。所以最后选择了以MATLAB作为开发工具写一个自己的上位机。
function varargout = uartchat(varargin)
% UARTCHAT MATLAB code for uartchat.fig
% UARTCHAT, by itself, creates a new UARTCHAT or raises the existing
% singleton*.
%
% H = UARTCHAT returns the handle to a new UARTCHAT or the handle to
% the existing singleton*.
%
% UARTCHAT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UARTCHAT.M with the given input arguments.
%
% UARTCHAT('Property','Value',...) creates a new UARTCHAT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before uartchat_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to uartchat_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 uartchat
% Last Modified by GUIDE v2.5 23-Sep-2019 10:39:30
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @uartchat_OpeningFcn, ...
'gui_OutputFcn', @uartchat_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
endif 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 uartchat is made visible.
function uartchat_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 uartchat (see VARARGIN)% Choose default command line output for uartchat
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);% UIWAIT makes uartchat wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = uartchat_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 clearbutton.
function clearbutton_Callback(hObject, eventdata, handles)
% hObject handle to clearbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.receivemessage,'String','');
clear a;
% --- Executes on button press in openbutton.
function openbutton_Callback(hObject, eventdata, handles)
% hObject handle to openbutton (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',9600,'Parity','none','stopbits',1,'databits',8,'FlowControl','none');
global rate;
set(handles.device_port_current,'BaudRate',rate);
set(handles.device_port_current,'DataBits',8);
set(handles.device_port_current,'StopBits',1);
set(handles.device_port_current,'Parity','none');
set(handles.device_port_current,'FlowControl','none');
handles.device_port_current.BytesAvailableFcnCount = 1;
handles.device_port_current.BytesAvailableFcnMode = 'byte';
handles.device_port_current.BytesAvailableFcn = {@ReceiveBytesAvailableFcn,handles};% handles.output=hObject;
guidata(hObject,handles);fopen(handles.device_port_current);
set(handles.openbutton,'enable','off');%打开串口后,该按钮变灰,无法再次点击该按钮
set(handles.commenu,'enable','off');
set(handles.closebutton,'enable','on');
guidata(hObject,handles);% --- Executes on button press in closebutton.
function closebutton_Callback(hObject, eventdata, handles)
% hObject handle to closebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fclose(handles.device_port_current);
set(handles.openbutton,'enable','on');%打开串口后,该按钮变灰,无法再次点击该按钮
set(handles.commenu,'enable','on');
set(handles.closebutton,'enable','off');
clear a;
guidata(hObject,handles);% --- Executes on button press in sendbutton.
function sendbutton_Callback(hObject, eventdata, handles)
% hObject handle to sendbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
senddata=get(handles.sendmessage,'String');
fprintf(handles.device_port_current,'%s',senddata);% --- Executes on selection change in commenu.
function commenu_Callback(hObject, eventdata, handles)
% hObject handle to commenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)% Hints: contents = cellstr(get(hObject,'String')) returns commenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from commenu
str=get(hObject,'String');
val=get(hObject,'value');
switch str{val}
case 'COM1'
handles.device_port_current=serial('COM1');
case 'COM2'
handles.device_port_current=serial('COM2');
case 'COM3'
handles.device_port_current=serial('COM3');
case 'COM4'
handles.device_port_current=serial('COM4');
case 'COM5'
handles.device_port_current=serial('COM5');
case 'COM6'
handles.device_port_current=serial('COM6');
case 'COM7'
handles.device_port_current=serial('COM7');
case 'COM8'
handles.device_port_current=serial('COM8');
case 'COM9'
handles.device_port_current=serial('COM9');
end
guidata(hObject,handles);%保存配置% --- Executes during object creation, after setting all properties.
function commenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to commenu (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 selection change in baudmenu2.
function baudmenu2_Callback(hObject, eventdata, handles)
% hObject handle to baudmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)% Hints: contents = cellstr(get(hObject,'String')) returns baudmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from baudmenu2
global rate;
val2=get(hObject,'value');
switch val2
case 2
rate=1200;
case 3
rate=4800;
case 4
rate=9600;
end
%set(handles.receivemessage,'String',rate);
guidata(hObject,handles);% --- Executes during object creation, after setting all properties.
function baudmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to baudmenu2 (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');
endfunction sendmessage_Callback(hObject, eventdata, handles)
% hObject handle to sendmessage (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 sendmessage as text
% str2double(get(hObject,'String')) returns contents of sendmessage as a double
sendtext=get(handles.sendmessage,'String');
fprintf(handles.device_port_current,'%s',sendtext);% --- Executes during object creation, after setting all properties.
function sendmessage_CreateFcn(hObject, eventdata, handles)
% hObject handle to sendmessage (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');
endfunction ReceiveBytesAvailableFcn(hObject,evendata,handles)
global a;
Receivedata=setstr(fread(handles.device_port_current,1));
a=strcat(a,Receivedata);
set(handles.receivemessage,'String',a);
这就是成品图案了,如果需要更换颜色背景什么的可以自己在GUI界面中进行修改。
注意在使用的过程中请一定要插在对应的串口上,如果你没有接入正确的串口或者说接入了串口被其他软件占用了,matlab的命令行窗口会报错说你的串口无法使用。如果你接好了并且串口并没有被占用它仍然报错的话,我个人也出现过这样的情况。我不知道是不是MATLAB的个人bug,不过重启matlab客户端是个不错的解决方法,就是略微有点麻烦。。。
笔者的上位机也还有一点缺陷,那就是每次清除串口接收区的消息后他会清除一次后之前的全部信息又回到了接收消息框,本人认为是接收缓冲区的锅。但是在网上咨询了一番发现好像没有人有我这种问题,我改了接收缓冲区的大小但是好像没有什么卵用。如果有大佬看到这篇文章,敬请指点matlab小白。