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

disp('---------- START ---------------------');
warning off MATLAB:serial:fscanf:unsuccessfulRead;

% Create a serial port object for GPS.
obj1 = instrfind('Type', 'serial', 'Port', 'COM6', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
    obj1 = serial('COM6');
else
    fclose(obj1);
    obj1 = obj1(1);
end
% Set the BaudRate
obj1.BaudRate = 4800;
% Connect to instrument object, obj1.
fopen(obj1);
% Jour du premier fichier
jour0 = 0;
time0 = 0;

disp('GPS port open');

% % % Create a serial port object for Interface box.
% obj2 = instrfind('Type', 'serial', 'Port', 'COM5', 'Tag', '');
% % % Create the serial port object if it does not exist
% % % otherwise use the object that was found.
% if isempty(obj2)
%     obj2 = serial('COM5');
% else
%     fclose(obj2);
%     obj2 = obj1(2);
% end
% % % Set the BaudRate
% obj2.BaudRate = 9600;
% % % Connect to instrument object, obj2.
% fopen(obj2);
% disp('TSG port open');

%% Boucle infinie !
for i=1:10000000
    
    %% Lecture GPS
    % We only want the strings with $GPRMC…
    datagps = '';
    while isempty(strmatch('$GPRMC',datagps))
        datagps = fscanf(obj1);
    end
    % Si jour change, nouveau fichier
    [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,',');
    
    %% TEST Trame NMEA
    if size(datagps,2) > 50
        %if str2num(date) ~= jour0;
        if str2num(time) > time0+1000
            % Fermeture du fichier si ouvert
            fclose('all');
            % Creation du fichier a partir de cette date et de l'heure courante
            filename = ['tsg_' time(1:6) '.txt'];
            fid=fopen(filename,'w');
            disp(filename);
            time0 = str2num(time);
            jour0 = str2num(date);
        end
        
        % Acquisition Interface box
        %   dataint = fscanf(obj2);
        dataint = datagps(1:10);
        
        %% Test Position
        
        % Concatenation de DATE, TIME, Latitude, Longitude, T(38), T(45), C(45)
        text = [dataint ', 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) ];
        dataok = datagps;
        if str2num(time)/5 == ceil(str2num(time)/5)
            disp(text);
            % Ecriture dans fichier
            fprintf(fid,'%s\n',text);
        end
        % Pause 1s
       
        %   pause(1);
    else
           disp(['WAITING for correct NMEA : ' datagps ]);
    end
    
end


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