65,187
社区成员




gprmc
本工具从NMEA文件里读取GPRMC的GPS时间戳 BY Hong Wenjun
Usage: gprmc.exe [NMEA.txt] [gps.txt] [8402]
输出文件不填,结果显示在屏幕上
* WGS-84 和 GCJ-02 的 EMEA 打印转换第三个参数 code 定义
* 02 表示 GCJ-02;
* 84 表示 WGS-84;
*
* code 定义 默认 8402
* 8484: 输入WGS-84 度分格式, 输出WGS-84 度小数
* 8402: 输入WGS-84 度分格式, 输出GCJ-02 火星坐标
*
* 0202: 输入GCJ-02 火星坐标, 输出GCJ-02 火星坐标
* 0284: 输入GCJ-02 火星坐标, 输出WGS-84 度小数
昨天刚写的读取一行解析,数据格式行尾都有校验码的
// 读取一行GPRMC数据,判断是否是GPRMC数据行
bool read_gprmc(const char* line, gprmc_format& rmc)
{
char line_buf[BUFSIZ];
strncpy(line_buf, line, BUFSIZ);
int chksum = 0;
if ('$' != line_buf[0]) {
print_error(1);
return false;;
} else {
char* pch = strrchr(line_buf, '*');
if (pch != NULL) {
*pch = '\0';
rmc.mode = *(pch - 1);
pch++;
rmc.chksum = strtol(pch, &pch, 16);
// printf("%X\n", chksum);
if (rmc.chksum != checksum(line_buf + 1)) {
print_error(2);
return false;;
}
} else {
print_error(3);
return false;;
}
if (strstr(line_buf, ",,"))
multi_replace(line_buf, ",,", ",|,");
pch = strtok(line_buf, ",");
if ((pch != NULL) && !strcmp(pch, "$GPRMC")) {
// printf("%s\n", pch); //GPRMC
pch = strtok(NULL, ",");
rmc.rcv_time = atof(pch);
pch = strtok(NULL, ",");
rmc.status = *pch;
pch = strtok(NULL, ",");
rmc.lat = atof(pch);
pch = strtok(NULL, ",");
rmc.lat_direct = *pch;
pch = strtok(NULL, ",");
rmc.lon = atof(pch);
pch = strtok(NULL, ",");
rmc.lon_direct = *pch;
pch = strtok(NULL, ",");
rmc.speed = atof(pch);
pch = strtok(NULL, ",");
rmc.cog = atof(pch);
pch = strtok(NULL, ",");
rmc.date = atoi(pch);
// 一般空
rmc.mag_variation = 0;
rmc.mag_var_direct = 'W';
rmc.mode = rmc.mode; // 之前已经读到
} else {
print_error(4);
return false;;
}
}
return true;
}
// 读取一行GPRMC数据,判断是否是GPRMC数据行
bool read_gprmc(const char* line, gprmc_format& rmc)
{
char line_buf[BUFSIZ];
strncpy(line_buf, line, BUFSIZ);
int chksum = 0;
if ('$' != line_buf[0]) {
print_error(1);
return false;;
} else {
char* pch = strrchr(line_buf, '*');
if (pch != NULL) {
*pch = '\0';
rmc.mode = *(pch - 1);
pch++;
rmc.chksum = strtol(pch, &pch, 16);
// printf("%X\n", chksum);
if (rmc.chksum != checksum(line_buf + 1)) {
print_error(2);
return false;;
}
} else {
print_error(3);
return false;;
}
if (strstr(line_buf, ",,"))
multi_replace(line_buf, ",,", ",|,");
pch = strtok(line_buf, ",");
if ((pch != NULL) && !strcmp(pch, "$GPRMC")) {
// printf("%s\n", pch); //GPRMC
pch = strtok(NULL, ",");
rmc.rcv_time = atof(pch);
pch = strtok(NULL, ",");
rmc.status = *pch;
pch = strtok(NULL, ",");
rmc.lat = atof(pch);
pch = strtok(NULL, ",");
rmc.lat_direct = *pch;
pch = strtok(NULL, ",");
rmc.lon = atof(pch);
pch = strtok(NULL, ",");
rmc.lon_direct = *pch;
pch = strtok(NULL, ",");
rmc.speed = atof(pch);
pch = strtok(NULL, ",");
rmc.cog = atof(pch);
pch = strtok(NULL, ",");
rmc.date = atoi(pch);
// 一般空
rmc.mag_variation = 0;
rmc.mag_var_direct = 'W';
rmc.mode = rmc.mode; // 之前已经读到
} else {
print_error(4);
return false;;
}
}
return true;
}
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
ifstream in;
in.open("srcdata.txt", ios::in);
char cTemp;
string strNumber;
int nNumber = -1;
while (in.get(cTemp))
{
if (isdigit(cTemp))
{
strNumber += cTemp;
}
else
{
// Custom handle here yourself.
if (cTemp == '#')
{
}
else if (cTemp == '\\')
{
}
// Trans string to number value if string not empty.
if (strNumber.length())
{
nNumber = atoi(strNumber.c_str());
cout << "nNumber = " << nNumber << endl;
nNumber = -1;
strNumber.clear();
}
}
}
if (strNumber.length())
{
nNumber = atoi(strNumber.c_str());
cout << "nNumber = " << nNumber << endl;
}
in.close();
return 0;
}