找回密码
 立即注册

QQ登录

只需一步,快速开始

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

matlab中GUI的串口调试程序(发送与接收,很全面)

  [复制链接]
跳转到指定楼层
楼主
基于matlab中的GUI串口调试程序(发送与接收,很全面),可以调试检测串口,参数设置灵活,在GUI面板上操作简易。



源程序下载:
matlab中GUI的串口调试程序.zip (13.33 KB, 下载次数: 720)




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

  23. % Last Modified by GUIDE v2.5 07-Sep-2013 13:47:05

  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', @untitled_OpeningFcn, ...
  29.                    'gui_OutputFcn',  @untitled_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 untitled is made visible.
  42. function untitled_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 untitled (see VARARGIN)

  48. % 初始化
  49. set(handles.caiji,'UserData',0);
  50. setappdata(handles.save,'ApplicationData',0);

  51. % Choose default command line output for untitled
  52. handles.output = hObject;

  53. % Update handles structure
  54. guidata(hObject, handles);

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


  57. % --- Outputs from this function are returned to the command line.
  58. function varargout = untitled_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. % --- Executes on button press in on.
  66. function on_Callback(hObject, eventdata, handles)
  67. % hObject    handle to on (see GCBO)
  68. % eventdata  reserved - to be defined in a future version of MATLAB
  69. % handles    structure with handles and user data (see GUIDATA)

  70. clc;

  71. s1 = instrfind('Type','serial','Port','COM1','Tag','');    % 查看端口是否存在
  72. s2 = instrfind('Type','serial','Port','COM2','Tag','');
  73. s3 = instrfind('Type','serial','Port','COM3','Tag','');    % 查看端口是否存在
  74. s4 = instrfind('Type','serial','Port','COM4','Tag','');
  75. % 获取参数值


  76. if (isempty(s1) && get(handles.port,'Value')==1 )           % 如果端口1存在,存创建端口
  77.     s = serial('COM1');                   % 创建串口号
  78.     if(get(handles.botelv,'Value')==1)    % 设置串口参数
  79.         s.BaudRate = 1200;                  
  80.     elseif(get(handles.botelv,'Value')==2)
  81.         s.BaudRate = 2400;   
  82.     elseif(get(handles.botelv,'Value')==3)
  83.         s.BaudRate = 4800;   
  84.     elseif(get(handles.botelv,'Value')==4)
  85.         s.BaudRate = 9600;  
  86.     elseif(get(handles.botelv,'Value')==5)
  87.         s.BaudRate = 19200;
  88.     elseif(get(handles.botelv,'Value')==6)
  89.         s.BaudRate = 38400;
  90.     elseif(get(handles.botelv,'Value')==7)
  91.         s.BaudRate = 76800;
  92.     elseif(get(handles.botelv,'Value')==8)
  93.         s.BaudRate = 115200;  
  94.     end
  95.     if(get(handles.jiaoyanw,'Value')==1)
  96.         s.Parity = 'none';                      % 无校验位
  97.     elseif(get(handles.jiaoyanw,'Value')==2)
  98.         s.Parity = 'odd';                      % 偶校验
  99.     elseif(get(handles.jiaoyanw,'Value')==3)
  100.         s.Parity = 'even';                      % 奇校验
  101.     end
  102.     if(get(handles.dataw,'Value')==1)           % 数据位
  103.         s.DataBits = 8;
  104.     elseif(get(handles.dataw,'Value')==2)
  105.         s.DataBits = 7;
  106.     elseif(get(handles.dataw,'Value')==3)
  107.         s.DataBits = 6;
  108.     elseif(get(handles.dataw,'Value')==4)
  109.         s.DataBits = 5;
  110.     end
  111.     if(get(handles.stopw,'Value')==1)           % 停止位
  112.         s.StopBits = 1;
  113.     elseif(get(handles.stopw,'Value')==2)
  114.         s.StopBits = 1.5;
  115.     elseif(get(handles.stopw,'Value')==3)
  116.         s.StopBits = 2;  
  117.     end
  118.     s.Timeout = 0.1;
  119.     s.InputBufferSize = 3072;
  120.     s.OutputBufferSize = 3072;
  121.     s.ReadAsyncMode = 'continuous';
  122.    
  123.     handles.sbuff = s;
  124.     guidata(hObject, handles);
  125.     s.BytesAvailableFcnMode = 'byte';
  126.     s.BytesAvailableFcnCount = 10;
  127.     s.BytesAvailableFcn = {@recive_data, handles};
  128.     fopen(s);
  129.     set(handles.caiji,'Enable','On');
  130.     set(handles.off,'Enable','On');
  131.     set(handles.on,'Enable','Off');
  132.     set(handles.edit1,'String','打开成功!');
  133.     set(handles.exit,'Enable','Off');
  134.     set(handles.edit1,'backgroundcolor',[0 1 0]);
  135.    
  136. elseif (isempty(s2) && get(handles.port,'Value')==2 )        % 如果端口2存在,存创建端口
  137.     s = serial('COM2');                   % 创建串口号
  138.     if(get(handles.botelv,'Value')==1)    % 设置串口参数
  139.         s.BaudRate = 1200;                  
  140.     elseif(get(handles.botelv,'Value')==2)
  141.         s.BaudRate = 2400;   
  142.     elseif(get(handles.botelv,'Value')==3)
  143.         s.BaudRate = 4800;   
  144.     elseif(get(handles.botelv,'Value')==4)
  145.         s.BaudRate = 9600;  
  146.     elseif(get(handles.botelv,'Value')==5)
  147.         s.BaudRate = 19200;
  148.     elseif(get(handles.botelv,'Value')==6)
  149.         s.BaudRate = 38400;
  150.     elseif(get(handles.botelv,'Value')==7)
  151.         s.BaudRate = 76800;
  152.     elseif(get(handles.botelv,'Value')==8)
  153.         s.BaudRate = 115200;  
  154.     end
  155.     if(get(handles.jiaoyanw,'Value')==1)
  156.         s.Parity = 'none';                      % 无校验位
  157.     elseif(get(handles.jiaoyanw,'Value')==2)
  158.         s.Parity = 'odd';                      % 偶校验
  159.     elseif(get(handles.jiaoyanw,'Value')==3)
  160.         s.Parity = 'even';                      % 奇校验
  161.     end
  162.     if(get(handles.dataw,'Value')==1)           % 数据位
  163.         s.DataBits = 8;
  164.     elseif(get(handles.dataw,'Value')==2)
  165.         s.DataBits = 7;
  166.     elseif(get(handles.dataw,'Value')==3)
  167.         s.DataBits = 6;
  168.     elseif(get(handles.dataw,'Value')==4)
  169.         s.DataBits = 5;
  170.     end
  171.     if(get(handles.stopw,'Value')==1)           % 停止位
  172.         s.StopBits = 1;
  173.     elseif(get(handles.stopw,'Value')==2)
  174.         s.StopBits = 1.5;
  175.     elseif(get(handles.stopw,'Value')==3)
  176.         s.StopBits = 2;  
  177.     end
  178.     s.Timeout = 0.1;
  179.     s.InputBufferSize = 3072;
  180.     s.OutputBufferSize = 3072;
  181.     s.ReadAsyncMode = 'continuous';
  182.    
  183.     handles.sbuff = s;
  184.     guidata(hObject, handles);
  185.     s.BytesAvailableFcnMode = 'byte';
  186.     s.BytesAvailableFcnCount = 10;
  187.     s.BytesAvailableFcn = {@recive_data, handles};
  188.     fopen(s);
  189.     set(handles.caiji,'Enable','On');
  190.     set(handles.off,'Enable','On');
  191.     set(handles.on,'Enable','Off');
  192.     set(handles.exit,'Enable','Off');
  193.     set(handles.edit1,'String','打开成功!');
  194.     set(handles.edit1,'backgroundcolor',[0 1 0]);
  195.    
  196. elseif (isempty(s3) && get(handles.port,'Value')==3 )        % 如果端口3存在,存创建端口
  197.     s = serial('COM3');                   % 创建串口号
  198.     if(get(handles.botelv,'Value')==1)    % 设置串口参数
  199.         s.BaudRate = 1200;                  
  200.     elseif(get(handles.botelv,'Value')==2)
  201.         s.BaudRate = 2400;   
  202.     elseif(get(handles.botelv,'Value')==3)
  203.         s.BaudRate = 4800;   
  204.     elseif(get(handles.botelv,'Value')==4)
  205.         s.BaudRate = 9600;  
  206.     elseif(get(handles.botelv,'Value')==5)
  207.         s.BaudRate = 19200;
  208.     elseif(get(handles.botelv,'Value')==6)
  209.         s.BaudRate = 38400;
  210.     elseif(get(handles.botelv,'Value')==7)
  211.         s.BaudRate = 76800;
  212.     elseif(get(handles.botelv,'Value')==8)
  213.         s.BaudRate = 115200;  
  214.     end
  215.     if(get(handles.jiaoyanw,'Value')==1)
  216.         s.Parity = 'none';                      % 无校验位
  217.     elseif(get(handles.jiaoyanw,'Value')==2)
  218.         s.Parity = 'odd';                      % 偶校验
  219.     elseif(get(handles.jiaoyanw,'Value')==3)
  220.         s.Parity = 'even';                      % 奇校验
  221.     end
  222.     if(get(handles.dataw,'Value')==1)           % 数据位
  223.         s.DataBits = 8;
  224.     elseif(get(handles.dataw,'Value')==2)
  225.         s.DataBits = 7;
  226.     elseif(get(handles.dataw,'Value')==3)
  227.         s.DataBits = 6;
  228.     elseif(get(handles.dataw,'Value')==4)
  229.         s.DataBits = 5;
  230.     end
  231.     if(get(handles.stopw,'Value')==1)           % 停止位
  232.         s.StopBits = 1;
  233.     elseif(get(handles.stopw,'Value')==2)
  234.         s.StopBits = 1.5;
  235.     elseif(get(handles.stopw,'Value')==3)
  236.         s.StopBits = 2;  
  237.     end
  238.     s.Timeout = 0.1;
  239.     s.InputBufferSize = 3072;
  240.     s.OutputBufferSize = 3072;
  241.     s.ReadAsyncMode = 'continuous';
  242.    
  243.     handles.sbuff = s;
  244.     guidata(hObject, handles);
  245.     s.BytesAvailableFcnMode = 'byte';
  246.     s.BytesAvailableFcnCount = 10;
  247.     s.BytesAvailableFcn = {@recive_data, handles};
  248.     fopen(s);
  249.     set(handles.caiji,'Enable','On');
  250.     set(handles.off,'Enable','On');
  251.     set(handles.on,'Enable','Off');
  252.     set(handles.exit,'Enable','Off');
  253.     set(handles.edit1,'String','打开成功!');
  254.     set(handles.edit1,'backgroundcolor',[0 1 0]);
  255.    
  256. elseif (isempty(s4) && get(handles.port,'Value')==4 )        % 如果端口4存在,存创建端口
  257.     s = serial('COM4');                   % 创建串口号
  258.     if(get(handles.botelv,'Value')==1)    % 设置串口参数
  259.         s.BaudRate = 1200;                  
  260.     elseif(get(handles.botelv,'Value')==2)
  261.         s.BaudRate = 2400;   
  262.     elseif(get(handles.botelv,'Value')==3)
  263.         s.BaudRate = 4800;   
  264.     elseif(get(handles.botelv,'Value')==4)
  265.         s.BaudRate = 9600;  
  266.     elseif(get(handles.botelv,'Value')==5)
  267.         s.BaudRate = 19200;
  268.     elseif(get(handles.botelv,'Value')==6)
  269.         s.BaudRate = 38400;
  270.     elseif(get(handles.botelv,'Value')==7)
  271.         s.BaudRate = 76800;
  272.     elseif(get(handles.botelv,'Value')==8)
  273.         s.BaudRate = 115200;  
  274.     end
  275.     if(get(handles.jiaoyanw,'Value')==1)
  276.         s.Parity = 'none';                      % 无校验位
  277.     elseif(get(handles.jiaoyanw,'Value')==2)
  278.         s.Parity = 'odd';                      % 偶校验
  279.     elseif(get(handles.jiaoyanw,'Value')==3)
  280.         s.Parity = 'even';                      % 奇校验
  281.     end
  282.     if(get(handles.dataw,'Value')==1)           % 数据位
  283.         s.DataBits = 8;
  284.     elseif(get(handles.dataw,'Value')==2)
  285.         s.DataBits = 7;
  286.     elseif(get(handles.dataw,'Value')==3)
  287.         s.DataBits = 6;
  288.     elseif(get(handles.dataw,'Value')==4)
  289.         s.DataBits = 5;
  290.     end
  291.     if(get(handles.stopw,'Value')==1)           % 停止位
  292.         s.StopBits = 1;
  293.     elseif(get(handles.stopw,'Value')==2)
  294.         s.StopBits = 1.5;
  295.     elseif(get(handles.stopw,'Value')==3)
  296.         s.StopBits = 2;  
  297.     end
  298.     s.Timeout = 0.1;
  299.     s.InputBufferSize = 3072;
  300.     s.OutputBufferSize = 3072;
  301.     s.ReadAsyncMode = 'continuous';
  302.    
  303.     handles.sbuff = s;
  304.     guidata(hObject, handles);
  305.     s.BytesAvailableFcnMode = 'byte';
  306.     s.BytesAvailableFcnCount = 10;
  307.     s.BytesAvailableFcn = {@recive_data, handles};
  308.     fopen(s);
  309.     set(handles.caiji,'Enable','On');
  310.     set(handles.off,'Enable','On');
  311.     set(handles.on,'Enable','Off');
  312.     set(handles.exit,'Enable','Off');
  313.     set(handles.edit1,'String','打开成功!');
  314.     set(handles.edit1,'backgroundcolor',[0 1 0]);
  315. else
  316.     msgbox('串口不存在或被占用!');
  317.     set(handles.edit1,'String','打开失败!');
  318.     set(handles.edit1,'backgroundcolor',[1 0/255 0]);
  319. end

  320.    
  321. % 中断处理程序
  322. function recive_data(t,  event, handles)

  323. if(getappdata(handles.caiji,'ApplicationData')~=0)            % 收到单击采集信号的信息
  324.    
  325.     g = handles.sbuff;
  326.     [out,huanchongcout] = fread(g,10);                       % 读取数据
  327. %     plot(handles.axes1,out);  
  328.     % 解析数据
  329.     if(out(1)==128 && bitxor(bitxor(bitxor(bitxor(out(2),out(3)),out(4)),out(5)),out(6)) == out(7))
  330.         data2 = dec2bin(out(2),8);
  331.         data3 = dec2bin(out(3),8);
  332.         data4 = dec2bin(out(4),8);
  333.         data5 = dec2bin(out(5),8);
  334.         data6 = dec2bin(out(6),8);
  335.         cur_data0 = 0;      % 初始化脉冲值
  336.         cur_data1 = dec2bin(cur_data0,32);   % 转成二进制,有效位数为32
  337.         cur_data1(1:4) = data6(5:8);
  338.         cur_data1(5:11) = data5(2:8);
  339.         cur_data1(12:18) = data4(2:8);
  340.         cur_data1(19:25) = data3(2:8);
  341.         cur_data1(26:32) = data2(2:8);
  342.         cur_data2 = cur_data1;
  343.         set(handles.current_data,'String',bin2dec(cur_data2));
  344.     end
  345.    
  346.    
  347.     if(getappdata(handles.save,'ApplicationData') ~=0)        % 开始存储数据
  348.         
  349.         File = get(handles.save_play,'String');
  350.         fid = fopen(File,'a');
  351.         fprintf(fid,'%d\t',bin2dec(cur_data2));
  352.         fprintf(fid,'\n');
  353.         fclose(fid);
  354.         
  355.     end      
  356. end  


  357. % --- Executes on button press in off.
  358. function off_Callback(hObject, eventdata, handles)
  359. % hObject    handle to off (see GCBO)
  360. % eventdata  reserved - to be defined in a future version of MATLAB
  361. % handles    structure with handles and user data (see GUIDATA)

  362.     s = handles.sbuff;      % 传递
  363.     fclose(s);              % 关闭串口设备对象
  364.     delete(s);              % 删除内存中的串口设备对象
  365.     clear s;                % 清除工作区间的串口设备对象
  366.     setappdata(handles.axes1,'ApplicationData',0);  % 清空缓存数据
  367.     set(handles.axes1,'UserData',0);                % 将计数器清0
  368.     set(handles.save,'UserData',0);                 % 将数据保存区清0
  369.     set(handles.edit1,'String','串口关闭');
  370.     set(handles.edit1,'backgroundcolor',[0 0/255 1]);
  371.     set(handles.caiji,'Enable','Off');
  372.     set(handles.on,'Enable','On');
  373.     set(handles.save,'Enable','Off');
  374.     set(handles.off,'Enable','Off');
  375.     set(handles.exit,'Enable','On');
  376. % --- Executes on selection change in port.
  377. function port_Callback(hObject, eventdata, handles)
  378. % hObject    handle to port (see GCBO)
  379. % eventdata  reserved - to be defined in a future version of MATLAB
  380. % handles    structure with handles and user data (see GUIDATA)

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


  383. % --- Executes during object creation, after setting all properties.
  384. function port_CreateFcn(hObject, eventdata, handles)
  385. % hObject    handle to port (see GCBO)
  386. % eventdata  reserved - to be defined in a future version of MATLAB
  387. % handles    empty - handles not created until after all CreateFcns called

  388. % Hint: popupmenu controls usually have a white background on Windows.
  389. %       See ISPC and COMPUTER.
  390. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  391.     set(hObject,'BackgroundColor','white');
  392. end


  393. % --- Executes on selection change in botelv.
  394. function botelv_Callback(hObject, eventdata, handles)
  395. % hObject    handle to botelv (see GCBO)
  396. % eventdata  reserved - to be defined in a future version of MATLAB
  397. % handles    structure with handles and user data (see GUIDATA)

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


  400. % --- Executes during object creation, after setting all properties.
  401. function botelv_CreateFcn(hObject, eventdata, handles)
  402. % hObject    handle to botelv (see GCBO)
  403. % eventdata  reserved - to be defined in a future version of MATLAB
  404. % handles    empty - handles not created until after all CreateFcns called

  405. % Hint: popupmenu controls usually have a white background on Windows.
  406. %       See ISPC and COMPUTER.
  407. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  408.     set(hObject,'BackgroundColor','white');
  409. end


  410. % --- Executes on selection change in dataw.
  411. function dataw_Callback(hObject, eventdata, handles)
  412. % hObject    handle to dataw (see GCBO)
  413. % eventdata  reserved - to be defined in a future version of MATLAB
  414. % handles    structure with handles and user data (see GUIDATA)

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


  417. % --- Executes during object creation, after setting all properties.
  418. function dataw_CreateFcn(hObject, eventdata, handles)
  419. % hObject    handle to dataw (see GCBO)
  420. % eventdata  reserved - to be defined in a future version of MATLAB
  421. % handles    empty - handles not created until after all CreateFcns called

  422. % Hint: popupmenu controls usually have a white background on Windows.
  423. %       See ISPC and COMPUTER.
  424. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  425.     set(hObject,'BackgroundColor','white');
  426. end


  427. % --- Executes on selection change in jiaoyanw.
  428. function jiaoyanw_Callback(hObject, eventdata, handles)
  429. % hObject    handle to jiaoyanw (see GCBO)
  430. % eventdata  reserved - to be defined in a future version of MATLAB
  431. % handles    structure with handles and user data (see GUIDATA)

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


  434. % --- Executes during object creation, after setting all properties.
  435. function jiaoyanw_CreateFcn(hObject, eventdata, handles)
  436. % hObject    handle to jiaoyanw (see GCBO)
  437. % eventdata  reserved - to be defined in a future version of MATLAB
  438. % handles    empty - handles not created until after all CreateFcns called

  439. % Hint: popupmenu controls usually have a white background on Windows.
  440. %       See ISPC and COMPUTER.
  441. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  442.     set(hObject,'BackgroundColor','white');
  443. end


  444. % --- Executes on selection change in stopw.
  445. function stopw_Callback(hObject, eventdata, handles)
  446. % hObject    handle to stopw (see GCBO)
  447. % eventdata  reserved - to be defined in a future version of MATLAB
  448. % handles    structure with handles and user data (see GUIDATA)

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


  451. % --- Executes during object creation, after setting all properties.
  452. function stopw_CreateFcn(hObject, eventdata, handles)
  453. % hObject    handle to stopw (see GCBO)
  454. % eventdata  reserved - to be defined in a future version of MATLAB
  455. % handles    empty - handles not created until after all CreateFcns called

  456. % Hint: popupmenu controls usually have a white background on Windows.
  457. %       See ISPC and COMPUTER.
  458. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  459.     set(hObject,'BackgroundColor','white');
  460. end

  461. % --- Executes during object creation, after setting all properties.
  462. function axes1_CreateFcn(hObject, eventdata, handles)
  463. % hObject    handle to axes1 (see GCBO)
  464. % eventdata  reserved - to be defined in a future version of MATLAB
  465. % handles    empty - handles not created until after all CreateFcns called

  466. % Hint: place code in OpeningFcn to populate axes1

  467. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  468.     set(hObject,'BackgroundColor','white');
  469. end

  470. % --- Executes on button press in caiji.
  471. function caiji_Callback(hObject, eventdata, handles)
  472. % hObject    handle to caiji (see GCBO)
  473. % eventdata  reserved - to be defined in a future version of MATLAB
  474. % handles    structure with handles and user data (see GUIDATA)
  475. %  data = handles.data;

  476. % 发送信号,传递给中断,将信号暂存在采集控件中

  477. % t = str2num(get(handles.save_time,'String'));
  478. index = get(handles.save_lujin,'UserData');         % 是否设定存储路径
  479. g = handles.sbuff;
  480. [a,b] = fread(g);
  481. if ( b==0 )
  482.     msgbox('当前无数据输入!');
  483.     set(handles.save,'Enable','Off');
  484. else
  485.     if(get(handles.caiji,'UserData')==0)
  486.         set(handles.caiji,'String','停止采集');
  487.         set(handles.caiji,'BackgroundColor',[1 0 0]);
  488.         setappdata(handles.caiji,'ApplicationData',1);   % 用于传递给中断响应函数,可以接收并显示数据了
  489.         set(handles.caiji,'UserData',1);
  490.         if(  index~=0 )                                       % 设定存储条件
  491.             set(handles.save,'Enable','On');
  492.         else
  493.             set(handles.save,'Enable','Off');
  494.         end
  495.     else
  496.         set(handles.caiji,'String','开始采集');
  497.         setappdata(handles.caiji,'ApplicationData',0);
  498.         set(handles.caiji,'BackgroundColor',[0.8 0.8 0.8]);
  499.         set(handles.caiji,'UserData',0);
  500.         set(handles.save,'Enable','Off');
  501.         set(handles.save_time,'String',0);
  502.     end
  503.    
  504. end
  505. % --- Executes on button press in save.
  506. function save_Callback(hObject, eventdata, handles)
  507. % hObject    handle to save (see GCBO)
  508. % eventdata  reserved - to be defined in a future version of MATLAB
  509. % handles    structure with handles and user data (see GUIDATA)

  510. % 发送开始存储信号,并带有时间控制功能

  511.   setappdata(handles.save,'ApplicationData',1);
  512. % g = handles.sbuff;
  513. % [data,b] = fread(g)
  514.   t = str2num(get(handles.save_time,'String'));
  515.   acc_time = t;        % 中间变量     
  516.   set(handles.time_play,'String',num2str(t));
  517.   for i = 1:t         % 时间显示     
  518.       
  519.      pause(0.995);
  520.      set(handles.time_play,'String',num2str(acc_time-1));   
  521.      acc_time = acc_time-1;     
  522.       
  523.   end   
  524.   setappdata(handles.save,'ApplicationData',0);
  525. %   File = get(handles.save_play,'String');                     % 获取存储路径
  526. %   fopen(File);
  527. %   g = handles.sbuff;
  528. %   [data,b] = fread(g)
  529. %   fprintf(File,'%12.8e\t',data);
  530. %  save ( File ,'uint8','data');
  531. % --- Executes on button press in exit.
  532. function exit_Callback(hObject, eventdata, handles)
  533. % hObject    handle to exit (see GCBO)
  534. % eventdata  reserved - to be defined in a future version of MATLAB
  535. % handles    structure with handles and user data (see GUIDATA)

  536. close(gcbf);

  537. function save_play_Callback(hObject, eventdata, handles)
  538. % hObject    handle to save_play (see GCBO)
  539. % eventdata  reserved - to be defined in a future version of MATLAB
  540. % handles    structure with handles and user data (see GUIDATA)

  541. % Hints: get(hObject,'String') returns contents of save_play as text
  542. %        str2double(get(hObject,'String')) returns contents of save_play as a double


  543. % --- Executes during object creation, after setting all properties.
  544. function save_play_CreateFcn(hObject, eventdata, handles)
  545. % hObject    handle to save_play (see GCBO)
  546. % eventdata  reserved - to be defined in a future version of MATLAB
  547. % handles    empty - handles not created until after all CreateFcns called

  548. % Hint: edit controls usually have a white background on Windows.
  549. %       See ISPC and COMPUTER.
  550. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  551.     set(hObject,'BackgroundColor','white');
  552. end


  553. % --- Executes on button press in save_lujin.
  554. function save_lujin_Callback(hObject, eventdata, handles)
  555. % hObject    handle to save_lujin (see GCBO)
  556. % eventdata  reserved - to be defined in a future version of MATLAB
  557. % handles    structure with handles and user data (see GUIDATA)
  558. set(handles.save,'Enable','On');
  559. [file, pathname, index] = uiputfile({'*.txt';'*.dat';'*.mat'},'文件另存为');
  560. file1 = strcat(pathname,file);
  561. set(handles.save_play,'String',file1);
  562. set(handles.save_lujin,'UserData',index);      % 判断是否设定了保存文件的路径

  563. function save_time_Callback(hObject, eventdata, handles)
  564. % hObject    handle to save_time (see GCBO)
  565. % eventdata  reserved - to be defined in a future version of MATLAB
  566. % handles    structure with handles and user data (see GUIDATA)

  567. % Hints: get(hObject,'String') returns contents of save_time as text
  568. %        str2double(get(hObject,'String')) returns contents of save_time as a double



  569. % --- Executes during object creation, after setting all properties.
  570. function save_time_CreateFcn(hObject, eventdata, handles)
  571. % hObject    handle to save_time (see GCBO)
  572. % eventdata  reserved - to be defined in a future version of MATLAB
  573. % handles    empty - handles not created until after all CreateFcns called

  574. % Hint: edit controls usually have a white background on Windows.
  575. %       See ISPC and COMPUTER.
  576. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  577.     set(hObject,'BackgroundColor','white');
  578. end


  579. % --- Executes during object creation, after setting all properties.
  580. function time_play_CreateFcn(hObject, eventdata, handles)
  581. % hObject    handle to time_play (see GCBO)
  582. % eventdata  reserved - to be defined in a future version of MATLAB
  583. % handles    empty - handles not created until after all CreateFcns called



  584. function current_data_Callback(hObject, eventdata, handles)
  585. % hObject    handle to current_data (see GCBO)
  586. % eventdata  reserved - to be defined in a future version of MATLAB
  587. % handles    structure with handles and user data (see GUIDATA)

  588. % Hints: get(hObject,'String') returns contents of current_data as text
  589. %        str2double(get(hObject,'String')) returns contents of current_data as a double


  590. % --- Executes during object creation, after setting all properties.
  591. function current_data_CreateFcn(hObject, eventdata, handles)
  592. % hObject    handle to current_data (see GCBO)
  593. % eventdata  reserved - to be defined in a future version of MATLAB
  594. % handles    empty - handles not created until after all CreateFcns called

  595. % Hint: edit controls usually have a white background on Windows.
  596. %       See ISPC and COMPUTER.
  597. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  598.     set(hObject,'BackgroundColor','white');
  599. end
