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

% Last Modified by GUIDE v2.5 24-Feb-2014 10:59:57

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

% Choose default command line output for Test_6
handles.output = hObject;
% --------------------------------------------------------
handles.IC=serial('com1','BaudRate', 56000, 'Terminator','LF','parity','none');
t_periode=0.2;
handles.timer=timer('TimerFcn',{@PlotCellFcn, handles},'Period',t_periode,'ExecutionMode','fixedRate');                           %timer runs continuously
set(handles.timer,'UserData',t_periode);
handles.clock=timer('TimerFcn',{@UpdateTimeFcn, handles},'Period',1,'ExecutionMode','fixedRate');
start(handles.clock);
handles.strom(1,1)=0;
handles.zeit(1,1)=0;
set(handles.zeit,'UserData',1);
% --------------------------------------------------------
% Update handles structure
guidata(hObject, handles);
default_Values(hObject, eventdata, handles)

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


% --- Outputs from this function are returned to the command line.
function varargout = Test_6_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 PlotCellFcn(timerObj,timerEvent,handles)
% function is called by MessTimer and gets data from BMS,
% ploting (Updating) Cell Voltages and Cell Temperature in axes=Cell_plot
% as well as current in axes=current
count=get(handles.zeit,'UserData');
t_periode=get(handles.timer,'UserData');
count=count+1;
fprintf(handles.IC,'spannung');
handles.spannung(count,1)=str2double(fscanf(handles.IC));
bar(handles.axes1,handles.spannung(count,1));
fprintf(handles.IC,'strom');
handles.strom(count,1)=str2double(fscanf(handles.IC));
handles.zeit(count,1)=handles.zeit([count-1],1)+t_periode;
anz=length(handles.strom);
if anz<=8
    plot(handles.axes2,handles.zeit,handles.strom);
    axis(handles.axes2,[0 10 0 250]);
else
    plot(handles.axes2,handles.zeit([anz-8]:anz),handles.strom([anz-8]:anz));
    axis(handles.axes2,[anz-8 anz+2 0 250]);
end
set(handles.zeit,'UserData',count);


function UpdateTimeFcn(clockObj,clockEvent,handles)
time=datestr(now,'HH:MM:SS');
set(handles.clock_box,'String',time);


function default_Values(hObject, eventdata, handles)
% set the default Values for several Items
set(handles.LED_rot,'Value',0);
set(handles.LED_gruen,'Value',0);
set(handles.LED_gelb,'Value',0);
set(handles.connect,'String','Verbinden');
set(handles.start,'String','Start');
set(handles.LED_rot,'Enable', 'Off');
set(handles.LED_gruen,'Enable', 'Off');
set(handles.LED_gelb,'Enable', 'Off');


% --- Executes on button press in connect.
function connect_Callback(hObject, eventdata, handles)
% hObject    handle to connect (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 connect
state=get(hObject,'Value');
if state == 1
    fopen(handles.IC);
    set(hObject,'String','Trennen');
    set(handles.LED_rot,'Enable', 'On');
    set(handles.LED_gruen,'Enable', 'On');
    set(handles.LED_gelb,'Enable', 'On');
    fprintf(handles.IC,'Status');
    status=fscanf(handles.IC);
    if strcmp('Verbunden',status(1:end-2))
        set(handles.start,'ForegroundColor',[0 1 0]);
    else
        set(handles.start,'ForegroundColor',[1 0 0]);
    end
else
    set(handles.start,'ForegroundColor',[1 0 0]);
    all=instrfindall;
    fclose(all);
    default_Values(hObject, eventdata, handles);
end

% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject    handle to start (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 start
state=get(hObject,'Value');
if state == 1
    start(handles.timer);
    set(hObject,'String','Stop');
    fprintf(handles.IC,'spannung');
    handles.spannung(1,1)=str2double(fscanf(handles.IC));
else
    stop(handles.timer);
    set(hObject,'String','Start');
end


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
% set 'NextPlot' to 'replacechildren to remain axis property
axis([0 2 0 250]);
axis manual;
grid on;


% --- Executes on button press in LED_gruen.
function LED_gruen_Callback(hObject, eventdata, handles)
% hObject    handle to LED_gruen (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 LED_gruen
state=get(handles.LED_gruen,'Value');
if state==1
    fprintf(handles.IC, 'G')
else
    fprintf(handles.IC,'g')
end


% --- Executes on button press in LED_gelb.
function LED_gelb_Callback(hObject, eventdata, handles)
% hObject    handle to LED_gelb (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 LED_gelb
state=get(handles.LED_gelb,'Value');
if state==1
    fprintf(handles.IC, 'Y')
else
    fprintf(handles.IC,'y')
end


% --- Executes on button press in LED_rot.
function LED_rot_Callback(hObject, eventdata, handles)
% hObject    handle to LED_rot (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 LED_rot
state=get(handles.LED_rot,'Value');
if state==1
    fprintf(handles.IC, 'R')
else
    fprintf(handles.IC,'r')
end


% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
stop(handles.clock);
all=instrfindall;
fclose(all);