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

% Last Modified by GUIDE v2.5 03-Jul-2011 17:46:25

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = com_to_matlab_2011_07_03_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 serialGetData(s, eventdata, handles)
 set(handles.ausgabe,'string', '- serialGetData - ');    %%<<<--------------- FUNKTIONIERT NICHT!!! 
 
 data_dummy = fgetl(s);                                     % Daten werden gelesen
 
 fprintf(' %s\n ', data_dummy);


% --- 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)
fprintf('----- CONNECT ----- \n');
set(handles.ausgabe,'string', 'TEST');
handles.s1 = openport;
guidata(hObject, handles);

% Function to open the COM-Port
function s1 = openport
out = instrfind('Port', 'COM2');                                           % Check to see if THAT serial port is already defined in MATLAB
if (~isempty(out))                                                         % It is
    disp('WARNING:  port in use.  Closing.')
    if (~strcmp(get(out(1), 'Status'),'open'))                             % Is it open?
        delete(out(1));                                                    % If not, delete
    else                                                                   % is open
        fclose(out(1));
        delete(out(1)); 
    end
end
s1 = serial('COM2','BaudRate',9600);
s1.Terminator = 'LF';                       % Bei <LF> Einlesen beenden (--> \n in C)
s1.BytesAvailableFcn =@serialGetData;
fopen(s1);

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

handles= guidata(hObject); 
set(handles.ausgabe,'string', 'RESET');
fclose(handles.s1);                % COM schließen
delete(handles.s1);                % COM löschen
guidata(hObject, handles);




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


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