% programme de recuperation des donnees DROP SONDE temps r�el
% a partir des donnees RMD et GONIO
% delta RMD/PC 17:27:21/19:38:20

%% raz
clear all;
close all;
fclose all;

warning off


%% dossier d'enregistrements
path_save=uigetdir('','S�l�ctionnez le dossier ou seront enregist�es les donn�es:');
cd(path_save);
now_save=datestr(now,31);

%%fenetre de selection des parametres du port com et chargement 
select_com_sbe_tr;

wait_param=0;
while wait_param==0
    wait_param=getappdata(0,'wait_param');
    pause(0.2);
end


Baud_Rate2=str2num(getappdata(0,'Baud_Rate2'));
data_bits2=str2num(getappdata(0,'data_bits2'));
com2=getappdata(0,'com2');
num_ct=getappdata(0,'num_ct');

s2=serial(com2,'BaudRate',Baud_Rate2,'dataBits',data_bits2,'timeout',1);
fopen(s2);
pause(1)
set(s2,'Terminator','CR');

%% syncronisation SBE_matlab
nowvec=datevec(now);
commande=['MMDDYY=' datestr(nowvec,5) datestr(nowvec,7) datestr(nowvec,11)];
fprintf(s2,commande); pause(1);
nowvec=datevec(now);
commande2=datestr(nowvec,13);
commande2([3 6])=[];
commande2=['HHMMSS=' commande2];
fprintf(s2,commande2); pause(1);
while get(s2,'bytesavailable')~=0
    fscanf(s2);
    pause(0.1)
end

%% lancement des acquisitions
res_sbe=[];

while_s;

disp('D�but d acquisition')

test=0;
fclose(s)
fopen(s)
pause(1)
i=0;
indic=0;
indic2=0;

temp_s2=-1;
tempold=datenum('00/00/0000 00:00:00');

while 1


        tempnow=now;

            if tempnow-tempold>datenum('00/00/0000 00:00:08')
                fprintf(s2,'ts');
                indic=1;
                tempold=tempnow;
            elseif tempnow-tempold>datenum('00/00/0000 00:00:04') & indic==1
                while get(s2,'bytesavailable')>52
                temp_s2=fscanf(s2);
                    if get(s2,'bytesavailable')==0, return; end
                end
                if length(temp_s2)==52
                    temp_sbe=temp_s2([30:53-1]);
                    temp_sbe([1 13])=[];
                    temp_sbe([3 7])='-';
                    res_sbe_temp=[datenum(temp_sbe,0)   str2num(temp_s2([3:9]))   str2num(temp_s2([12:18]))*10   str2num(temp_s2([22:28]))];
                    res_sbe=[res_sbe ; res_sbe_temp];
                    indic2=1;
                    indic=0; 
                    
                    figure(1)
                        subplot(2,1,1)
                        AXA2=plot(res_dif(:,1),res_dif(:,2),'r');
                        datetick('x',13);
                        grid on
                        
                    	subplot(2,1,2)
                        AXB2=plot(res_dif(:,1),res_dif(:,3),'r');
                        datetick('x',13);
                        grid on
                        
                        drawnow;
                end
            end  

    if getappdata(0,'stop_w')==1
        break
    end
    pause(0.01)
end    

res_sbe=res_dif; 
name=[path_save '\SBE_' now_save([9 10 6 7 3 4]) '_' now_save([12 13 15 16]) '.mat'];     %save datas 
save(name , 'res_sbe');

fclose(s2)
