關于stm32f4與Matlab串口通信 Matlab文件
0.png (61.87 KB, 下載次數: 94)
下載附件
2017-5-18 17:06 上傳
源程序如下:
- function varargout = Gui_Datadisplay(varargin)
- % GUI_DATADISPLAY MATLAB code for Gui_Datadisplay.fig
- % GUI_DATADISPLAY, by itself, creates a new GUI_DATADISPLAY or raises the existing
- % singleton*.
- %
- % H = GUI_DATADISPLAY returns the handle to a new GUI_DATADISPLAY or the handle to
- % the existing singleton*.
- %
- % GUI_DATADISPLAY('CALLBACK',hObject,eventData,handles,...) calls the local
- % function named CALLBACK in GUI_DATADISPLAY.M with the given input arguments.
- %
- % GUI_DATADISPLAY('Property','Value',...) creates a new GUI_DATADISPLAY or raises the
- % existing singleton*. Starting from the left, property value pairs are
- % applied to the GUI before Gui_Datadisplay_OpeningFcn gets called. An
- % unrecognized property name or invalid value makes property application
- % stop. All inputs are passed to Gui_Datadisplay_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 Gui_Datadisplay
- % Last Modified by GUIDE v2.5 17-Jun-2015 14:11:40
- % Begin initialization code - DO NOT EDIT
- gui_Singleton = 1;
- gui_State = struct('gui_Name', mfilename, ...
- 'gui_Singleton', gui_Singleton, ...
- 'gui_OpeningFcn', @Gui_Datadisplay_OpeningFcn, ...
- 'gui_OutputFcn', @Gui_Datadisplay_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 Gui_Datadisplay is made visible.
- function Gui_Datadisplay_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 Gui_Datadisplay (see VARARGIN)
- global COM;
- global rate;
- COM='COM7'
- rate = 115200;
- set(handles.ppCOM,'value', 7);
- set(handles.ppBaudRates,'value',1);
- set(handles.pbCloseSerial,'Enable','off');
- % Choose default command line output for Gui_Datadisplay
- handles.output = hObject;
- % Update handles structure
- guidata(hObject, handles);
- % UIWAIT makes Gui_Datadisplay wait for user response (see UIRESUME)
- % uiwait(handles.figure1);
- % --- Outputs from this function are returned to the command line.
- function varargout = Gui_Datadisplay_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;
- function Value_Callback(hObject, eventdata, handles)
- % hObject handle to Value (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 Value as text
- % str2double(get(hObject,'String')) returns contents of Value as a double
- % --- Executes during object creation, after setting all properties.
- function Value_CreateFcn(hObject, eventdata, handles)
- % hObject handle to Value (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 pbOpenSerial.
- function pbOpenSerial_Callback(hObject, eventdata, handles)
- % hObject handle to pbOpenSerial (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- clc
- instrreset
- global s;
- global rate;
- global COM;
- global out;
- out=1;
- s=serial(COM);
- set(s,'BaudRate',rate);%%%Baud初始化
- set(s,'DataBits',8);%%%設置數據長度
- set(s,'StopBits',1);%%%設置停止位長度
- set(s,'InputBufferSize',1024000);%%%設置輸入緩沖區大小為1M
- set(handles.pbOpenSerial,'Enable','off');
- set(handles.pbCloseSerial,'Enable','on');
- %串口事件回調設置
- s.BytesAvailableFcnMode='terminator';
- s.BytesAvailableFcnCount=10; %輸入緩沖區存在10個字節觸發回調函數
- s.BytesAvailableFcn={@EveBytesAvailableFcn,handles};%回調函數的指定
- fopen(s);%打開串口
- global count;
- count=1;
- fprintf('ceshi_dakaichuankou\n');
- %回調函數
- function EveBytesAvailableFcn( t,event,handles )
- global s;
- global a;
- global d;
- global arrX;
- global arrY;
- global arrZ;
- global count;
- arrX=zeros([1,100]);
- % arrY=[1,1000];
- % arrZ=[1,1000];
- % fprintf('Receive callback');
- a=fscanf(s);
- M=textscan(a,'%s%d%d%d','delimiter',' ');
- % data = [data , '\n'];
- [Name,x,y,z] = deal(M{:});
- arrX(1,count) = x;
- % arrY(1,count) = y;
- % arrZ(1,count) = z;
- a = strcat(a ,char(10), get(handles.editR,'string'));
- set(handles.editR,'string',a);
- set(handles.text4,'string',x);
- plot(arrX);
- hold on;
- count=count+1;
- if count==100
- count=1;
- clf;
- end
- % --- Executes on selection change in ppCOM.
- function ppCOM_Callback(hObject, eventdata, handles)
- % hObject handle to ppCOM (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 ppCOM contents as cell array
- % contents{get(hObject,'Value')} returns selected item from ppCOM
- global COM;
- % COM=1;
- rate=1;
- val=get(hObject,'value');
- switch val
- case 1
- COM='COM1';
- fprintf('ceshi_COM=1\n');
- case 2
- COM='COM2';
- case 3
- COM='COM3';
- case 4
- COM='COM4';
- case 5
- COM='COM5';
- case 6
- COM='COM6';
- case 7
- COM='COM7';
- fprintf('ceshi_COM=7\n');
- case 8
- COM='COM8';
- case 9
- COM='COM9';
- end
- % --- Executes during object creation, after setting all properties.
- function ppCOM_CreateFcn(hObject, eventdata, handles)
- % hObject handle to ppCOM (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 ppBaudRates.
- function ppBaudRates_Callback(hObject, eventdata, handles)
- % hObject handle to ppBaudRates (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 ppBaudRates contents as cell array
- % contents{get(hObject,'Value')} returns selected item from ppBaudRates
- global rate;
- val=get(hObject,'value');
- switch val
- case 1
- rate=115200;
- case 2
- rate=9600;
- end
- % --- Executes during object creation, after setting all properties.
- function ppBaudRates_CreateFcn(hObject, eventdata, handles)
- % hObject handle to ppBaudRates (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 pbCloseSerial.
- function pbCloseSerial_Callback(hObject, eventdata, handles)
- % hObject handle to pbCloseSerial (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- global s;
- fclose(s);
- delete(s);
- set(handles.pbOpenSerial,'Enable','on');
- set(handles.pbCloseSerial,'Enable','off');
- fprintf('Close_COM');
- function editR_Callback(hObject, eventdata, handles)
- % hObject handle to editR (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 editR as text
- % str2double(get(hObject,'String')) returns contents of editR as a double
- % --- Executes during object creation, after setting all properties.
- function editR_CreateFcn(hObject, eventdata, handles)
- % hObject handle to editR (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 pbClearR.
- function pbClearR_Callback(hObject, eventdata, handles)
- % hObject handle to pbClearR (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- set(handles.editR,'string','');
- set(handles.Value,'string','');
- set(handles.text4,'string','');
- % --- Executes on button press in pbClearT.
- function pbClearT_Callback(hObject, eventdata, handles)
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
Gui_Data_display.zip
(13.3 KB, 下載次數: 108)
2017-5-18 16:53 上傳
點擊文件名下載附件
Matlab文件 下載積分: 黑幣 -5
|