MATLAB语言的串口助手
来源:互联网 发布:唐斯大学数据 编辑:程序博客网 时间:2024/04/27 15:39
1,在命令行下运行guide命令,在向导中单击Create New GUI,选择Blank GUI(Default);然后在下面选择保存路径,命名文件名为Serial_Port;完成之后点击OK。
2,编辑图形界面,如下图所示,在左侧选择控件,拖到右侧区域中,右键设置属性;
3,编辑源代码,在上面菜单选项中,单击右侧第4个M-file Editor按钮,开始编辑Serial_Port.m文件。
M文件源代码如下所示,请注意控件编号与函数号相对应。
function varargout = Serial_Port(varargin)% SERIAL_PORT M-file for Serial_Port.fig% SERIAL_PORT, by itself, creates a new SERIAL_PORT or raises the existing% singleton*.%% H = SERIAL_PORT returns the handle to a new SERIAL_PORT or the handle to% the existing singleton*.%% SERIAL_PORT('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in SERIAL_PORT.M with the given input arguments.%% SERIAL_PORT('Property','Value',...) creates a new SERIAL_PORT or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before Serial_Port_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to Serial_Port_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 Serial_Port% Last Modified by GUIDE v2.5 17-Jun-2015 10:13:45% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @Serial_Port_OpeningFcn, ... 'gui_OutputFcn', @Serial_Port_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 Serial_Port is made visible.function Serial_Port_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 Serial_Port (see VARARGIN)% Choose default command line output for Serial_Porthandles.output = hObject;% Update handles structureguidata(hObject, handles);%{关闭、删除已打开的串口对象scoms=instrfind;fclose(scoms);delete(scoms);clear scoms;%}%获取可用串口global getcomgetcom=instrhwinfo('serial');set(handles.popupmenu1,'string',char(getcom.AvailableSerialPorts));global aa=clock;% UIWAIT makes Serial_Port wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = Serial_Port_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)function edit1_Callback(hObject, eventdata, handles)% hObject handle to edit1 (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 edit1 as text% str2double(get(hObject,'String')) returns contents of edit1 as a double% --- Executes during object creation, after setting all properties.function edit1_CreateFcn(hObject, eventdata, handles)% hObject handle to edit1 (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 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 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 on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%获取当前按钮的名字string=get(handles.pushbutton1,'string');if(strcmp(string,'打开串口')==1) set(handles.pushbutton1,'string','关闭串口'); %禁止修改串口号和波特率 set(handles.popupmenu1,'Enable','off'); set(handles.popupmenu2,'Enable','off'); %getcom=instrhwinfo('serial'); global getcom global scom %选择串口号 if size(getcom.AvailableSerialPorts)==0 errordlg('无有效COM口','提示','replace'); else if size(getcom.AvailableSerialPorts)==1 scom=serial(char(getcom.AvailableSerialPorts)); else for i=1:size(getcom.AvailableSerialPorts) if i==get(handles.popupmenu1,'Value') scom=serial(char(getcom.AvailableSerialPorts(i))); break; end; end; end; end; %设置串口波特率 switch get(handles.popupmenu2,'Value') case 1 scom.BaudRate=115200; case 2 scom.BaudRate=57600; case 3 scom.BaudRate=56000; case 4 scom.BaudRate=43000; case 5 scom.BaudRate=38400; case 6 scom.BaudRate=19200; case 7 scom.BaudRate=9600; case 8 scom.BaudRate=4800; case 9 scom.BaudRate=2400; case 10 scom.BaudRate=1200; case 11 scom.BaudRate=600; case 12 scom.BaudRate=300; end; %scom.terminator='CR'; scom.BytesAvailableFcnMode='terminator'; scom.BytesAvailableFcn={@mycallback,handles}; fopen(scom);else %按键的名字设为‘打开串口’ set(handles.pushbutton1,'string','打开串口'); %禁止修改串口号和波特率 set(handles.popupmenu1,'Enable','on'); set(handles.popupmenu2,'Enable','on'); %查找串口对象 scoms = instrfind; %尝试停止、关闭删除串口对象 stopasync(scoms); fclose(scoms); delete(scoms);end;% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global scom;%得到发送区的数据send_data=get(handles.edit2,'string');global a;a=GetSecs;fprintf(scom,send_data);% --- 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 = cellstr(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% --- Executes on selection change in popupmenu2.function popupmenu2_Callback(hObject, eventdata, handles)% hObject handle to popupmenu2 (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 popupmenu2 contents as cell array% contents{get(hObject,'Value')} returns selected item from popupmenu2% --- Executes during object creation, after setting all properties.function popupmenu2_CreateFcn(hObject, eventdata, handles)% hObject handle to popupmenu2 (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 mycallback(scom,BytesAvailable,handles)n_bytes=get(scom,'BytesAvailable');if n_bytes>0 char=fscanf(scom);end;original=get(handles.edit1,'string');char=strcat(original,char);set(handles.edit1,'string',char);global a;set(handles.edit1,'string',num2char(GetSecs-a));function edit3_Callback(hObject, eventdata, handles)% hObject handle to edit3 (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 edit3 as text% str2double(get(hObject,'String')) returns contents of edit3 as a double% --- Executes during object creation, after setting all properties.function edit3_CreateFcn(hObject, eventdata, handles)% hObject handle to edit3 (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
1 0
- MATLAB语言的串口助手
- matlab 串口小助手软件
- 串口助手
- c#的串口开发调试助手
- Qt写的串口调试助手
- MFC实现的串口通信助手
- PjComm:控制台版本的串口调试助手
- MFC实现的串口通信助手
- MFC实现的串口通信助手
- C#编写的串口调试助手
- 基于QT4的linux串口助手
- 支持lua脚本的串口调试助手
- Qt5.6实现的串口调试助手
- 简单的串口助手程序开发
- C# 下串口助手的实现
- C#打造属于自己的串口助手!
- 串口调试助手和VSPE虚拟串口的一些用法
- 虚拟串口软件和串口调试助手的简单使用
- POJ-2039
- 【人工智能】农夫过河问题
- HDOJ 题目1576 A/B(拓展欧几里得求乘法逆元)
- 顺序表的插入,删除算法
- 呵呵,双城记
- MATLAB语言的串口助手
- U-Boot配置过程
- 如何避免block中不停的使用weak self
- 【百度】写一个字符串逆序的程序,时间复杂度和空间复杂度最低,效率越高越好
- hdu 2602
- linux终端快捷键
- 自定义控件
- 如何点击listView的item跳转到指定的Activity
- 黑马程序员--OC的Foundation-结构体