找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6428|回复: 7
收起左侧

关于stm32f4与Matlab串口通信 Matlab文件

[复制链接]
ID:202249 发表于 2017-5-18 16:54 | 显示全部楼层 |阅读模式
关于stm32f4与Matlab串口通信 Matlab文件
0.png

源程序如下:
  1. function varargout = Gui_Datadisplay(varargin)
  2. % GUI_DATADISPLAY MATLAB code for Gui_Datadisplay.fig
  3. %      GUI_DATADISPLAY, by itself, creates a new GUI_DATADISPLAY or raises the existing
  4. %      singleton*.
  5. %
  6. %      H = GUI_DATADISPLAY returns the handle to a new GUI_DATADISPLAY or the handle to
  7. %      the existing singleton*.
  8. %
  9. %      GUI_DATADISPLAY('CALLBACK',hObject,eventData,handles,...) calls the local
  10. %      function named CALLBACK in GUI_DATADISPLAY.M with the given input arguments.
  11. %
  12. %      GUI_DATADISPLAY('Property','Value',...) creates a new GUI_DATADISPLAY or raises the
  13. %      existing singleton*.  Starting from the left, property value pairs are
  14. %      applied to the GUI before Gui_Datadisplay_OpeningFcn gets called.  An
  15. %      unrecognized property name or invalid value makes property application
  16. %      stop.  All inputs are passed to Gui_Datadisplay_OpeningFcn via varargin.
  17. %
  18. %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
  19. %      instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES

  22. % Edit the above text to modify the response to help Gui_Datadisplay

  23. % Last Modified by GUIDE v2.5 17-Jun-2015 14:11:40

  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct('gui_Name',       mfilename, ...
  27.                    'gui_Singleton',  gui_Singleton, ...
  28.                    'gui_OpeningFcn', @Gui_Datadisplay_OpeningFcn, ...
  29.                    'gui_OutputFcn',  @Gui_Datadisplay_OutputFcn, ...
  30.                    'gui_LayoutFcn',  [] , ...
  31.                    'gui_Callback',   []);
  32. if nargin && ischar(varargin{1})
  33.     gui_State.gui_Callback = str2func(varargin{1});
  34. end

  35. if nargout
  36.     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38.     gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT


  41. % --- Executes just before Gui_Datadisplay is made visible.
  42. function Gui_Datadisplay_OpeningFcn(hObject, eventdata, handles, varargin)
  43. % This function has no output args, see OutputFcn.
  44. % hObject    handle to figure
  45. % eventdata  reserved - to be defined in a future version of MATLAB
  46. % handles    structure with handles and user data (see GUIDATA)
  47. % varargin   command line arguments to Gui_Datadisplay (see VARARGIN)

  48. global COM;
  49. global rate;
  50. COM='COM7'
  51. rate = 115200;
  52. set(handles.ppCOM,'value', 7);
  53. set(handles.ppBaudRates,'value',1);
  54. set(handles.pbCloseSerial,'Enable','off');

  55. % Choose default command line output for Gui_Datadisplay
  56. handles.output = hObject;

  57. % Update handles structure
  58. guidata(hObject, handles);

  59. % UIWAIT makes Gui_Datadisplay wait for user response (see UIRESUME)
  60. % uiwait(handles.figure1);


  61. % --- Outputs from this function are returned to the command line.
  62. function varargout = Gui_Datadisplay_OutputFcn(hObject, eventdata, handles)
  63. % varargout  cell array for returning output args (see VARARGOUT);
  64. % hObject    handle to figure
  65. % eventdata  reserved - to be defined in a future version of MATLAB
  66. % handles    structure with handles and user data (see GUIDATA)

  67. % Get default command line output from handles structure
  68. varargout{1} = handles.output;



  69. function Value_Callback(hObject, eventdata, handles)
  70. % hObject    handle to Value (see GCBO)
  71. % eventdata  reserved - to be defined in a future version of MATLAB
  72. % handles    structure with handles and user data (see GUIDATA)

  73. % Hints: get(hObject,'String') returns contents of Value as text
  74. %        str2double(get(hObject,'String')) returns contents of Value as a double


  75. % --- Executes during object creation, after setting all properties.
  76. function Value_CreateFcn(hObject, eventdata, handles)
  77. % hObject    handle to Value (see GCBO)
  78. % eventdata  reserved - to be defined in a future version of MATLAB
  79. % handles    empty - handles not created until after all CreateFcns called

  80. % Hint: edit controls usually have a white background on Windows.
  81. %       See ISPC and COMPUTER.
  82. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  83.     set(hObject,'BackgroundColor','white');
  84. end


  85. % --- Executes on button press in pbOpenSerial.
  86. function pbOpenSerial_Callback(hObject, eventdata, handles)
  87. % hObject    handle to pbOpenSerial (see GCBO)
  88. % eventdata  reserved - to be defined in a future version of MATLAB
  89. % handles    structure with handles and user data (see GUIDATA)
  90. clc
  91. instrreset
  92. global s;
  93. global rate;
  94. global COM;
  95. global out;
  96. out=1;

  97. s=serial(COM);
  98. set(s,'BaudRate',rate);%%%Baud初始化
  99. set(s,'DataBits',8);%%%设置数据长度
  100. set(s,'StopBits',1);%%%设置停止位长度
  101. set(s,'InputBufferSize',1024000);%%%设置输入缓冲区大小为1M
  102. set(handles.pbOpenSerial,'Enable','off');
  103. set(handles.pbCloseSerial,'Enable','on');

  104. %串口事件回调设置
  105. s.BytesAvailableFcnMode='terminator';
  106. s.BytesAvailableFcnCount=10; %输入缓冲区存在10个字节触发回调函数
  107. s.BytesAvailableFcn={@EveBytesAvailableFcn,handles};%回调函数的指定
  108. fopen(s);%打开串口

  109. global count;
  110. count=1;
  111. fprintf('ceshi_dakaichuankou\n');

  112. %回调函数
  113. function EveBytesAvailableFcn( t,event,handles )
  114. global s;
  115. global a;
  116. global d;
  117. global arrX;
  118. global arrY;
  119. global arrZ;
  120. global count;

  121. arrX=zeros([1,100]);
  122. % arrY=[1,1000];
  123. % arrZ=[1,1000];
  124. % fprintf('Receive callback');
  125. a=fscanf(s);
  126. M=textscan(a,'%s%d%d%d','delimiter',' ');
  127. % data = [data , '\n'];
  128. [Name,x,y,z] = deal(M{:});
  129. arrX(1,count) = x;
  130. % arrY(1,count) = y;
  131. % arrZ(1,count) = z;
  132. a = strcat(a ,char(10), get(handles.editR,'string'));
  133. set(handles.editR,'string',a);
  134. set(handles.text4,'string',x);
  135. plot(arrX);
  136. hold on;
  137. count=count+1;
  138. if count==100
  139.     count=1;
  140.     clf;
  141. end

  142. % --- Executes on selection change in ppCOM.
  143. function ppCOM_Callback(hObject, eventdata, handles)
  144. % hObject    handle to ppCOM (see GCBO)
  145. % eventdata  reserved - to be defined in a future version of MATLAB
  146. % handles    structure with handles and user data (see GUIDATA)

  147. % Hints: contents = cellstr(get(hObject,'String')) returns ppCOM contents as cell array
  148. %        contents{get(hObject,'Value')} returns selected item from ppCOM
  149. global COM;
  150. % COM=1;
  151. rate=1;
  152. val=get(hObject,'value');
  153. switch val
  154.     case 1
  155.         COM='COM1';
  156.         fprintf('ceshi_COM=1\n');
  157.     case 2
  158.         COM='COM2';
  159.     case 3
  160.         COM='COM3';
  161.     case 4
  162.         COM='COM4';
  163.     case 5
  164.         COM='COM5';
  165.     case 6
  166.         COM='COM6';
  167.     case 7
  168.         COM='COM7';
  169.         fprintf('ceshi_COM=7\n');
  170.     case 8
  171.         COM='COM8';
  172.     case 9
  173.         COM='COM9';
  174. end

  175. % --- Executes during object creation, after setting all properties.
  176. function ppCOM_CreateFcn(hObject, eventdata, handles)
  177. % hObject    handle to ppCOM (see GCBO)
  178. % eventdata  reserved - to be defined in a future version of MATLAB
  179. % handles    empty - handles not created until after all CreateFcns called


  180. % Hint: popupmenu controls usually have a white background on Windows.
  181. %       See ISPC and COMPUTER.
  182. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  183.     set(hObject,'BackgroundColor','white');
  184. end


  185. % --- Executes on selection change in ppBaudRates.
  186. function ppBaudRates_Callback(hObject, eventdata, handles)
  187. % hObject    handle to ppBaudRates (see GCBO)
  188. % eventdata  reserved - to be defined in a future version of MATLAB
  189. % handles    structure with handles and user data (see GUIDATA)

  190. % Hints: contents = cellstr(get(hObject,'String')) returns ppBaudRates contents as cell array
  191. %        contents{get(hObject,'Value')} returns selected item from ppBaudRates
  192. global rate;
  193. val=get(hObject,'value');
  194. switch val
  195.     case 1
  196.         rate=115200;
  197.     case 2
  198.         rate=9600;
  199. end

  200. % --- Executes during object creation, after setting all properties.
  201. function ppBaudRates_CreateFcn(hObject, eventdata, handles)
  202. % hObject    handle to ppBaudRates (see GCBO)
  203. % eventdata  reserved - to be defined in a future version of MATLAB
  204. % handles    empty - handles not created until after all CreateFcns called

  205. % Hint: popupmenu controls usually have a white background on Windows.
  206. %       See ISPC and COMPUTER.
  207. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  208.     set(hObject,'BackgroundColor','white');
  209. end


  210. % --- Executes on button press in pbCloseSerial.
  211. function pbCloseSerial_Callback(hObject, eventdata, handles)
  212. % hObject    handle to pbCloseSerial (see GCBO)
  213. % eventdata  reserved - to be defined in a future version of MATLAB
  214. % handles    structure with handles and user data (see GUIDATA)
  215. global s;
  216. fclose(s);
  217. delete(s);
  218. set(handles.pbOpenSerial,'Enable','on');
  219. set(handles.pbCloseSerial,'Enable','off');
  220. fprintf('Close_COM');


  221. function editR_Callback(hObject, eventdata, handles)
  222. % hObject    handle to editR (see GCBO)
  223. % eventdata  reserved - to be defined in a future version of MATLAB
  224. % handles    structure with handles and user data (see GUIDATA)

  225. % Hints: get(hObject,'String') returns contents of editR as text
  226. %        str2double(get(hObject,'String')) returns contents of editR as a double


  227. % --- Executes during object creation, after setting all properties.
  228. function editR_CreateFcn(hObject, eventdata, handles)
  229. % hObject    handle to editR (see GCBO)
  230. % eventdata  reserved - to be defined in a future version of MATLAB
  231. % handles    empty - handles not created until after all CreateFcns called

  232. % Hint: edit controls usually have a white background on Windows.
  233. %       See ISPC and COMPUTER.
  234. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  235.     set(hObject,'BackgroundColor','white');
  236. end


  237. % --- Executes on button press in pbClearR.
  238. function pbClearR_Callback(hObject, eventdata, handles)
  239. % hObject    handle to pbClearR (see GCBO)
  240. % eventdata  reserved - to be defined in a future version of MATLAB
  241. % handles    structure with handles and user data (see GUIDATA)
  242. set(handles.editR,'string','');
  243. set(handles.Value,'string','');
  244. set(handles.text4,'string','');


  245. % --- Executes on button press in pbClearT.
  246. function pbClearT_Callback(hObject, eventdata, handles)
  247. ……………………

  248. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
Gui_Data_display.zip (13.3 KB, 下载次数: 108)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:91907 发表于 2017-11-25 17:00 | 显示全部楼层
不错,有用
回复

使用道具 举报

ID:295119 发表于 2018-3-21 17:01 | 显示全部楼层
学习学习!!!
回复

使用道具 举报

ID:186122 发表于 2018-6-9 21:26 | 显示全部楼层
希望有用
回复

使用道具 举报

ID:186122 发表于 2018-6-9 21:32 | 显示全部楼层
楼主用的单片机型号是什么呀
回复

使用道具 举报

ID:414005 发表于 2018-10-23 16:35 | 显示全部楼层
希望有用 正在研究gui实时绘制
回复

使用道具 举报

ID:414065 发表于 2018-10-23 19:32 | 显示全部楼层
很强势
回复

使用道具 举报

ID:224496 发表于 2019-4-22 20:12 | 显示全部楼层
感谢分享,学习中。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表