复制代码


评分

参与人数 1黑币 +5 收起 理由
wohuonini + 5

查看全部评分

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

使用道具 举报

沙发
ID:229874 发表于 2017-8-29 15:20 | 只看该作者
学习一下,正需要这个
回复

使用道具 举报

板凳
ID:234844 发表于 2017-10-12 21:37 | 只看该作者
你好我现在也是在做这个,但是设置的是每个一定的时间显示一个接收的数据,但是数据却不是按延时时间显示的,大神知道这是怎么回事吗
回复

使用道具 举报

地板
ID:254599 发表于 2017-11-28 11:54 | 只看该作者
i顶一下
回复

使用道具 举报

5#
ID:256701 发表于 2017-12-3 10:13 | 只看该作者
真是太棒了!对我帮助很大。谢谢楼主!
回复

使用道具 举报

6#
ID:256701 发表于 2017-12-3 10:14 | 只看该作者
真是太棒了!对我帮助很大,谢谢楼主!
回复

使用道具 举报

7#
ID:167718 发表于 2017-12-19 18:21 | 只看该作者
正在学习这个,多谢
回复

使用道具 举报

8#
ID:262276 发表于 2017-12-20 09:46 | 只看该作者
vivipp 发表于 2017-12-3 10:14
真是太棒了!对我帮助很大,谢谢楼主!

