- function varargout = Serial_Port(varargin)
- % SERIAL_PORT M-file for Serial_Port.fig
- % SERIAL_PORT, by itself, creates a new SERIAL_PORT or raises the existing
- % singleton*.
- %
- % H = SERIAL_PORT returns the handle to a new SERIAL_PORT or the handle to
- % the existing singleton*.
- %
- % SERIAL_PORT('CALLBACK',hObject,eventData,handles,...) calls the local
- % function named CALLBACK in SERIAL_PORT.M with the given input arguments.
- %
- % SERIAL_PORT('Property','Value',...) creates a new SERIAL_PORT or raises the
- % existing singleton*. Starting from the left, property value pairs are
- % applied to the GUI before Serial_Port_OpeningFcn gets called. An
- % unrecognized property name or invalid value makes property application
- % stop. All inputs are passed to Serial_Port_OpeningFcn via varargin.
- %
- % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
- % instance to run (singleton)".
- %
- % See also: GUIDE, GUIDATA, GUIHANDLES
- % Edit the above text to modify the response to help Serial_Port
- % Last Modified by GUIDE v2.5 17-Jun-2015 10:13:45
- % Begin initialization code - DO NOT EDIT
- gui_Singleton = 1;
- gui_State = struct('gui_Name', mfilename, ...
- 'gui_Singleton', gui_Singleton, ...
- 'gui_OpeningFcn', @Serial_Port_OpeningFcn, ...
- 'gui_OutputFcn', @Serial_Port_OutputFcn, ...
- 'gui_LayoutFcn', [] , ...
- 'gui_Callback', []);
- if nargin && ischar(varargin{1})
- gui_State.gui_Callback = str2func(varargin{1});
- end
- if nargout
- [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
- else
- gui_mainfcn(gui_State, varargin{:});
- end
- % End initialization code - DO NOT EDIT
- % --- Executes just before Serial_Port is made visible.
- function Serial_Port_OpeningFcn(hObject, eventdata, handles, varargin)
- % This function has no output args, see OutputFcn.
- % hObject handle to figure
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- % varargin command line arguments to Serial_Port (see VARARGIN)
- % Choose default command line output for Serial_Port
- handles.output = hObject;
- % Update handles structure
- guidata(hObject, handles);
- %{
- 关闭、删除已打开的串口对象
- scoms=instrfind;
- fclose(scoms);
- delete(scoms);
- clear scoms;
- %}
- %获取可用串口
- global getcom
- getcom=instrhwinfo('serial');
- set(handles.popupmenu1,'string',char(getcom.AvailableSerialPorts));
- global a
- a=clock;
- % UIWAIT makes Serial_Port wait for user response (see UIRESUME)
- % uiwait(handles.figure1);
- % --- Outputs from this function are returned to the command line.
- function varargout = Serial_Port_OutputFcn(hObject, eventdata, handles)
- % varargout cell array for returning output args (see VARARGOUT);
- % hObject handle to figure
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- function edit1_Callback(hObject, eventdata, handles)
- % hObject handle to edit1 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- % Hints: get(hObject,'String') returns contents of edit1 as text
- % str2double(get(hObject,'String')) returns contents of edit1 as a double
- % --- Executes during object creation, after setting all properties.
- function edit1_CreateFcn(hObject, eventdata, handles)
- % hObject handle to edit1 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles empty - handles not created until after all CreateFcns called
- % Hint: edit controls usually have a white background on Windows.
- % See ISPC and COMPUTER.
- if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
- set(hObject,'BackgroundColor','white');
- end
- function edit2_Callback(hObject, eventdata, handles)
- % hObject handle to edit2 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- % Hints: get(hObject,'String') returns contents of edit2 as text
- % str2double(get(hObject,'String')) returns contents of edit2 as a double
- % --- Executes during object creation, after setting all properties.
- function edit2_CreateFcn(hObject, eventdata, handles)
- % hObject handle to edit2 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles empty - handles not created until after all CreateFcns called
- % Hint: edit controls usually have a white background on Windows.
- % See ISPC and COMPUTER.
- if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
- set(hObject,'BackgroundColor','white');
- end
- % --- Executes on button press in pushbutton1.
- function pushbutton1_Callback(hObject, eventdata, handles)
- % hObject handle to pushbutton1 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- %获取当前按钮的名字
- string=get(handles.pushbutton1,'string');
- if(strcmp(string,'打开串口')==1)
- set(handles.pushbutton1,'string','关闭串口');
- %禁止修改串口号和波特率
- set(handles.popupmenu1,'Enable','off');
- set(handles.popupmenu2,'Enable','off');
- %getcom=instrhwinfo('serial');
- global getcom
- global scom
- %选择串口号
- if size(getcom.AvailableSerialPorts)==0
- errordlg('无有效COM口','提示','replace');
- else if size(getcom.AvailableSerialPorts)==1
- scom=serial(char(getcom.AvailableSerialPorts));
- else
- for i=1:size(getcom.AvailableSerialPorts)
- if i==get(handles.popupmenu1,'Value')
- scom=serial(char(getcom.AvailableSerialPorts(i)));
- break;
- end;
- end;
- end;
- end;
- %设置串口波特率
- switch get(handles.popupmenu2,'Value')
- case 1
- scom.BaudRate=115200;
- case 2
- scom.BaudRate=57600;
- case 3
- scom.BaudRate=56000;
- case 4
- scom.BaudRate=43000;
- case 5
- scom.BaudRate=38400;
- case 6
- scom.BaudRate=19200;
- case 7
- scom.BaudRate=9600;
- case 8
- scom.BaudRate=4800;
- case 9
- scom.BaudRate=2400;
- case 10
- scom.BaudRate=1200;
- case 11
- scom.BaudRate=600;
- case 12
- scom.BaudRate=300;
- end;
- %scom.terminator='CR';
- scom.BytesAvailableFcnMode='terminator';
- scom.BytesAvailableFcn={@mycallback,handles};
- fopen(scom);
- else
- %按键的名字设为‘打开串口’
- set(handles.pushbutton1,'string','打开串口');
- %禁止修改串口号和波特率
- set(handles.popupmenu1,'Enable','on');
- set(handles.popupmenu2,'Enable','on');
- %查找串口对象
- scoms = instrfind;
- %尝试停止、关闭删除串口对象
- stopasync(scoms);
- fclose(scoms);
- delete(scoms);
- end;
- % --- Executes on button press in pushbutton2.
- function pushbutton2_Callback(hObject, eventdata, handles)
- % hObject handle to pushbutton2 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- global scom;
- %得到发送区的数据
- send_data=get(handles.edit2,'string');
- global a;
- a=GetSecs;
- fprintf(scom,send_data);
- % --- Executes on selection change in popupmenu1.
- function popupmenu1_Callback(hObject, eventdata, handles)
- % hObject handle to popupmenu1 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
- % contents{get(hObject,'Value')} returns selected item from popupmenu1
- % --- Executes during object creation, after setting all properties.
- function popupmenu1_CreateFcn(hObject, eventdata, handles)
- % hObject handle to popupmenu1 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles empty - handles not created until after all CreateFcns called
- % Hint: popupmenu controls usually have a white background on Windows.
- % See ISPC and COMPUTER.
- if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
- set(hObject,'BackgroundColor','white');
- end
- % --- Executes on selection change in popupmenu2.
- function popupmenu2_Callback(hObject, eventdata, handles)
- % hObject handle to popupmenu2 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
- % contents{get(hObject,'Value')} returns selected item from popupmenu2
- % --- Executes during object creation, after setting all properties.
- function popupmenu2_CreateFcn(hObject, eventdata, handles)
- % hObject handle to popupmenu2 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles empty - handles not created until after all CreateFcns called
- % Hint: popupmenu controls usually have a white background on Windows.
- % See ISPC and COMPUTER.
- if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
- set(hObject,'BackgroundColor','white');
- end
- %串口接收显示程序
- function mycallback(scom,BytesAvailable,handles)
- n_bytes=get(scom,'BytesAvailable');
- if n_bytes>0
- char=fscanf(scom);
- end;
- original=get(handles.edit1,'string');
- char=strcat(original,char);
- set(handles.edit1,'string',char);
- global a;
- set(handles.edit1,'string',num2char(GetSecs-a));
- function edit3_Callback(hObject, eventdata, handles)
- % hObject handle to edit3 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- % Hints: get(hObject,'String') returns contents of edit3 as text
- % str2double(get(hObject,'String')) returns contents of edit3 as a double
- % --- Executes during object creation, after setting all properties.
- function edit3_CreateFcn(hObject, eventdata, handles)
- % hObject handle to edit3 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles empty - handles not created until after all CreateFcns called
- % Hint: edit controls usually have a white background on Windows.
- % See ISPC and COMPUTER.
- if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
- set(hObject,'BackgroundColor','white');
- end
复制代码
|