function varargout = CruiseControl(varargin)
% CRUISECONTROL MATLAB code for CruiseControl.fig
% CRUISECONTROL, by itself, creates a new CRUISECONTROL or raises the existing
% singleton*.
%
% H = CRUISECONTROL returns the handle to a new CRUISECONTROL or the handle to
% the existing singleton*.
%
% CRUISECONTROL('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CRUISECONTROL.M with the given input arguments.
%
% CRUISECONTROL('Property','Value',...) creates a new CRUISECONTROL or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before CruiseControl_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to CruiseControl_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 CruiseControl
% Last Modified by GUIDE v2.5 10-Aug-2017 10:11:20
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @CruiseControl_OpeningFcn, ...
'gui_OutputFcn', @CruiseControl_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
% --- Executes just before CruiseControl is made visible.
function CruiseControl_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 CruiseControl (see VARARGIN)
% Choose default command line output for CruiseControl
handles.output = hObject;
%%初始化参数========================================================
handles.timer = timer('Period',1,'ExecutionMode','FixedRate', 'TimerFcn',...
{@timercallback,handles});%开创一个定时器
start(handles.timer);
Ahead_Picture = importdata('Ahead.jpg');
set(handles.Ahead,'CDATA',Ahead_Picture);
Back_Picture = importdata('Back.jpg');
set(handles.Back,'CDATA',Back_Picture);
Right_Picture = importdata('Right.jpg');
set(handles.Right,'CDATA',Right_Picture);
Left_Picture = importdata('Left.jpg');
set(handles.Left,'CDATA',Left_Picture);
Stop_Picture = importdata('Stop.jpg');
set(handles.Stop,'CDATA',Stop_Picture);
Indicator_Red = imread('indicator_red.jpg')%红色指示灯
axes(handles.indicator);
imshow(Indicator_Red);
vehicle = imread('vehicle_picture.jpg')%导入车辆
axes(handles.vehicle_picture);
imshow(vehicle);
wifi = imread('wifi1.jpg');
axes(handles.wifi);
imshow(wifi);
%=========================================================
%% 初始化参数
hasData = false; %表征网口是否接收到数据
isShow = false; %表征是否正在进行数据显示,即是否正在执行函数dataDisp
isStopDisp = false; %表征是否按下了【停止显示】按钮
isHexDisp = false; %表征是否勾选了【十六进制显示】
isHexSend = false; %表征是否勾选了【十六进制发送】
numRec = 0; %接收字符计数
numSend = 0; %发送字符计数
strRec = ''; %已接收的字符串
%% 将上述参数作为应用数据,存入窗口对象内
setappdata(hObject,'hasData',hasData);
setappdata(hObject,'strRec',strRec);
setappdata(hObject, 'numRec', numRec);
setappdata(hObject, 'numSend', numSend);
setappdata(hObject,'isShow',isShow);
setappdata(hObject,'isStopDisp',isStopDisp);
setappdata(hObject, 'isHexDisp', isHexDisp);
setappdata(hObject, 'isHexSend', isHexSend);
% Update handles structure
guidata(hObject, handles);
function timercallback(obj, event, handles)
time_num = fix(clock);
time_str1 = strcat(num2str(time_num(4)),':',num2str(time_num(5)+2),':',num2str(time_num(6)));
time_str2 = strcat(num2str(time_num(4)),':',num2str(time_num(5)+4),':',num2str(time_num(6)));
set(handles.clock1, 'String', datestr(now, 'HH:MM:SS'),'Fontsize',11);
set(handles.clock2,'String',time_str1,'Fontsize',11);
set(handles.clock3,'String',time_str2,'Fontsize',11);
% --- Outputs from this function are returned to the command line.
function varargout = CruiseControl_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;
function IP_Address_Callback(hObject, eventdata, handles)
% hObject handle to IP_Address (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 IP_Address as text
% str2double(get(hObject,'String')) returns contents of IP_Address as a double
% --- Executes during object creation, after setting all properties.
function IP_Address_CreateFcn(hObject, eventdata, handles)
% hObject handle to IP_Address (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 PORT_Callback(hObject, eventdata, handles)
% hObject handle to PORT (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 PORT as text
% str2double(get(hObject,'String')) returns contents of PORT as a double
% --- Executes during object creation, after setting all properties.
function PORT_CreateFcn(hObject, eventdata, handles)
% hObject handle to PORT (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 Connect_Button.
function Connect_Button_Callback(hObject, eventdata, handles)
%% 若按下【连接小车】按钮,打开网口
if get (hObject,'value')
set(handles.Status,'String','状态:已连接')
ip = get(handles.IP_Address,'String');
port = str2num(get(handles.PORT,'String'));
global NET ;
NET = tcpip(ip,port);
set(NET,'InputBufferSize',30000);
set(NET,'BytesAvailableFcnMode','byte')
set(NET,'BytesAvailableFcn',{@bytes,handles})
set(NET, 'TimerPeriod',0.05,'timerfcn',{@dataDisp,handles});
%将网口对象的句柄作为用户数据,存入窗口对象
set(handles.figure1,'UserData',NET);
try
fopen(NET);
catch
msgbox('连接失败!请重新连接','警告',10);
set(hObject,'value',0);%弹起本按钮
return;
end
%%打开网口后,允许网口发送数据,同时点亮串口状态指示灯
Indicator_Green = imread('indicator_green.jpg')
axes(handles.indicator);
imshow(Indicator_Green);
else
%%停止并删除定时器
t = timerfind;
if ~isempty(t)
stop(t);
delete(t);
end
%%停止连接对象
fclose(NET);
delete(NET);
评论1