运行之后显示  无数据输入,这个该怎么改?
回复

使用道具 举报

9#
ID:277651 发表于 2018-1-19 10:39 | 只看该作者
正需要这个  赶紧看看
回复

使用道具 举报

10#
ID:240600 发表于 2018-3-15 10:33 | 只看该作者
来学习一下,不知道2017版本的可以直接调用不
回复

使用道具 举报

11#
ID:299685 发表于 2018-3-30 21:59 | 只看该作者
正需要这个,学习一下,谢谢
回复

使用道具 举报

12#
ID:305837 发表于 2018-4-11 14:25 | 只看该作者
学习一下,正需要这个
回复

使用道具 举报

13#
ID:265142 发表于 2018-4-12 14:40 | 只看该作者
正需要这个,看看
回复

使用道具 举报

14#
ID:336626 发表于 2018-5-23 16:46 | 只看该作者
非常赞,多谢楼主了
回复

使用道具 举报

15#
ID:398830 发表于 2018-9-17 02:29 | 只看该作者
太给力了,感谢楼主!
回复

使用道具 举报

16#
ID:399168 发表于 2018-9-17 18:54 | 只看该作者
想看看
回复

使用道具 举报

17#
ID:401307 发表于 2018-9-22 14:21 | 只看该作者
谢谢谢谢!!!
回复

