Advertisement
gha890826

find GPGGA in NMEA

May 20th, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. // find_GPGGA_in_nmea.cpp : 此檔案包含 'main' 函式。程式會於該處開始執行及結束執行。
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include <cctype>
  9. using namespace std;
  10.  
  11. void main()
  12. {
  13.     fstream nmea("E:\\VisualStudio_used\\find_GPGGA_in_nmea\\nmea_sample.txt", ios::in);
  14.     fstream GPGGA_log("E:\\VisualStudio_used\\find_GPGGA_in_nmea\\GPGGA_log.txt", ios::out);
  15.     if (nmea)
  16.         cout << "成功開啟nmea_sample.txt\n";
  17.     else
  18.         cout << "nmea_sample.txt開啟失敗\n";
  19.     if (GPGGA_log)
  20.         cout << "成功建立GPGGA_log.txt\n";
  21.     else
  22.         cout << "GPGGA_log.txt建立失敗\n";
  23.  
  24.     string temp;
  25.     while (!nmea.eof())
  26.     {
  27.         nmea >> temp;
  28.  
  29.         //=====find $GPGGA=====
  30.         if (temp.find("$GPGGA") != string::npos)
  31.         {
  32.             cout << temp << endl;
  33.             GPGGA_log << temp << endl;
  34.         }
  35.         else
  36.             continue;
  37.  
  38.         //=====count check_char=====
  39.         char check_char=temp[1];
  40.         for (int i = 2; i < temp.find('*'); i++)
  41.         {
  42.             check_char ^= temp[i];
  43.         }
  44.  
  45.         //=====count check_num=====
  46.         int check_num;
  47.         if(isalpha(temp[temp.find('*')+1]))
  48.             check_num = (temp[temp.find('*') + 1] - 'A' + 10) * 16;
  49.         else
  50.             check_num = (temp[temp.find('*') + 1] - '0') * 16;
  51.         if(isalpha(temp[temp.find('*') + 2]))
  52.             check_num += (temp[temp.find('*') + 2] - 'A' + 10);
  53.         else
  54.             check_num += (temp[temp.find('*') + 2] - '0');
  55.         //=====print check resault=====
  56.         if (unsigned(check_char) == check_num)
  57.         {
  58.             cout << "檢查碼正確\t";
  59.             GPGGA_log << "檢查碼正確\t";
  60.         }
  61.         else
  62.         {
  63.             cout << "檢查碼錯誤\t";
  64.             GPGGA_log << "檢查碼錯誤\t";
  65.         }
  66.  
  67.         //=====print time=====
  68.         int comma_pos=0,comma_num=0;
  69.         while (comma_num != 1)
  70.         {
  71.             comma_pos=temp.find(',', comma_pos);
  72.             comma_num++;
  73.         }
  74.         int hour, min, sec;
  75.         hour = (temp[comma_pos + 1] - '0') * 10 + temp[comma_pos + 2] - '0';
  76.         min= (temp[comma_pos + 3] - '0') * 10 + temp[comma_pos + 4] - '0';
  77.         sec= (temp[comma_pos + 5] - '0') * 10 + temp[comma_pos + 6] - '0';
  78.         if (hour < 12)
  79.         {
  80.             cout << "Time: " << hour << ':' << min << ':' << sec << " AM UTC\t";
  81.             GPGGA_log << "Time: " << hour << ':' << min << ':' << sec << " AM UTC\t";
  82.         }
  83.         else if (hour == 12)
  84.         {
  85.             cout << "Time: " << hour << ':' << min << ':' << sec << " PM UTC\t";
  86.             GPGGA_log << "Time: " << hour << ':' << min << ':' << sec << " PM UTC\t";
  87.         }
  88.         else
  89.         {
  90.             cout << "Time: " << hour - 12 << ':' << min << ':' << sec << " PM UTC\t";
  91.             GPGGA_log << "Time: " << hour - 12 << ':' << min << ':' << sec << " PM UTC\t";
  92.         }
  93.  
  94.         //=====print 緯度=====
  95.         while (comma_num != 2)
  96.         {
  97.             comma_pos = temp.find(',', comma_pos+1);
  98.             comma_num++;
  99.         }
  100.         int comma_latitude_pos = temp.find(',', comma_pos + 1);
  101.         for (int i = comma_pos + 1; i < comma_latitude_pos; i++)
  102.         {
  103.             if (i == temp.find('.', comma_pos + 1) - 2)
  104.             {
  105.                 cout << " ";
  106.                 GPGGA_log << " ";
  107.             }
  108.             cout << temp[i];
  109.             GPGGA_log << temp[i];
  110.         }
  111.         cout <<" " << temp[comma_latitude_pos + 1] << '\t';
  112.         GPGGA_log << " " << temp[comma_latitude_pos + 1] << '\t';
  113.  
  114.         //=====print 經度=====
  115.         while (comma_num != 4)
  116.         {
  117.             comma_pos = temp.find(',', comma_pos + 1);
  118.             comma_num++;
  119.         }
  120.         int comma_longitude_pos = temp.find(',', comma_pos + 1);
  121.         //cout << "temp.find('.', comma_pos + 1)=" << temp.find('.', comma_pos + 1) << endl;
  122.         for (int i = comma_pos + 1; i < comma_longitude_pos; i++)
  123.         {
  124.             if (i == temp.find('.', comma_pos + 1) - 2)
  125.             {
  126.                 cout << " ";
  127.                 GPGGA_log << " ";
  128.             }
  129.             cout << temp[i];
  130.             GPGGA_log << temp[i];
  131.         }
  132.         cout << " " << temp[comma_longitude_pos + 1] << '\t';
  133.         GPGGA_log << " " << temp[comma_longitude_pos + 1] << '\t';
  134.  
  135.         //=====衛星數=====
  136.         while (comma_num != 7)
  137.         {
  138.             comma_pos = temp.find(',', comma_pos + 1);
  139.             comma_num++;
  140.         }
  141.         int satellite_num = (temp[comma_pos + 1] - '0') * 10 + (temp[comma_pos + 2] - '0');
  142.         cout <<"使用 "<< satellite_num<<"顆衛星\t";
  143.         GPGGA_log << "使用 " << satellite_num << "顆衛星\t";
  144.  
  145.         cout << endl<<endl;
  146.         GPGGA_log << endl << endl;
  147.     }
  148. }
  149.  
  150. // 執行程式: Ctrl + F5 或 [偵錯] > [啟動但不偵錯] 功能表
  151. // 偵錯程式: F5 或 [偵錯] > [啟動偵錯] 功能表
  152.  
  153. // 開始使用的秘訣:
  154. //   1. 使用 [方案總管] 視窗,新增/管理檔案
  155. //   2. 使用 [Team Explorer] 視窗,連線到原始檔控制
  156. //   3. 使用 [輸出] 視窗,參閱組建輸出與其他訊息
  157. //   4. 使用 [錯誤清單] 視窗,檢視錯誤
  158. //   5. 前往 [專案] > [新增項目],建立新的程式碼檔案,或是前往 [專案] > [新增現有項目],將現有程式碼檔案新增至專案
  159. //   6. 之後要再次開啟此專案時,請前往 [檔案] > [開啟] > [專案],然後選取 .sln 檔案
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement