找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1833|回复: 2
打印 上一主题 下一主题
收起左侧

MATLAB GUI设计之读取串口数据并计算绘图程序

[复制链接]
跳转到指定楼层
楼主
在操的时候,模块会返回数据到串口通过读取串口的数据,分析模块的运行。利用matla读取串口数据的能力,并加上其强大的绘图能力,可以让数据更为直观的可视化,便于用户更加了解这个模块的运行情况,方便调试。
  1. function varargout = comtest2(varargin)
  2. % COMTEST2 MATLAB code for comtest2.fig
  3. %      COMTEST2, by itself, creates a new COMTEST2 or raises the existing
  4. %      singleton*.
  5. %
  6. %      H = COMTEST2 returns the handle to a new COMTEST2 or the handle to
  7. %      the existing singleton*.
  8. %
  9. %      COMTEST2('CALLBACK',hObject,eventData,handles,...) calls the local
  10. %      function named CALLBACK in COMTEST2.M with the given input arguments.
  11. %
  12. %      COMTEST2('Property','Value',...) creates a new COMTEST2 or raises the
  13. %      existing singleton*.  Starting from the left, property value pairs are
  14. %      applied to the GUI before comtest2_OpeningFcn gets called.  An
  15. %      unrecognized property name or invalid value makes property application
  16. %      stop.  All inputs are passed to comtest2_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 comtest2

  23. % Last Modified by GUIDE v2.5 15-Dec-2010 16:18:59

  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', @comtest2_OpeningFcn, ...
  29.                    'gui_OutputFcn',  @comtest2_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 comtest2 is made visible.
  42. function comtest2_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 comtest2 (see VARARGIN)

  48. % Choose default command line output for comtest2
  49. handles.output = hObject;
  50. com = get(handles.popupmenu5,'value');  
  51. com = strcat('COM',num2str(com));
  52. handles.serial = serial(com);
  53. % Update handles structure
  54. guidata(hObject, handles);

  55. % UIWAIT makes comtest2 wait for user response (see UIRESUME)
  56. % uiwait(handles.figure1);


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

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

  65. .......省略了下拉菜单的函数回调函数.......


  66. % --- Executes on button press in togglebutton1.
  67. function togglebutton1_Callback(hObject, eventdata, handles)       %这是打开串口的开关,打开后开始读取数据,并使能callback函数
  68. % hObject    handle to togglebutton1 (see GCBO)
  69. % eventdata  reserved - to be defined in a future version of MATLAB
  70. % handles    structure with handles and user data (see GUIDATA)

  71. % Hint: get(hObject,'Value') returns toggle state of togglebutton1
  72. sta = get(hObject,'value');
  73. switch sta
  74.     case 0
  75. try   
  76.             fclose(handles.serial);
  77.             delete(handles.serial);
  78.             guidata(hObject,handles);
  79. catch
  80. msgbox('出错,串口不存在,或其他软件使用该串口','警告','warn');
  81. set(handles.togglebutton1,'value',1);
  82. end
  83.     case 1
  84. try
  85.             com = get(handles.popupmenu5,'value');
  86.             com = strcat('COM',num2str(com));
  87.             br = 1200*get(handles.popupmenu1,'value');
  88.             sbs = get(handles.popupmenu2,'value');
  89.             parit = get(handles.popupmenu3,'value');
  90.             switch parit
  91.                 case 1
  92.                     strpar = 'odd';
  93.                 case 2
  94.                     strpar = 'even';
  95.                 case 3
  96.                     strpar = 'none';
  97.             end
  98.             datbit = 5+get(handles.popupmenu4,'value');
  99.             handles.serial = serial(com,'baudrate',br,'parity',strpar,'databits',datbit,'stopbits',sbs);
  100.             set(handles.serial,'InputBufferSize',16);
  101.             set(handles.serial,'BytesAvailableFcnMode','byte');
  102.             set(handles.serial,'BytesAvailableFcncount',16);
  103.             guidata(hObject,handles);
  104.             set(handles.serial,'BytesAvailableFcn',{@mycom,handles});
  105.             fopen(handles.serial);
  106. catch
  107. msgbox('出错,串口不存在,或其他软件使用该串口','警告','warn');
  108. set(handles.togglebutton1,'value',0);
  109. end
  110. end

  111. function mycom(hObject,eventdata,handles)   %这是串口读取16个数据后执行的callback函数
  112. newd = fread(handles.serial);
  113. axes(handles.axes1);
  114. plot(newd);

  115. % --- Executes on selection change in popupmenu5.
  116. function popupmenu5_Callback(hObject, eventdata, handles)
  117. % hObject    handle to popupmenu5 (see GCBO)
  118. % eventdata  reserved - to be defined in a future version of MATLAB
  119. % handles    structure with handles and user data (see GUIDATA)

  120. % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu5 contents as cell array
  121. %        contents{get(hObject,'Value')} returns selected item from popupmenu5


  122. % --- Executes during object creation, after setting all properties.
  123. function popupmenu5_CreateFcn(hObject, eventdata, handles)
  124. % hObject    handle to popupmenu5 (see GCBO)
  125. % eventdata  reserved - to be defined in a future version of MATLAB
  126. % handles    empty - handles not created until after all CreateFcns called

  127. % Hint: popupmenu controls usually have a white background on Windows.
  128. %       See ISPC and COMPUTER.
  129. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  130.     set(hObject,'BackgroundColor','white');
  131. end
复制代码


chuankou.rar

5.24 KB, 下载次数: 38, 下载积分: 黑币 -5

MATLAB代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:549028 发表于 2019-5-28 09:43 | 只看该作者
谢谢分享
回复

使用道具 举报

板凳
ID:485411 发表于 2019-8-6 11:09 | 只看该作者
感谢楼主 困扰了许久终于解决了
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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