% Acquisition TSG45 - SBE38 - GPS USB
% Decodage et enregistrement dans un fichier horaire

disp('---------------- START ---------------------');

%% Create a serial port object for GPS.
obj1 = instrfind('Type', 'serial', 'Port', 'COM8', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
    obj1 = serial('COM8');
else
    fclose(obj1);
    obj1 = obj1(1);
end
% Set the BaudRate
obj1.BaudRate = 4800;
obj1.Timeout= 50;
% Connect to instrument object, obj1.

%% Create a serial port object for Interface box.
obj2 = instrfind('Type', 'serial', 'Port', 'COM1', 'Tag', '');
% % Create the serial port object if it does not exist
% % otherwise use the object that was found.
if isempty(obj2)
    obj2 = serial('COM1');
else
    fclose(obj2);
    obj2 = obj2(1);
end
% % Set the BaudRate
obj2.BaudRate = 9600;
% Set the TimeOut
obj2.Timeout= 50;
fopen(obj2);

% Jour du premier fichier
jour0 = 0;
time0 = 0;

line = 0;
dat = [];
nbl = 59;

%% Boucle infinie !
for i=1:10000000
    % Acquisition Interface box

    dataint = 'hello';
    %   disp([dataint '  ' num2str(i) '   ' num2str(obj2.ValuesReceived)]);
    while isempty(strmatch('t1=',dataint)) == 1
        % readasync(obj2);
        % dataint = fgetl(obj2);
        dataint = fscanf(obj2);
        %   disp([dataint '  ' num2str(i) '   ' num2str(obj2.ValuesReceived)]);
    end
    %disp(dataint);
    %   fclose(obj2);

    %% Lecture GPS
    fopen(obj1);
    %   disp('GPS port open');
    % We only want the strings with $GPRMC…
    datagps = '';
    while isempty(strmatch('$GPRMC',datagps))
        datagps = fscanf(obj1);
    end

    %   disp(['datagps= ' datagps]);

    [frame,data] = strtok(datagps,',');
    [time,data] = strtok(data,',');
    [letter,data] = strtok(data,',');
    [lat,data] = strtok(data,',');
    [nsCardinal,data] = strtok(data,',');
    [long,data] = strtok(data,',');
    [ewCardinal,data] = strtok(data,',');
    [letter,data] = strtok(data,',');
    [letter,data] = strtok(data,',');
    [date,data] = strtok(data,',');

    datep = ['20' date(5:6) date(3:4) date(1:2)];
    %pause(3);
    fclose(obj1);
    if str2num(date) ~= jour0;
        % if str2num(time) > time0+1000
        for k=1:line;
            fprintf(fid,'%s\n',dat(k,:));
        end
        line = 0;
        dat = [];

        % Fermeture du fichier si ouvert
        % fclose('all');
        % Creation du fichier a partir de cette date et de l'heure courante
        filename = ['C:\TSG\tara_tsg_' datep '_' time(1:6) '.txt'];
        fid=fopen(filename,'a');
        disp([filename ' created now']);
        time0 = str2num(time);
        jour0 = str2num(date);
    end

    % Concatenation de DATE, TIME, Latitude, Longitude, T(38), T(45), C(45)
    text = [dataint(1:49) ', lat=' num2str(lat(1:2)) ' ' num2str(lat(3:end)) ' ' nsCardinal ', lon=',num2str(long(1:3)) ' ' num2str(long(4:end)) ' ' ewCardinal ', hms=' num2str(time(1:6)) ', dmy=' num2str(date) ];

    if line == nbl
        fclose(obj2);
        for k=1:nbl;
            fprintf(fid,'%s\n',dat(k,:));

        end
        fprintf(fid,'%s\n',text);
        if line+1 < 10
            disp(['0' num2str(line+1) '/' num2str(nbl+1) '    ' text]);
        else
            disp([num2str(line+1) '/' num2str(nbl+1) '    ' text]);
        end
        dat = [];
        line = 0;
        disp(['----------------> All data lines saved now in ' filename ]);

        fopen(obj2);
    else
        if line+1 < 10
            disp(['0' num2str(line+1) '/' num2str(nbl+1) '    ' text]);
        else
            disp([num2str(line+1) '/' num2str(nbl+1) '    ' text]);
        end
        line = line+1;
        dat = [dat;char(text)];
    end
end

%% Free the serial ports
fclose(obj2);
close('all');
