% 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_Rate=str2num(getappdata(0,'Baud_Rate'));
data_bits=str2num(getappdata(0,'data_bits'));
com=getappdata(0,'com');
Baud_Rate2=str2num(getappdata(0,'Baud_Rate2'));
data_bits2=str2num(getappdata(0,'data_bits2'));
com2=getappdata(0,'com2');
num_ct=getappdata(0,'num_ct');

s = serial(com,'BaudRate',Baud_Rate,'dataBits',data_bits,'timeout',1);    %R�glage du port COM serie

%% load ficjier d etalonnage .PRB
[tt cc]=read_prb(['F:\CT\'], ['CTP' num_ct '.PRB']);


%% lancement des acquisitions

res=[]; 
date=[];

while_s;

disp('D�but d acquisition')

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


while 1

    temp=0;
    str=0;
    if get(s,'bytesavailable')==6
      
        [temp,A ,B]=fread(s,6);
        tempnow=now;
        if (length(temp)==6) & (get(s,'bytesavailable')==0)
            
            str = dec2bin(temp,5);
            
            if size(str,2)>7
                str=str(:,[size(str,2)-7:end]);
                i=i+1;                           
                    if bin2dec(str(:,end)')==54 & bin2dec(str(3,1:5))==3 & bin2dec(str(6,1:5))==4  % adresse message 1 et 2       
                    	date=[date ; tempnow]; 
                    	T_temp=bin2dec([str(3,[6:7]) str(2,[1:7]) str(1,[1:7])]);   % message 1
                    	C_temp=bin2dec([str(6,[6:7]) str(5,[1:7]) str(4,[1:7])]);   % message 2      
                        T_temp=tt(1)+T_temp*tt(2); 
                        C_temp=cc(1)+C_temp*cc(2)+C_temp.^2*cc(3)+C_temp.^3*cc(4); 
                        res=[res ; tempnow T_temp C_temp];
                        
                    	figure(1)
                        subplot(2,1,1)
                    	AXA1=plot(res(:,1),res(:,2));
                        datetick('x',13);
                        grid on

                    	subplot(2,1,2)
                    	AXB1=plot(res(:,1),res(:,3));
                        datetick('x',13);
                        grid on
                        
                        drawnow;
                     end
            end
        end
    elseif get(s,'bytesavailable')>6
        fread(s,get(s,'bytesavailable')-mod(get(s,'bytesavailable'),6));
    end

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

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


fclose(s)
fclose(s2)
