|
在操的时候,模块会返回数据到串口通过读取串口的数据,分析模块的运行。利用matla读取串口数据的能力,并加上其强大的绘图能力,可以让数据更为直观的可视化,便于用户更加了解这个模块的运行情况,方便调试。
- function varargout = comtest2(varargin)
- % COMTEST2 MATLAB code for comtest2.fig
- % COMTEST2, by itself, creates a new COMTEST2 or raises the existing
- % singleton*.
- %
- % H = COMTEST2 returns the handle to a new COMTEST2 or the handle to
- % the existing singleton*.
- %
- % COMTEST2('CALLBACK',hObject,eventData,handles,...) calls the local
- % function named CALLBACK in COMTEST2.M with the given input arguments.
- %
- % COMTEST2('Property','Value',...) creates a new COMTEST2 or raises the
- % existing singleton*. Starting from the left, property value pairs are
- % applied to the GUI before comtest2_OpeningFcn gets called. An
- % unrecognized property name or invalid value makes property application
- % stop. All inputs are passed to comtest2_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 comtest2
- % Last Modified by GUIDE v2.5 15-Dec-2010 16:18:59
- % Begin initialization code - DO NOT EDIT
- gui_Singleton = 1;
- gui_State = struct('gui_Name', mfilename, ...
- 'gui_Singleton', gui_Singleton, ...
- 'gui_OpeningFcn', @comtest2_OpeningFcn, ...
- 'gui_OutputFcn', @comtest2_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 comtest2 is made visible.
- function comtest2_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 comtest2 (see VARARGIN)
- % Choose default command line output for comtest2
- handles.output = hObject;
- com = get(handles.popupmenu5,'value');
- com = strcat('COM',num2str(com));
- handles.serial = serial(com);
- % Update handles structure
- guidata(hObject, handles);
- % UIWAIT makes comtest2 wait for user response (see UIRESUME)
- % uiwait(handles.figure1);
- % --- Outputs from this function are returned to the command line.
- function varargout = comtest2_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 togglebutton1.
- function togglebutton1_Callback(hObject, eventdata, handles) %这是打开串口的开关,打开后开始读取数据,并使能callback函数
- % hObject handle to togglebutton1 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- % Hint: get(hObject,'Value') returns toggle state of togglebutton1
- sta = get(hObject,'value');
- switch sta
- case 0
- try
- fclose(handles.serial);
- delete(handles.serial);
- guidata(hObject,handles);
- catch
- msgbox('出错,串口不存在,或其他软件使用该串口','警告','warn');
- set(handles.togglebutton1,'value',1);
- end
- case 1
- try
- com = get(handles.popupmenu5,'value');
- com = strcat('COM',num2str(com));
- br = 1200*get(handles.popupmenu1,'value');
- sbs = get(handles.popupmenu2,'value');
- parit = get(handles.popupmenu3,'value');
- switch parit
- case 1
- strpar = 'odd';
- case 2
- strpar = 'even';
- case 3
- strpar = 'none';
- end
- datbit = 5+get(handles.popupmenu4,'value');
- handles.serial = serial(com,'baudrate',br,'parity',strpar,'databits',datbit,'stopbits',sbs);
- set(handles.serial,'InputBufferSize',16);
- set(handles.serial,'BytesAvailableFcnMode','byte');
- set(handles.serial,'BytesAvailableFcncount',16);
- guidata(hObject,handles);
- set(handles.serial,'BytesAvailableFcn',{@mycom,handles});
- fopen(handles.serial);
- catch
- msgbox('出错,串口不存在,或其他软件使用该串口','警告','warn');
- set(handles.togglebutton1,'value',0);
- end
- end
- function mycom(hObject,eventdata,handles) %这是串口读取16个数据后执行的callback函数
- newd = fread(handles.serial);
- axes(handles.axes1);
- plot(newd);
- % --- Executes on selection change in popupmenu5.
- function popupmenu5_Callback(hObject, eventdata, handles)
- % hObject handle to popupmenu5 (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 popupmenu5 contents as cell array
- % contents{get(hObject,'Value')} returns selected item from popupmenu5
- % --- Executes during object creation, after setting all properties.
- function popupmenu5_CreateFcn(hObject, eventdata, handles)
- % hObject handle to popupmenu5 (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
复制代码
|
|