⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xxx_mc_gpx.cpp

📁 GPS Source code with C++. It is usefull for introducing gps
💻 CPP
字号:
/*
  Name:         mc_gpx.cpp
  Copyright:    Keins
  Author:       Thomas Wiens
  Date: 19.02.07 18:04
  Description:  The stored Gps points are read from the controller and saved as trackpoints in the gpx format.

*/
/* Using the serial com port
   MSDN:
   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp
 
   serial com port usage class
   http://www.winapi.net/index.php?inhalt=t3
*/
#include "mc_gpx.h"
#define DEBUG

using namespace std;

ofstream output;
CSerial com;

const char GpxHeader[] =  "<?xml version=\"1.0\"?>\n"
                    "<gpx version=\"1.0\"\n"
                    " creator=\"GPS-Logger by T.Wiens\"\n"
                    " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
                    " xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
                    " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0"
                    " http://www.topografix.com/GPX/1/0/gpx.xsd\">\n";
                    
// *****************************************************************************
// function:
//                       get_data_from_logger
// 躡ergabeparameter:
//                       int com_port
//                                   Number of the serial port that will be opened
//                       char trackname
//                                   name of the track (max. 50 characters)
//                       char track_description            
//                                   description of the track (max. 50 characters)
//                       char waypoint_names
//                                   description of the waypoints (if available, the numbers will be increased)
//  R點kgabewert:
//                       0 -> no failure
//                       >0 -> Failure code see headerfile                                                                                                                                                            
// ***************************************************************************** 
int get_data_from_logger(int com_port, char filename[50],char trackname[50], char track_description[50], char waypoint_names[50]) {
    tGPXtrackPoint gpx;
    const char trkpt_request[] = "$TREQ*\r\n";
    const char waypt_request[] = "$WREQ*\r\n";
    char buf[61], temp [20];
    int datasets;
        
    // *** Open com port **
    if(!com.Open(com_port, 4800, 8, NOPARITY, ONESTOPBIT)) {
        cout << "error: Com port could not be opened\n";
        return GPX_ERR_SERIAL_OPEN;
    }
    // *** Open file ***
    output.open(filename, ios_base::out);
    if (!output.good()){
        cout << "error: file " << filename << " could not be opened." << endl;
        return GPS_ERR_FILE_OPEN;
    }
    // write GPX-Header
    output << GpxHeader;
    output << "<trk>\n";
    output << "<name>" << trackname << "</name>\n";
    output << "<desc>" << track_description << "</desc>\n";
    output << "<trkseg>\n";    
    
    // request for number of stored location files
    com.SendData(trkpt_request, sizeof(trkpt_request));
    // get answer
    // answer in form $TCNT,12*<CR><NL>       
    if(com.ReadData(buf, 20)) {
        extract_data(buf, "$TCNT,", "*\r\n");
        datasets = atoi(buf);
        cout << "there are"  << datasets << " locations stored" << endl;
    }
    else {
        cout << "error: device does not respond correctly" << endl;
        com.Close();
        return GPS_ERR_CONTR_COM;
    }    
    
    // get Trackpoint files
    for (int i = 0; i < datasets; i++) {
        if (!get_gps_trkPoint(i, buf)) { 
            get_gpx_data(buf, &gpx);  
            add_gpx_trkPoint_to_file(&gpx);  
        }        
        else {
            com.Close();   
            return GPS_ERR_CONTR_COM;
        }
        
    } 
    // end of track segment
    output << "</trkseg>\n";
    output << "</trk>\n";     
    
    // request for number of strored Waypoint files
    com.SendData(waypt_request, sizeof(waypt_request));
    if(com.ReadData(buf, 20)) {
        extract_data(buf, "$WCNT,", "*\r\n");
        datasets = atoi(buf);
        cout << "there are"  << datasets << " Waypoints stored" << endl;
    }
    else {
        cout << "error: device does not respond correctly" << endl;
        com.Close();
        return GPS_ERR_CONTR_COM;
    }  
    if (datasets > 0) {
        // get Waypoint files
        for (int i = 0; i < datasets; i++) {   
            if (!get_gps_trkPoint(get_waypoint(i), buf)) { 
                get_gpx_data(buf, &gpx);  
                sprintf(temp, "%s %d", waypoint_names, i + 1);
                add_gpx_wayPoint_to_file(&gpx, temp);  
            }
            else {
                com.Close();                           
                return GPS_ERR_CONTR_COM;
            }
        }       
    }
    // end of GPX segment
    output << "</gpx>\n";   
    output.close();   
    com.Close();    
    return 0;
}

