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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2522|回復: 1
收起左側

matlab gui 串口通訊實例

[復制鏈接]
ID:217021 發表于 2017-7-16 09:34 | 顯示全部樓層 |閱讀模式
0.png

matlab gui串口源程序如下:
  1. function varargout = seriesComm(varargin)
  2. % SERIESCOMM M-file for seriesComm.fig
  3. %      SERIESCOMM, by itself, creates a new SERIESCOMM or raises the existing
  4. %      singleton*.
  5. %
  6. %      H = SERIESCOMM returns the handle to a new SERIESCOMM or the handle to
  7. %      the existing singleton*.
  8. %
  9. %      SERIESCOMM('CALLBACK',hObject,eventData,handles,...) calls the local
  10. %      function named CALLBACK in SERIESCOMM.M with the given input arguments.
  11. %
  12. %      SERIESCOMM('Property','Value',...) creates a new SERIESCOMM or raises the
  13. %      existing singleton*.  Starting from the left, property value pairs are
  14. %      applied to the GUI before seriesComm_OpeningFunction gets called.  An
  15. %      unrecognized property name or invalid value makes property application
  16. %      stop.  All inputs are passed to seriesComm_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 seriesComm

  23. % Last Modified by GUIDE v2.5 16-Nov-2005 22:07:39

  24. % Begin initialization code - DO NOT EDIT

  25. %%Author: spirit3772,THUEE,NIP LAB,lks04@mails.tsinghua.edu.cn
  26. %%Date: 2005/11/16,23:00
  27. gui_Singleton = 1;
  28. gui_State = struct('gui_Name',       mfilename, ...
  29.                    'gui_Singleton',  gui_Singleton, ...
  30.                    'gui_OpeningFcn', @seriesComm_OpeningFcn, ...
  31.                    'gui_OutputFcn',  @seriesComm_OutputFcn, ...
  32.                    'gui_LayoutFcn',  [] , ...
  33.                    'gui_Callback',   []);
  34. if nargin && ischar(varargin{1})
  35.     gui_State.gui_Callback = str2func(varargin{1});
  36. end

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


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

  50. % Choose default command line output for seriesComm
  51. handles.output = hObject;

  52. %設置初始化參數
  53. set(handles.text3,'string','');
  54. set(handles.text4,'string','');
  55. set(handles.pushbutton4,'enable','off');
  56. set(handles.pushbutton5,'enable','off');
  57. set(handles.pushbutton6,'enable','off');
  58. set(handles.pushbutton1,'String','Open Serial Port','foreGroundColor',[1,0,0.5]);
  59. set(handles.checkbox1,'Value',0);
  60. set(handles.checkbox2,'Value',0);
  61. %設置handles中的一些變量,便于全局共享
  62. handles.displayT = 0;
  63. handles.displayR = 1;
  64. handles.baud_rate =  600;
  65. handles.com = 'COM1';
  66. handles.verifiedBits = 'NONE';
  67. handles.dataBits = 8;
  68. handles.stopBit = 1;
  69. handles.flowControl = 'NONE';
  70. handles.receiveString = '';
  71. % Update handles structure
  72. guidata(hObject, handles);

  73. % UIWAIT makes seriesComm wait for user response (see UIRESUME)
  74. % uiwait(handles.figure1);


  75. % --- Outputs from this function are returned to the command line.
  76. function varargout = seriesComm_OutputFcn(hObject, eventdata, handles)
  77. % varargout  cell array for returning output args (see VARARGOUT);
  78. % hObject    handle to figure
  79. % eventdata  reserved - to be defined in a future version of MATLAB
  80. % handles    structure with handles and user data (see GUIDATA)

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



  83. function edit1_Callback(hObject, eventdata, handles)
  84. % hObject    handle to edit1 (see GCBO)
  85. % eventdata  reserved - to be defined in a future version of MATLAB
  86. % handles    structure with handles and user data (see GUIDATA)

  87. % Hints: get(hObject,'String') returns contents of edit1 as text
  88. %        str2double(get(hObject,'String')) returns contents of edit1 as a double


  89. % --- Executes during object creation, after setting all properties.
  90. function edit1_CreateFcn(hObject, eventdata, handles)
  91. % hObject    handle to edit1 (see GCBO)
  92. % eventdata  reserved - to be defined in a future version of MATLAB
  93. % handles    empty - handles not created until after all CreateFcns called

  94. % Hint: edit controls usually have a white background on Windows.
  95. %       See ISPC and COMPUTER.
  96. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  97.     set(hObject,'BackgroundColor','white');
  98. end


  99. % --- Executes on button press in pushbutton2.
  100. function pushbutton2_Callback(hObject, eventdata, handles)
  101. % hObject    handle to pushbutton2 (see GCBO)
  102. % eventdata  reserved - to be defined in a future version of MATLAB
  103. % handles    structure with handles and user data (see GUIDATA)
  104. set(handles.edit1,'string','');


  105. function edit2_Callback(hObject, eventdata, handles)
  106. % hObject    handle to edit2 (see GCBO)
  107. % eventdata  reserved - to be defined in a future version of MATLAB
  108. % handles    structure with handles and user data (see GUIDATA)

  109. % Hints: get(hObject,'String') returns contents of edit2 as text




  110. % --- Executes during object creation, after setting all properties.
  111. function edit2_CreateFcn(hObject, eventdata, handles)
  112. % hObject    handle to edit2 (see GCBO)
  113. % eventdata  reserved - to be defined in a future version of MATLAB
  114. % handles    empty - handles not created until after all CreateFcns called

  115. % Hint: edit controls usually have a white background on Windows.
  116. %       See ISPC and COMPUTER.
  117. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  118.     set(hObject,'BackgroundColor','white');
  119. end


  120. % --- Executes on button press in pushbutton3.
  121. function pushbutton3_Callback(hObject, eventdata, handles)
  122. % hObject    handle to pushbutton3 (see GCBO)
  123. % eventdata  reserved - to be defined in a future version of MATLAB
  124. % handles    structure with handles and user data (see GUIDATA)
  125. set(handles.edit2,'string','');
  126. handles.recieveString = '';
  127. guidata(hObject,handles);

  128. % --- Executes on selection change in popupmenu1.
  129. function popupmenu1_Callback(hObject, eventdata, handles)
  130. % hObject    handle to popupmenu1 (see GCBO)
  131. % eventdata  reserved - to be defined in a future version of MATLAB
  132. % handles    structure with handles and user data (see GUIDATA)

  133. % Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
  134. %        contents{get(hObject,'Value')} returns selected item from popupmenu1
  135. com = get(hObject,'string');
  136. handles.com = com{get(hObject,'Value')};
  137. guidata(hObject, handles);

  138. % --- Executes during object creation, after setting all properties.
  139. function popupmenu1_CreateFcn(hObject, eventdata, handles)
  140. % hObject    handle to popupmenu1 (see GCBO)
  141. % eventdata  reserved - to be defined in a future version of MATLAB
  142. % handles    empty - handles not created until after all CreateFcns called

  143. % Hint: popupmenu controls usually have a white background on Windows.
  144. %       See ISPC and COMPUTER.
  145. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  146.     set(hObject,'BackgroundColor','white');
  147. end


  148. % --- Executes on selection change in popupmenu2.
  149. function popupmenu2_Callback(hObject, eventdata, handles)
  150. % hObject    handle to popupmenu2 (see GCBO)
  151. % eventdata  reserved - to be defined in a future version of MATLAB
  152. % handles    structure with handles and user data (see GUIDATA)

  153. % Hints: contents = get(hObject,'String') returns popupmenu2 contents as cell array
  154. %        contents{get(hObject,'Value')} returns selected item from popupmenu2
  155. baud_rate = get(hObject,'string');
  156. handles.baud_rate = str2num(baud_rate{get(hObject,'Value')});
  157. guidata(hObject, handles);

  158. % --- Executes during object creation, after setting all properties.
  159. function popupmenu2_CreateFcn(hObject, eventdata, handles)
  160. % hObject    handle to popupmenu2 (see GCBO)
  161. % eventdata  reserved - to be defined in a future version of MATLAB
  162. % handles    empty - handles not created until after all CreateFcns called

  163. % Hint: popupmenu controls usually have a white background on Windows.
  164. %       See ISPC and COMPUTER.
  165. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  166.     set(hObject,'BackgroundColor','white');
  167. end


  168. % --- Executes on selection change in popupmenu3.
  169. function popupmenu3_Callback(hObject, eventdata, handles)
  170. % hObject    handle to popupmenu3 (see GCBO)
  171. % eventdata  reserved - to be defined in a future version of MATLAB
  172. % handles    structure with handles and user data (see GUIDATA)

  173. % Hints: contents = get(hObject,'String') returns popupmenu3 contents as cell array
  174. %        contents{get(hObject,'Value')} returns selected item from popupmenu3
  175. verifiedBits = get(hObject,'string');
  176. handles.verifiedBits = verifiedBits{get(hObject,'Value')};
  177. guidata(hObject, handles);

  178. % --- Executes during object creation, after setting all properties.
  179. function popupmenu3_CreateFcn(hObject, eventdata, handles)
  180. % hObject    handle to popupmenu3 (see GCBO)
  181. % eventdata  reserved - to be defined in a future version of MATLAB
  182. % handles    empty - handles not created until after all CreateFcns called

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


  188. % --- Executes on selection change in popupmenu4.
  189. function popupmenu4_Callback(hObject, eventdata, handles)
  190. % hObject    handle to popupmenu4 (see GCBO)
  191. % eventdata  reserved - to be defined in a future version of MATLAB
  192. % handles    structure with handles and user data (see GUIDATA)

  193. % Hints: contents = get(hObject,'String') returns popupmenu4 contents as cell array
  194. %        contents{get(hObject,'Value')} returns selected item from popupmenu4
  195. dataBits = get(hObject,'string');
  196. handles.dataBits = str2num(dataBits{get(hObject,'Value')});
  197. guidata(hObject, handles);

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

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


  208. % --- Executes on selection change in popupmenu5.
  209. function popupmenu5_Callback(hObject, eventdata, handles)
  210. % hObject    handle to popupmenu5 (see GCBO)
  211. % eventdata  reserved - to be defined in a future version of MATLAB
  212. % handles    structure with handles and user data (see GUIDATA)

  213. % Hints: contents = get(hObject,'String') returns popupmenu5 contents as cell array
  214. %        contents{get(hObject,'Value')} returns selected item from popupmenu5
  215. stopBit = get(hObject,'string');
  216. handles.stopBit = str2num(stopBit{get(hObject,'Value')});
  217. guidata(hObject, handles);

  218. % --- Executes during object creation, after setting all properties.
  219. function popupmenu5_CreateFcn(hObject, eventdata, handles)
  220. % hObject    handle to popupmenu5 (see GCBO)
  221. % eventdata  reserved - to be defined in a future version of MATLAB
  222. % handles    empty - handles not created until after all CreateFcns called

  223. % Hint: popupmenu controls usually have a white background on Windows.
  224. %       See ISPC and COMPUTER.
  225. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  226.     set(hObject,'BackgroundColor','white');
  227. end


  228. % --- Executes on button press in pushbutton1.
  229. function pushbutton1_Callback(hObject, eventdata, handles)
  230. % hObject    handle to pushbutton1 (see GCBO)
  231. % eventdata  reserved - to be defined in a future version of MATLAB
  232. % handles    structure with handles and user data (see GUIDATA)

  233. %打開串口按鈕的回調程序
  234. %首先檢驗是打開串口操作還是關閉操作

  235. %打開操作
  236. if(strcmp(get(hObject,'string') , 'Open Serial Port'))
  237.     %建立一個serial object,并設置其參數(接收的終止符號為\n)
  238.      try
  239.         handles.serial =  serial(handles.com,'BaudRate',handles.baud_rate,'DataBits',handles.dataBits,...
  240.                                 'Parity',handles.verifiedBits,'StopBits',handles.stopBit,'FlowControl',...
  241.                                 handles.flowControl,'terminator',char(13));
  242.         set(handles.serial,'OutputBufferSize',512000);
  243.         set(handles.serial,'InputBufferSize',512000);
  244.         handles.serial.BytesAvailableFcnMode = 'terminator';
  245.         %當接受到數據后如果碰到結束符\n就讀入,調用receiveData函數
  246.         handles.serial.BytesAvailableFcn={'receiveData',handles};
  247.         guidata(hObject,handles);
  248.         %打開串口

  249.         fopen(handles.serial);
  250.         set(hObject,'String','Close Serial Port','foreGroundColor',[67/255,64/255,119/255]);
  251.         str = ['Serial Port ', handles.com , ' opened sucessfully!'];
  252.         set(handles.text3,'String',str,'fontweight','bold');
  253.         set(handles.pushbutton4,'enable','on');
  254.         set(handles.pushbutton5,'enable','on');
  255.         set(handles.pushbutton6,'enable','on');
  256.      catch
  257.         %進行出錯處理
  258.         errmsg = lasterr;
  259.         errordlg([errmsg,char(10),'   Can''t Open Serial Port : ',handles.com, char(10),'   Please choose another Port!'],'Serial Port Open Error');
  260.         rethrow(lasterror);
  261.     end
  262. %關閉串口操作
  263. else
  264.    
  265.     fclose(handles.serial);
  266.     set(hObject,'String','Open Serial Port','foreGroundColor',[1,0,0.5]);
  267.     str = ['Serial Port ', handles.com , ' closed sucessfully!'];
  268.     set(handles.text3,'String',str,'fontweight','bold');
  269.     set(handles.pushbutton4,'enable','off');
  270.     set(handles.pushbutton5,'enable','off');
  271.     set(handles.pushbutton6,'enable','off');
  272. end
  273.    
  274. % --- Executes on selection change in popupmenu6.
  275. function popupmenu6_Callback(hObject, eventdata, handles)
  276. % hObject    handle to popupmenu6 (see GCBO)
  277. % eventdata  reserved - to be defined in a future version of MATLAB
  278. % handles    structure with handles and user data (see GUIDATA)

  279. % Hints: contents = get(hObject,'String') returns popupmenu6 contents as cell array
  280. %        contents{get(hObject,'Value')} returns selected item from popupmenu6

  281. flowControl = get(hObject,'string');
  282. handles.flowControl = flowControl{get(hObject,'Value')};
  283. guidata(hObject, handles);

  284. % --- Executes during object creation, after setting all properties.
  285. function popupmenu6_CreateFcn(hObject, eventdata, handles)
  286. % hObject    handle to popupmenu6 (see GCBO)
  287. % eventdata  reserved - to be defined in a future version of MATLAB
  288. % handles    empty - handles not created until after all CreateFcns called

  289. % Hint: popupmenu controls usually have a white background on Windows.
  290. %       See ISPC and COMPUTER.
  291. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  292.     set(hObject,'BackgroundColor','white');
  293. end




  294. % --- Executes on button press in pushbutton4.
  295. function pushbutton4_Callback(hObject, eventdata, handles)
  296. % hObject    handle to pushbutton4 (see GCBO)
  297. % eventdata  reserved - to be defined in a future version of MATLAB
  298. % handles    structure with handles and user data (see GUIDATA)

  299. %發送數據按鈕回調程序
  300. str = get(handles.edit1,'string');
  301. %檢測端口狀態
  302. if(strcmp(handles.serial.status,'closed'))
  303.     str1 = ['Serial Port ', handles.com , ' is closed ,Please open it!'];
  304.     set(handles.text3,'String',str1,'fontweight','bold');
  305. else
  306.     %打開時進行串口寫出
  307.     if(handles.displayT == 0)
  308.         fprintf(handles.serial,'%s',str);
  309.         str2 = ['Data has sent in ASCI format sucessfully!'];
  310.         set(handles.text3,'String',str2,'fontweight','bold');
  311.     else
  312.         fprintf(handles.serial,'%x',str);
  313.         str3 = ['Data has sent in HEX format sucessfully!'];
  314.         set(handles.text3,'String',str3,'fontweight','bold');
  315.     end
  316. end




  317. % --- Executes on button press in checkbox1.
  318. function checkbox1_Callback(hObject, eventdata, handles)
  319. % hObject    handle to checkbox1 (see GCBO)
  320. % eventdata  reserved - to be defined in a future version of MATLAB
  321. % handles    structure with handles and user data (see GUIDATA)

  322. % Hint: get(hObject,'Value') returns toggle state of checkbox1
  323. handles.displayT = get(hObject,'Value');
  324. guidata(hObject,handles);

  325. % --- Executes on button press in checkbox2.
  326. function checkbox2_Callback(hObject, eventdata, handles)
  327. % hObject    handle to checkbox2 (see GCBO)
  328. % eventdata  reserved - to be defined in a future version of MATLAB
  329. % handles    structure with handles and user data (see GUIDATA)

  330. % Hint: get(hObject,'Value') returns toggle state of checkbox2
  331. handles.displayR = get(hObject,'Value');
  332. guidata(hObject,handles);




  333. % --- Executes when user attempts to close figure1.
  334. function figure1_CloseRequestFcn(hObject, eventdata, handles)
  335. % hObject    handle to figure1 (see GCBO)
  336. % eventdata  reserved - to be defined in a future version of MATLAB
  337. % handles    structure with handles and user data (see GUIDATA)

  338. % Hint: delete(hObject) closes the figure

  339. %推出界面時 檢查是否關閉串口
  340. button = questdlg('Do you really want to quit? ','Quit?','Yes','No','No');
  341. if(strcmp(button, 'Yes'))
  342.         if(strcmp(get(handles.pushbutton1,'string'),'Close Serial Port'))
  343.             fclose(handles.serial);
  344.         end
  345.     delete(hObject);
  346. else
  347.     return;
  348. end




  349. % --- Executes on button press in pushbutton5.
  350. function pushbutton5_Callback(hObject, eventdata, handles)
  351. ……………………

  352. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:
