www.gusucode.com > vnt 源码程序 matlab案例代码 > vnt/demoVNT_ManageCANMessageGUIFig.m

    function varargout = demoVNT_ManageCANMessageGUIFig(varargin)
% DEMOVNT_MANAGECANMESSAGEGUIFIG MATLAB code for demoVNT_ManageCANMessageGUIFig.fig
%      DEMOVNT_MANAGECANMESSAGEGUIFIG, by itself, creates a new DEMOVNT_MANAGECANMESSAGEGUIFIG or raises the existing
%      singleton*.
%
%      H = DEMOVNT_MANAGECANMESSAGEGUIFIG returns the handle to a new DEMOVNT_MANAGECANMESSAGEGUIFIG or the handle to
%      the existing singleton*.
%
%      DEMOVNT_MANAGECANMESSAGEGUIFIG('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DEMOVNT_MANAGECANMESSAGEGUIFIG.M with the given input arguments.
%
%      DEMOVNT_MANAGECANMESSAGEGUIFIG('Property','Value',...) creates a new DEMOVNT_MANAGECANMESSAGEGUIFIG or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before demoVNT_ManageCANMessageGUIFig_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to demoVNT_ManageCANMessageGUIFig_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 demoVNT_ManageCANMessageGUIFig

% Last Modified by GUIDE v2.5 25-Oct-2011 15:41:11

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @demoVNT_ManageCANMessageGUIFig_OpeningFcn, ...
                   'gui_OutputFcn',  @demoVNT_ManageCANMessageGUIFig_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 demoVNT_ManageCANMessageGUIFig is made visible.
function demoVNT_ManageCANMessageGUIFig_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 demoVNT_ManageCANMessageGUIFig (see VARARGIN)

% Choose default command line output for demoVNT_ManageCANMessageGUIFig
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes demoVNT_ManageCANMessageGUIFig wait for user response (see UIRESUME)
% uiwait(handles.MainFig);

% Open the CAN database file.
db = canDatabase('demoVNT_ManageCANMessageGUI.dbc');

% Create a CAN channel for sending and receiving messages.
canCh = canChannel('Vector', 'Virtual 1', 1);
% Attach the CAN database file to the channel for received message decoding.
canCh.Database = db;
% Start the channel.
start(canCh);
% Store the channel in the GUI so it persists for the life of the GUI.
setappdata(handles.MainFig, 'CANChannel', canCh);

% Create the two messages to send.
msgVehicleData = canMessage(db, 'VehicleData');
msgWheelSpeeds = canMessage(db, 'WheelSpeeds');
% Store the messages in the GUI so they persist for the life of the GUI.
setappdata(handles.MainFig, 'MsgVehicleData', msgVehicleData);
setappdata(handles.MainFig, 'MsgWheelSpeeds', msgWheelSpeeds);

% Create a timer which will drive the update of the GUI. It will call to a
% callback function in this file.
updateGUITimer = timer(...
    'Period', 0.500,...
    'ExecutionMode', 'fixedSpacing', ...
    'TimerFcn', {@updateGUITimerCallback, handles});
% Start the timer.
start(updateGUITimer);
% Store the timer in the GUI so it persists for the life of the GUI.
setappdata(handles.MainFig, 'UpdateGUITimer', updateGUITimer);

% Create a timer which will regularly set random values into the signal 
% data to send. It will call to a callback function in this file.
periodicDataUpdateTimer = timer(...
    'Period', 0.100,...
    'ExecutionMode', 'fixedSpacing', ...
    'TimerFcn', {@periodicDataUpdateTimerCallback, msgVehicleData, msgWheelSpeeds});
% Start the timer.
start(periodicDataUpdateTimer);
% Store the timer in the GUI so it persists for the life of the GUI.
setappdata(handles.MainFig, 'PeriodicDataUpdateTimer', periodicDataUpdateTimer);


% --- Executes when user attempts to close MainFig.
function MainFig_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to MainFig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Stop the periodic signal value update timer.
periodicDataUpdateTimer = getappdata(handles.MainFig, 'PeriodicDataUpdateTimer');
stop(periodicDataUpdateTimer);

% Stop the CAN channel.
canCh = getappdata(handles.MainFig, 'CANChannel');
stop(canCh);

% Stop the GUI update timer.
updateGUITimer = getappdata(handles.MainFig, 'UpdateGUITimer');
stop(updateGUITimer);

% Hint: delete(hObject) closes the figure
delete(hObject);


function updateGUITimerCallback(obj, ~, handles)
% updateGUITimerCallback Timer callback function for GUI updating.

% Receive the available CAN messages.
canCh = getappdata(handles.MainFig, 'CANChannel');
msg = receive(canCh, Inf);

% Exit if no messages were received as there is nothing to update.
if isempty(msg)
    return;
end

% Update the vehicle data parameters on the GUI with the most recent data.
msgVehicleData = extractRecent(msg, 'VehicleData');
if ~isempty(msgVehicleData)
    % Set the updated data values onto the GUI.
    set(handles.appValue, 'String', msgVehicleData.Signals.Accel_Pos);
    set(handles.espValue, 'String', msgVehicleData.Signals.Engine_Speed);
end

% Update the wheel speed parameters on the GUI with the most recent data.
msgWheelSpeeds = extractRecent(msg, 'WheelSpeeds');
if ~isempty(msgWheelSpeeds)
    % Set the updated data values onto the GUI.
    set(handles.wspLFValue, 'String', msgWheelSpeeds.Signals.LF_WSpeed);
    set(handles.wspLRValue, 'String', msgWheelSpeeds.Signals.LR_WSpeed);
    set(handles.wspRFValue, 'String', msgWheelSpeeds.Signals.RF_WSpeed);
    set(handles.wspRRValue, 'String', msgWheelSpeeds.Signals.RR_WSpeed);
end

function periodicDataUpdateTimerCallback(obj, ~, msgVehicleData, msgWheelSpeeds)
% periodicDataUpdateTimerCallback Timer callback function for message data updating.

% Set new data.
msgVehicleData.Signals.Accel_Pos = rand * 100;
msgVehicleData.Signals.Engine_Speed = rand * 8000;

% Set new data.
value = max(0.5, rand);
msgWheelSpeeds.Signals.LF_WSpeed = value * 1500;
msgWheelSpeeds.Signals.LR_WSpeed = value * 1505;
msgWheelSpeeds.Signals.RF_WSpeed = value * 1495;
msgWheelSpeeds.Signals.RR_WSpeed = value * 1490;



% --- Outputs from this function are returned to the command line.
function varargout = demoVNT_ManageCANMessageGUIFig_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 pbSendSingleMessages.
function pbSendSingleMessages_Callback(hObject, eventdata, handles)
% hObject    handle to pbSendSingleMessages (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get the channel and messages from the GUI for message transmit.
canCh = getappdata(handles.MainFig, 'CANChannel');
msgVehicleData = getappdata(handles.MainFig, 'MsgVehicleData');
msgWheelSpeeds = getappdata(handles.MainFig, 'MsgWheelSpeeds');

% Set new signal values and transmit.
msgVehicleData.Signals.Accel_Pos = rand * 100;
msgVehicleData.Signals.Engine_Speed = rand * 8000;
transmit(canCh, msgVehicleData);

% Set new signal values and transmit.
value = max(0.5, rand);
msgWheelSpeeds.Signals.LF_WSpeed = value * 1500;
msgWheelSpeeds.Signals.LR_WSpeed = value * 1505;
msgWheelSpeeds.Signals.RF_WSpeed = value * 1495;
msgWheelSpeeds.Signals.RR_WSpeed = value * 1490;
transmit(canCh, msgWheelSpeeds);

% --- Executes on button press in pbRepeatMessages.
function pbRepeatMessages_Callback(hObject, eventdata, handles)
% hObject    handle to pbRepeatMessages (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of pbRepeatMessages

% Get the channel and messages from the GUI for message transmit.
canCh = getappdata(handles.MainFig, 'CANChannel');
msgVehicleData = getappdata(handles.MainFig, 'MsgVehicleData');
msgWheelSpeeds = getappdata(handles.MainFig, 'MsgWheelSpeeds');

% Take action when repeat message tranmit is being turned on.
if logical(get(hObject, 'Value'))
    % Enable the periodic message transmit on the CAN channel.
    transmitPeriodic(canCh, msgVehicleData, 'On', 0.100);
    transmitPeriodic(canCh, msgWheelSpeeds, 'On', 0.100);
else
    % Shut off the periodic message transmit on the CAN channel.
    transmitPeriodic(canCh, msgVehicleData, 'Off');
    transmitPeriodic(canCh, msgWheelSpeeds, 'Off');
end



function appValue_Callback(hObject, eventdata, handles)
% hObject    handle to appValue (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 appValue as text
%        str2double(get(hObject,'String')) returns contents of appValue as a double


% --- Executes during object creation, after setting all properties.
function appValue_CreateFcn(hObject, eventdata, handles)
% hObject    handle to appValue (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 espValue_Callback(hObject, eventdata, handles)
% hObject    handle to espValue (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 espValue as text
%        str2double(get(hObject,'String')) returns contents of espValue as a double


% --- Executes during object creation, after setting all properties.
function espValue_CreateFcn(hObject, eventdata, handles)
% hObject    handle to espValue (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 wspLFValue_Callback(hObject, eventdata, handles)
% hObject    handle to wspLFValue (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 wspLFValue as text
%        str2double(get(hObject,'String')) returns contents of wspLFValue as a double


% --- Executes during object creation, after setting all properties.
function wspLFValue_CreateFcn(hObject, eventdata, handles)
% hObject    handle to wspLFValue (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 wspLRValue_Callback(hObject, eventdata, handles)
% hObject    handle to wspLRValue (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 wspLRValue as text
%        str2double(get(hObject,'String')) returns contents of wspLRValue as a double


% --- Executes during object creation, after setting all properties.
function wspLRValue_CreateFcn(hObject, eventdata, handles)
% hObject    handle to wspLRValue (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 wspRFValue_Callback(hObject, eventdata, handles)
% hObject    handle to wspRFValue (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 wspRFValue as text
%        str2double(get(hObject,'String')) returns contents of wspRFValue as a double


% --- Executes during object creation, after setting all properties.
function wspRFValue_CreateFcn(hObject, eventdata, handles)
% hObject    handle to wspRFValue (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 wspRRValue_Callback(hObject, eventdata, handles)
% hObject    handle to wspRRValue (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 wspRRValue as text
%        str2double(get(hObject,'String')) returns contents of wspRRValue as a double


% --- Executes during object creation, after setting all properties.
function wspRRValue_CreateFcn(hObject, eventdata, handles)
% hObject    handle to wspRRValue (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