diff --git a/EC800M.c b/EC800M.c index 54b3296..12d8563 100644 --- a/EC800M.c +++ b/EC800M.c @@ -1,5 +1,6 @@ #include "EC800M.h" + char GPSDATA[GPSDATA_LENGTH]; int GPSDATA_LEN; @@ -49,6 +50,7 @@ void reset_data() GPSDATA_LEN = 0; } +// 初始化EC800M串口 int EC800M_INIT() { uart_init(GPS_UART, 2400); @@ -93,6 +95,7 @@ int EC800M_INIT() return 0; } +// 读取GPS信息 char *EC800M() { uart_send_string("AT+QGPSLOC=0\r\n"); @@ -105,12 +108,68 @@ char *EC800M() return GPSDATA; } -char *EC800M_GPS_DATA_PARSING(char *GPS_DATA) +// GPS 经纬度转10进制 +long double convertToDecimal(long double coordinate) { - char *p = NULL; - p = strstr(GPSDATA, "QGPSLOC:"); - - printf("%s\n", p); - - return 0; + int degree = (int)(coordinate / 100); + long double minutes = coordinate - degree * 100; + return degree + minutes / 60; } + +// 解释GPS数据 +void EC800M_GPS_DATA_PARSING(char *GPS_DATA, GPS_ * gps_) +{ + int i = 1; + char *p = strchr(GPS_DATA, ':'); + char *p1 = strstr(p + 2, "\r\n"); + char GPS_DATA_[p1 - p]; + memset(gps_->time, 0, 10); + memset(gps_->N, 0, 20); + memset(gps_->E, 0, 20); + memset(GPS_DATA_, 0, p1 - p); + memcpy(GPS_DATA_, p + 2, p1 - p - 2 + 1); + printf("%s\n", GPS_DATA_); + + char *token = strtok(GPS_DATA_, ","); + while (token != NULL) { + if (i == 1) { + strcpy(gps_->time, token); + } + if (i == 2) { + strcpy(gps_->N, token); + } + if (i == 3) { + strcpy(gps_->E, token); + } + + token = strtok(NULL, ","); + i++; + } + + gps_->N[strlen(gps_->N) - 1] = '\0'; + gps_->E[strlen(gps_->E) - 1] = '\0'; + + long double N,E; + N=atof(gps_->N); + E=atof(gps_->E); + //printf("GPS : %.9lf, %.9lf\n", N, E); + //printf("GPS : %.6lf, %.6lf\n", convertToDecimal(N), convertToDecimal(E)); + + // 095921.00 + char hours[3] = { 0 }; + char minutes[3] = { 0 }; + char seconds[9] = { 0 }; + char *p2 = gps_->time; + memcpy(hours, p2, 2); + memcpy(minutes, p2 + 2, 2); + memcpy(seconds, p2 + 4, 2); + //printf("GPS 修正时间: %d:%s:%s\n", atoi(hours) + 8, minutes, seconds); + + sprintf(gps_->time, "%d:%s:%s", atoi(hours) + 8, minutes, seconds); + sprintf(gps_->N, "%.6lf", convertToDecimal(atof(gps_->N))); + sprintf(gps_->E, "%.6lf", convertToDecimal(atof(gps_->E))); + + return ; +} + + diff --git a/EC800M.h b/EC800M.h index ec27759..8f365a5 100644 --- a/EC800M.h +++ b/EC800M.h @@ -2,6 +2,7 @@ #define EC800M_H #include +#include #include #include "pico/stdlib.h" #include "hardware/uart.h" @@ -10,12 +11,17 @@ #define GPS_UART_BAUD_RATE 115200 #define GPSDATA_LENGTH 8192 +typedef struct GPS_ { + char time[10]; + char N[20]; + char E[20]; +} GPS_; + extern char GPSDATA[GPSDATA_LENGTH]; extern int GPSDATA_LEN; extern int EC800M_INIT(); extern char *EC800M(); -extern char *EC800M_GPS_DATA_PARSING(char *GPS_DATA); -extern void reset_data(); +extern void EC800M_GPS_DATA_PARSING(char *GPS_DATA, GPS_ * gps_); #endif diff --git a/main.c b/main.c index b568c24..20071db 100644 --- a/main.c +++ b/main.c @@ -1,66 +1,6 @@ #include "main.h" #include "EC800M.h" -typedef struct GPS_ { - char time[10]; - char N[20]; - char E[20]; -} GPS_; - -float convertToDecimal(float coordinate) -{ - int degree = (int)(coordinate / 100); - float minutes = coordinate - degree * 100; - return degree + minutes / 60; -} - -void Data_parsing(char *gps_data, GPS_ * gps_) -{ - char *p = strchr(gps_data, ':'); - char *p1 = strstr(p + 2, "\r\n"); - char GPS_DATA_[p1 - p]; - - memset(GPS_DATA_, 0, p1 - p); - memcpy(GPS_DATA_, p + 2, p1 - p - 2 + 1); - //printf("%s\n", GPS_DATA_); - - char *token = strtok(GPS_DATA_, ","); - int i = 1; - while (token != NULL) { - if (i == 1) { - strcpy(gps_->time, token); - } - if (i == 2) { - strcpy(gps_->N, token); - } - if (i == 3) { - strcpy(gps_->E, token); - } - - token = strtok(NULL, ","); - i++; - } - - gps_->N[strlen(gps_->N) - 1] = '\0'; - gps_->E[strlen(gps_->E) - 1] = '\0'; - //printf("GPS : %.6f, %.6f\n", convertToDecimal(atof(gps_->N)), convertToDecimal(atof(gps_->E))); - - // 095921.00 - char hours[3] = { 0 }; - char minutes[3] = { 0 }; - char seconds[9] = { 0 }; - char *p2 = gps_->time; - memcpy(hours, p2, 2); - memcpy(minutes, p2 + 2, 2); - memcpy(seconds, p2 + 4, 2); - //printf("GPS 修正时间: %d:%s:%s\n", atoi(hours) + 8, minutes, seconds); - - sprintf(gps_->time, "%d:%s:%s", atoi(hours) + 8, minutes, seconds); - sprintf(gps_->N, "%.6f", convertToDecimal(atof(gps_->N))); - sprintf(gps_->E, "%.6f", convertToDecimal(atof(gps_->E))); - -} - int main(void) { stdio_init_all(); @@ -75,17 +15,24 @@ int main(void) watchdog_enable(8300, 1); // 8秒检测是否重新加载看门狗计数器. (不更新计数器则重启硬件, 最高8秒) watchdog_start_tick(12); - printf("EC800M\n"); - EC800M_INIT(); - char gps_data[1024] = { 0 }; + printf("EC800M\n"); + static char GPS_DATA[1024] = { 0 }; + EC800M_INIT(); + GPS_ gps_; + memset(gps_.time, 0, 10); + memset(gps_.N, 0, 20); + memset(gps_.E, 0, 20); + while (1) { watchdog_update(); // 喂狗 - strcpy(gps_data, EC800M()); - Data_parsing(gps_data, &gps_); + strcpy(GPS_DATA, EC800M()); + EC800M_GPS_DATA_PARSING(GPS_DATA, &gps_); printf(" T: %s\n N: %s\n E: %s\n", gps_.time, gps_.N, gps_.E); + printf(" NE: %s, %s\n", gps_.N, gps_.E); + printf("\r\n"); sleep_ms(3000);