使用道具 举报

18#
ID:437624 发表于 2018-12-2 13:38 | 只看该作者
不错,哎没钱币下载啊,谁能传一份
回复

使用道具 举报

19#
ID:143203 发表于 2019-3-20 08:48 | 只看该作者
怎么 运行???
回复

使用道具 举报

20#
ID:115379 发表于 2019-4-28 22:02 | 只看该作者
谢谢善于分享的人
回复

使用道具 举报

21#
ID:409871 发表于 2019-5-5 10:08 | 只看该作者
感谢分享
回复

使用道具 举报

22#
ID:536286 发表于 2019-5-12 17:31 | 只看该作者
学习一下
回复

使用道具 举报

23#
ID:537668 发表于 2019-5-14 09:36 | 只看该作者

真是太棒了!对我帮助很大,谢谢楼主!
回复

使用道具 举报

24#
ID:312058 发表于 2019-7-9 20:49 | 只看该作者
多谢楼主分享
回复

使用道具 举报

25#
ID:317193 发表于 2019-7-14 17:55 | 只看该作者
学习一下,正需要这个
回复

使用道具 举报

26#
ID:481460 发表于 2019-7-26 09:38 | 只看该作者
正在做这个,guide的串口调试程序,实时接收数据显示并绘图,学习下
回复

