久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1988|回復: 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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 99re99| www.精品一区 | 亚洲三区在线 | 国产一级在线 | 天天色综网 | 91成人在线视频 | 成人免费在线视频 | 中文字幕视频在线看5 | 最新日韩在线视频 | 国产精品自拍啪啪 | 九九九视频在线 | 福利视频二区 | 激情六月丁香婷婷 | 欧美视频在线播放 | 日韩国产中文字幕 | 羞羞视频在线观看 | 国产精品亚洲综合 | 日韩av最新网址 | 日韩av在线免费 | 国产精品一区二区三区在线 | 欧美一极视频 | 国产精品一区二区三区在线播放 | 国产精品欧美一区二区三区不卡 | 91精品国产91久久综合桃花 | 国产日韩一区二区三区 | 一区二区三区四区在线视频 | 超碰在线播 | 久草成人网 | 国产一级淫片a直接免费看 免费a网站 | 日韩一区二区在线看 | 精品国产91乱码一区二区三区 | 中文字幕在线看 | 九色综合网| 久久久久国产精品 | 免费观看黄| 粉嫩在线 | 国产一区二区久久久 | 久久九精品 | 国产精品一区二区三 | 日韩一区二区三区在线看 | 亚洲欧美国产视频 |