
clear ; 
ptr = 1;
starttime = datevec(datetime( 'now' ));
rel_plot = subplot( 2, 3, 1 ); rel_axis = plot( nan );
abs_plot = subplot( 2, 3, 2 ); abs_axis = plot( nan );
ph_plot = subplot( 2, 3, 4 ); ph_axis = plot( nan );
mv_plot = subplot( 2, 3, 5 ); mv_axis = plot( nan );
temp_plot = subplot( 2, 3, 3 ); temp_axis = plot( nan );
press_plot = subplot( 2, 3, 6 ); press_axis = plot( nan ); 

title( rel_plot, 'Dissolved oxygen relative (%Sat)' )
title( abs_plot, 'Dissolved oxygen absolute (mg/l)' ); 
title( ph_plot, 'pH' )
title( mv_plot, 'Redox potential in mV' ); 
title( temp_plot, 'Temperature in °C' ); 
title( press_plot, 'Barometric pressure in mBar' ); 

xlabel( rel_plot, 'Time in seconds' );
xlabel( abs_plot, 'Time in seconds' );
xlabel( ph_plot, 'Time in seconds' );
xlabel( mv_plot, 'Time in seconds' );
xlabel( temp_plot, 'Time in seconds' );
xlabel( press_plot, 'Time in seconds' );


while true
    [state, rel, abs, partialp, ph, mv, temp, press] = getOxyScanreading( '192.168.178.44', 10000 );
    if state == 0
        sampletime(ptr) = etime( datevec(datetime('now')), starttime );
        atemp(ptr) = temp; apress(ptr) = press; arel(ptr) = rel; amv(ptr) = mv;
        aabs(ptr) = abs; aph(ptr) = ph;
        ptr = ptr + 1;      
        
        set( rel_axis, 'YDATA', arel ); set( rel_axis, 'XDATA', sampletime );
        set( abs_axis, 'YDATA', aabs ); set( abs_axis, 'XDATA', sampletime );
        set( ph_axis, 'YDATA', aph ); set( ph_axis, 'XDATA', sampletime );               
        set( mv_axis, 'YDATA', amv ); set( mv_axis, 'XDATA', sampletime );        
        set( temp_axis, 'YDATA', atemp ); set( temp_axis, 'XDATA', sampletime );        
        set( press_axis, 'YDATA', apress ); set( press_axis, 'XDATA', sampletime );        
        drawnow;
    end    
    pause( 2.5 );
end




function [state, rel, abs, partialp, ph, mv, temp, press] = getOxyScanreading( ip, port )
%ip and port as shown at the OxyScan 300 PC Software
%update every 5 seconds
    state = 0;          % if successfull state = 0
    rel         = 0;    % Dissolved oxygen %Saturation
    abs         = 0;    % Dissolved oxygen absolute in mg/l
    partialp    = 0;    % Dissolved oxygen partial pressure in mmHg
    ph          = 0;    % pH (if pH sensor connected)
    mv          = 0;    % Redox potential in mV (if redox sensor connected)
    temp        = 0;    % Temperature in °C
    press       = 0;    % Barometric pressure in mBar
    t = tcpip( ip, port );
    try
        fopen(t);
    catch 
        state = 2;
    end
    if state == 0        
        if t.Status == 'open'
            fprintf( t, 'get\n' );
            receivedstring = fscanf( t );
            fclose(t);
            receivedelements = split(  receivedstring );
            noelements = size( receivedelements );    
            if noelements == [ 15, 1 ]               
                rel         = sscanf( receivedelements{1}, '%f' );
                abs         = sscanf( receivedelements{3}, '%f' );
                partialp    = sscanf( receivedelements{5}, '%f' );
                ph          = sscanf( receivedelements{7}, '%f' );
                mv          = sscanf( receivedelements{9}, '%f' );
                temp        = sscanf( receivedelements{11}, '%f' );
                press       = sscanf( receivedelements{13}, '%f' );      
            else
                state = 1;
            end            
        end
    end    
end




    