matlab gui串口通信程序.zip (9.64 KB, 下載次數: 24)
回復

使用道具 舉報

ID:221248 發表于 2017-7-21 08:57 | 顯示全部樓層
學習了
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 久久精品国产亚洲一区二区三区 | 毛片在线看看 | 久久久久国产精品免费免费搜索 | 国产免费一区二区 | 久草在线| 午夜精品一区二区三区免费视频 | 亚洲天堂一区 | 色888www视频在线观看 | 99国产精品视频免费观看一公开 | 久久久新视频 | 欧美色综合一区二区三区 | 国产精品久久久久久久久动漫 | 99久久婷婷国产综合精品首页 | 欧美专区在线视频 | 国产美女久久 | 精品国产一区二区在线 | 中日av| 一区二区三区高清 | 日韩一区欧美一区 | 四虎在线视频 | 日韩在线一区二区三区 | 一区二区免费在线观看 | 在线视频国产一区 | 国产精品视频综合 | 欧美中文字幕一区二区 | 日韩av一区二区在线 | 中文字幕av亚洲精品一部二部 | 欧美国产日韩一区二区三区 | 国产精品久久久久久av公交车 | 不卡的av在线 | 色狠狠桃花综合 | 国产a视频| 91精品国产91久久久久久吃药 | 激情欧美一区二区三区中文字幕 | 成人一级黄色毛片 | 一区二区三区视频免费看 | 午夜视频免费在线观看 | 欧美精品二区 | 国产视频久久 | 亚洲免费在线播放 | 精品久久久久久久久久久久久久 |