使用道具 举报

27#
ID:541439 发表于 2019-8-30 15:26 | 只看该作者
谢谢分享,学习中
回复

使用道具 举报

28#
ID:620618 发表于 2019-10-8 17:20 | 只看该作者
谢谢分享,学习中,,不知道是否可以实现CAN
回复

使用道具 举报

29#
ID:637330 发表于 2019-12-9 09:37 | 只看该作者
正在做matlab串口通信,现在matlab可以接收到串口发送的数据,但是串口接收不到matlab发送的数据,不知道是什么情况啊
回复

使用道具 举报

30#
ID:477218 发表于 2019-12-20 21:44 | 只看该作者
谢谢楼主,楼主好人一生平安。
回复

使用道具 举报

31#
ID:688169 发表于 2020-1-26 00:49 | 只看该作者
方常棒的教學,正需要這個!
回复

使用道具 举报

32#
ID:90713 发表于 2020-2-17 11:17 | 只看该作者
我的MATLAB版本是2018,这个程序运行报错,哪位有解决方法吗

1.png (20.01 KB, 下载次数: 113)

截图

截图
回复

使用道具 举报

33#
ID:404423 发表于 2020-4-13 17:46 | 只看该作者
vgboy8866 发表于 2020-2-17 11:17
我的MATLAB版本是2018,这个程序运行报错,哪位有解决方法吗

我也是这样请问解决了嘛
回复

使用道具 举报

34#
ID:514296 发表于 2020-6-2 16:19 | 只看该作者
虽然是旧帖,还是感谢一下分享。
回复

使用道具 举报

35#
ID:437554 发表于 2020-8-12 11:07 | 只看该作者
需要看看有没有用
回复

使用道具 举报

36#
ID:853439 发表于 2020-12-2 21:49 | 只看该作者
不错的程序,正好需要!
回复

使用道具 举报

37#
ID:487635 发表于 2021-5-9 21:26 | 只看该作者
有定位最后的图吗
回复

使用道具 举报

38#
ID:956957 发表于 2022-5-14 15:10 | 只看该作者
提示串口不存在或被调用,但是实际没有啊,怎么解决
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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