// *****************************************************************************
// Get Trackpoint
// ***************************************************************************** 
int get_gps_trkPoint(int trkpt_number, char *line) {
    char buf[61], str[20];
    int ret_chars;
    // making the request string
    strcpy(str, "$TSET,");
    sprintf(buf, "%i", trkpt_number);
    strcat(str, buf);
    strcat(str, "*\r\n");   
    // sending the request
    com.SendData(str, strlen(str));
    ret_chars = com.ReadData(buf, TRKPT_DATASET_LENGTH);        
    // calculating the answer
    if (ret_chars) {
        extract_data(buf, "$", "*\r\n");
        if (strlen(buf) == TRKPT_DATASET_LENGTH - 4) {                             
            #ifdef DEBUG
            cout << "File Nr. " << trkpt_number + 1 << ": " << buf << endl;   
            #endif    
            strcpy(line, buf);              
        }
        else {
            #ifdef DEBUG 
            cout << "Error in file length!" << endl;
            #endif
            return GPS_ERR_CONTR_COM;
        }                     
    }
    else {
        #ifdef DEBUG 
        cout << "error: device does not respond correctly" << endl;
        #endif
        return GPS_ERR_CONTR_COM;    
    }
    return 0; 
}

// ***************************************************************************** 
// Get Waypoint
// ***************************************************************************** 
int get_waypoint(int point_number) {
    char buf[61], str[20];  
    // making the request string
    strcpy(str, "$WSET,");
    sprintf(buf, "%i", point_number);
    strcat(str, buf);
    strcat(str, "*\r\n");   
    // send request
    com.SendData(str, strlen(str));     
    // calculating the answer
    if (com.ReadData(buf, WAYPT_DATASET_LENGTH)) {
        extract_data(buf, "$", "*\r\n");
        if (strlen(buf) == WAYPT_DATASET_LENGTH - 4) {                             
            #ifdef DEBUG
            cout << "File Nr. " << point_number + 1 << ": " << buf << endl;   
            #endif
            buf[5] = '\0';    
            return (atoi(buf));                   
        }
        else {
            #ifdef DEBUG 
            cout << "Error in file length!" << endl;
            #endif
            return GPS_ERR_CONTR_COM;
        }                     
    }
    else {
        #ifdef DEBUG 
        cout << "error: device does not respond correctly" << endl;
        #endif
        return GPS_ERR_CONTR_COM;    
    }     
}

// *****************************************************************************       
// Sorry, koksal i dont really know how to translate this part. I'll try:
// a pointer ??? is put on a string. In beginning and end, this character string is removed.
// Es wird ein Zeiger auf einen String 黚ergeben. Bei Auftreten von
// begin und end werden diese Zeichenketten entfernt.
// ***************************************************************************** 
int extract_data(char *ext_string, char begin[10], char end[10]) {
    char *ptr_anf;
    char *ptr_end;
    char buf[100];
    char temp[100];    
    strncpy(buf, ext_string, 99);    
    ptr_anf = strstr(buf, begin);
    ptr_end = strstr(buf, end);       
    if (ptr_anf && ptr_end) {
        *ptr_end = '\0';
        strcpy(temp, ptr_anf + strlen(begin)); 
        strcpy(ext_string, temp);
        return 0;
    }
    else {
        return 1;       // pointer ??? string not found
    }
}

