Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // find_GPGGA_in_nmea.cpp : 此檔案包含 'main' 函式。程式會於該處開始執行及結束執行。
- //
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <cctype>
- using namespace std;
- void main()
- {
- fstream nmea("E:\\VisualStudio_used\\find_GPGGA_in_nmea\\nmea_sample.txt", ios::in);
- fstream GPGGA_log("E:\\VisualStudio_used\\find_GPGGA_in_nmea\\GPGGA_log.txt", ios::out);
- if (nmea)
- cout << "成功開啟nmea_sample.txt\n";
- else
- cout << "nmea_sample.txt開啟失敗\n";
- if (GPGGA_log)
- cout << "成功建立GPGGA_log.txt\n";
- else
- cout << "GPGGA_log.txt建立失敗\n";
- string temp;
- while (!nmea.eof())
- {
- nmea >> temp;
- //=====find $GPGGA=====
- if (temp.find("$GPGGA") != string::npos)
- {
- cout << temp << endl;
- GPGGA_log << temp << endl;
- }
- else
- continue;
- //=====count check_char=====
- char check_char=temp[1];
- for (int i = 2; i < temp.find('*'); i++)
- {
- check_char ^= temp[i];
- }
- //=====count check_num=====
- int check_num;
- if(isalpha(temp[temp.find('*')+1]))
- check_num = (temp[temp.find('*') + 1] - 'A' + 10) * 16;
- else
- check_num = (temp[temp.find('*') + 1] - '0') * 16;
- if(isalpha(temp[temp.find('*') + 2]))
- check_num += (temp[temp.find('*') + 2] - 'A' + 10);
- else
- check_num += (temp[temp.find('*') + 2] - '0');
- //=====print check resault=====
- if (unsigned(check_char) == check_num)
- {
- cout << "檢查碼正確\t";
- GPGGA_log << "檢查碼正確\t";
- }
- else
- {
- cout << "檢查碼錯誤\t";
- GPGGA_log << "檢查碼錯誤\t";
- }
- //=====print time=====
- int comma_pos=0,comma_num=0;
- while (comma_num != 1)
- {
- comma_pos=temp.find(',', comma_pos);
- comma_num++;
- }
- int hour, min, sec;
- hour = (temp[comma_pos + 1] - '0') * 10 + temp[comma_pos + 2] - '0';
- min= (temp[comma_pos + 3] - '0') * 10 + temp[comma_pos + 4] - '0';
- sec= (temp[comma_pos + 5] - '0') * 10 + temp[comma_pos + 6] - '0';
- if (hour < 12)
- {
- cout << "Time: " << hour << ':' << min << ':' << sec << " AM UTC\t";
- GPGGA_log << "Time: " << hour << ':' << min << ':' << sec << " AM UTC\t";
- }
- else if (hour == 12)
- {
- cout << "Time: " << hour << ':' << min << ':' << sec << " PM UTC\t";
- GPGGA_log << "Time: " << hour << ':' << min << ':' << sec << " PM UTC\t";
- }
- else
- {
- cout << "Time: " << hour - 12 << ':' << min << ':' << sec << " PM UTC\t";
- GPGGA_log << "Time: " << hour - 12 << ':' << min << ':' << sec << " PM UTC\t";
- }
- //=====print 緯度=====
- while (comma_num != 2)
- {
- comma_pos = temp.find(',', comma_pos+1);
- comma_num++;
- }
- int comma_latitude_pos = temp.find(',', comma_pos + 1);
- for (int i = comma_pos + 1; i < comma_latitude_pos; i++)
- {
- if (i == temp.find('.', comma_pos + 1) - 2)
- {
- cout << " ";
- GPGGA_log << " ";
- }
- cout << temp[i];
- GPGGA_log << temp[i];
- }
- cout <<" " << temp[comma_latitude_pos + 1] << '\t';
- GPGGA_log << " " << temp[comma_latitude_pos + 1] << '\t';
- //=====print 經度=====
- while (comma_num != 4)
- {
- comma_pos = temp.find(',', comma_pos + 1);
- comma_num++;
- }
- int comma_longitude_pos = temp.find(',', comma_pos + 1);
- //cout << "temp.find('.', comma_pos + 1)=" << temp.find('.', comma_pos + 1) << endl;
- for (int i = comma_pos + 1; i < comma_longitude_pos; i++)
- {
- if (i == temp.find('.', comma_pos + 1) - 2)
- {
- cout << " ";
- GPGGA_log << " ";
- }
- cout << temp[i];
- GPGGA_log << temp[i];
- }
- cout << " " << temp[comma_longitude_pos + 1] << '\t';
- GPGGA_log << " " << temp[comma_longitude_pos + 1] << '\t';
- //=====衛星數=====
- while (comma_num != 7)
- {
- comma_pos = temp.find(',', comma_pos + 1);
- comma_num++;
- }
- int satellite_num = (temp[comma_pos + 1] - '0') * 10 + (temp[comma_pos + 2] - '0');
- cout <<"使用 "<< satellite_num<<"顆衛星\t";
- GPGGA_log << "使用 " << satellite_num << "顆衛星\t";
- cout << endl<<endl;
- GPGGA_log << endl << endl;
- }
- }
- // 執行程式: Ctrl + F5 或 [偵錯] > [啟動但不偵錯] 功能表
- // 偵錯程式: F5 或 [偵錯] > [啟動偵錯] 功能表
- // 開始使用的秘訣:
- // 1. 使用 [方案總管] 視窗,新增/管理檔案
- // 2. 使用 [Team Explorer] 視窗,連線到原始檔控制
- // 3. 使用 [輸出] 視窗,參閱組建輸出與其他訊息
- // 4. 使用 [錯誤清單] 視窗,檢視錯誤
- // 5. 前往 [專案] > [新增項目],建立新的程式碼檔案,或是前往 [專案] > [新增現有項目],將現有程式碼檔案新增至專案
- // 6. 之後要再次開啟此專案時,請前往 [檔案] > [開啟] > [專案],然後選取 .sln 檔案
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement