|
|
发个MATLAB GUI串口程序把,用于US100型超声波传感器测距离的
function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_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 untitled
% Last Modified by GUIDE v2.5 23-Sep-2012 18:43:00
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_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 untitled is made visible.
function untitled_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 untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_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)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
% --- 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)
global s;%定义全局变量s,用于标识串口
global datas;%定义全局变量datas,用于存储串口发送来的数据
global out;%定义全局变量out,用于标识是否关闭串口,out=1不关闭串口,out=0关闭串口
global rate;%定义全局变量rate,用于表示串口通信的波特率
global COM;%定义全局变量COM,用于标识选取的COM口
out=1;
%通过Select_COM下拉条控件改变全局变量COM的值,根据COM的值选取打开的COM口
if(COM==1)
s=serial('COM1');
elseif(COM==2)
s=serial('COM2');
elseif(COM==3)
s=serial('COM3');
elseif(COM==4)
s=serial('COM4');
elseif(COM==5)
s=serial('COM5');
elseif(COM==6)
s=serial('COM6');
end
%通过Baude下拉条改变全局变量rate的值,根据rate的值选取串口所对应的波特率
if(rate==1)
set(s,'BaudRate',9600);
elseif(rate==2)
set(s,'BaudRate',19200);
end
fopen(s);%打开串口
s
d=get(handles.Value,'string')
d
y=str2num(d)
y
a=num2str(datestr(now,'HH:MM:SS'));%获得实时时间并转换为字符型存在a变量中
set(handles.Start_time,'String',a);%将字符型a显示在静态存储Start中
guidata(hObject, handles); %更新结构体
flag=fread(s,2*y,'uint8');%以二进制读取串口s中的数据并存储在datas中
for info=1:y
datas(info)=flag(2*info-1)*256+flag(2*info)
end
plot(datas);%绘制数组datas的图形
b=num2str(datestr(now,'HH:MM:SS'));%实时更新显示时间
set(handles.Time,'String',b);
guidata(hObject, handles); %更新结构体
function pushbutton2_Callback(hObject, eventdata, handles)
uiwait %等待
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
uiresume %继续显示
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clear
close all %关闭GUI界面窗口
clc
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global datas;%声明全局变量datas
dlmwrite('a.txt',datas,'\t');
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global out;%声明全局变量out
out=0;
% --- Executes on selection change in popupmenu6.
function popupmenu6_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu6 (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 popupmenu6 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu6
global COM;%声明全局变量COM
COM=1;%COM赋初始值为1
rate=1;%rate赋初始值为1
val=get(hObject,'value');%将下拉条value的值赋给val
%通过val值选择COM的值
switch val
case 1
COM=1;
case 2
COM=2;
case 3
COM=3;
case 4
COM=4;
case 5
COM=5;
case 6
COM=6;
end
% --- Executes during object creation, after setting all properties.
function popupmenu6_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu6 (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 popupmenu7.
function popupmenu7_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu7 (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 popupmenu7 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu7
global rate;%声明全局变量rate
val=get(hObject,'value');%将下拉条value值赋给val
%通过val的值选择rate 的值
switch val
case 1
rate=1;
case 2
rate=2;
end
% --- Executes during object creation, after setting all properties.
function popupmenu7_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu7 (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 Value_Callback(hObject, eventdata, handles)
% hObject handle to Value (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 Value as text
% str2double(get(hObject,'String')) returns contents of Value as a double
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','0')
end
% --- Executes during object creation, after setting all properties.
function Value_CreateFcn(hObject, eventdata, handles)
% hObject handle to Value (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
在2010B上测试过,见图片,第一次开启的时候,有数据漂移,图片中的框框可以填取多少数据。。。不过2010B有个BUG。。取一次数据,端口就识别不了。悲剧。汗。要用的孩纸还是装MATLAB 的插件把。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
评分
-
查看全部评分
|