// *****************************************************************************  
// Changing the data into the GPX file format
// *****************************************************************************  
void get_gpx_data(char *line, tGPXtrackPoint *gpx) {
    double degree, minutes, dezimal_lat, dezimal_lon;
    char temp[20];
    int altitude, i;   
    
    // making the time stamp
    // 0000160806 0000194137 0052153447 0008031685 0079NE
    // 0123456789 0123456789 0123456789 0123456789 012345    
    strcpy(gpx->datetime, "20");                                                 // In the 20th century
    gpx->datetime[2] = line[8];
    gpx->datetime[3] = line[9];
    gpx->datetime[4] = '-';
    gpx->datetime[5] = line[6];
    gpx->datetime[6] = line[7];
    gpx->datetime[7] = '-';
    gpx->datetime[8] = line[4];
    gpx->datetime[9] = line[5];
    gpx->datetime[10] = 'T';
    gpx->datetime[11] = line[14];
    gpx->datetime[12] = line[15];
    gpx->datetime[13] = ':';
    gpx->datetime[14] = line[16];
    gpx->datetime[15] = line[17];
    gpx->datetime[16] = ':';
    gpx->datetime[17] = line[18];
    gpx->datetime[18] = line[19];
    gpx->datetime[19] = 'Z';
    gpx->datetime[20] = '\0';
    #ifdef DEBUG
    cout << "datetime: " << gpx->datetime << endl;   
    #endif
    // *** copying the latitude numbers ***    
    // latitude 0052153407
    for (i = 0; i < 4; i++) {
        temp[i] = line[i + 20];
    }
    temp[4] = '\0';
    degree = atof(temp); 
    #ifdef DEBUG   
    cout << "latitude degree as text: " << temp << endl;
    #endif
    temp[0] = line[24];
    temp[1] = line[25];
    temp[2] = '.';
    for (i = 0; i < 5; i++) {
        temp[i + 3] = line[i + 26];
    }   
    temp[7] = '\0';
    #ifdef DEBUG   
    cout << "minutes as text: " << temp << endl;
    #endif
    minutes = atof(temp);    
    dezimal_lat = degree + minutes / 60;   
    // Southern latitude is negative
    if (line[44] == 'S')
        dezimal_lat = -dezimal_lat;
    #ifdef DEBUG
    cout << "degree: " << degree << " minutes: " << minutes << " latitude degree decimal: " << dezimal_lat << endl;
    #endif
    sprintf(gpx->latitude, "%.6f", dezimal_lat);
    #ifdef DEBUG
    cout << "Text: " << gpx->latitude << endl;
    #endif
    // longitude degree
    for (i = 0; i < 4; i++) {
        temp[i] = line[i + 30];
    }
    temp[4] = '\0';
    degree = atof(temp);    
    temp[0] = line[34];
    temp[1] = line[35];
    temp[2] = '.';
    for (i = 0; i < 4; i++) {
        temp[i + 3] = line[i + 36];
    }   
    temp[7] = '\0';
    minutes = atof(temp);    
    dezimal_lon = degree + minutes / 60;   
    // Western longitude degree is negative
    if (line[45] == 'W')
        dezimal_lat = -dezimal_lon;
    #ifdef DEBUG
    cout << "degree: " << degree << " Minutes: " << minutes << " Longitude degree decimal: " << dezimal_lon << endl;
    #endif
    sprintf(gpx->longitude, "%.6f", dezimal_lon);
    #ifdef DEBUG
    cout << "Text: " << gpx->longitude << endl;    
    #endif    
    // Altitude
    temp[0] = line[40];
    temp[1] = line[41];
    temp[2] = line[42];
    temp[3] = line[43];
    temp[4] = '\0';
    altitude = atoi(temp);
    #ifdef DEBUG
    cout << "Altitude: " << altitude << endl;
    #endif
    sprintf(gpx->altitude, "%d", altitude);
    #ifdef DEBUG
    cout << "Text: " << gpx->altitude << endl;    
    #endif  
}

void add_gpx_trkPoint_to_file(tGPXtrackPoint *gpx) {
    output << "<trkpt lat=\"" << gpx->latitude << "\" ";
    output << "lon=\"" << gpx->longitude << "\">\n";
    output << "<ele>" << gpx->altitude << "</ele>\n";
    output << "<time>" << gpx->datetime << "</time>\n";
    output << "</trkpt>\n";
}

void add_gpx_wayPoint_to_file(tGPXtrackPoint *gpx, char *pointname) {
    output << "<wpt lat=\"" << gpx->latitude << "\" ";
    output << "lon=\"" << gpx->longitude << "\">\n";
    output << "<name>" << pointname << "</name>\n";
    output << "<type>Waypoint</type>\n";
    output << "</wpt>\n";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -