Files
EC800M/main.c
2024-03-25 15:05:38 +08:00

86 lines
1.9 KiB
C

#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();
sleep_ms(1000);
printf("EC800M\n");
EC800M_INIT();
char gps_data[1024] = { 0 };
GPS_ gps_;
while (1) {
strcpy(gps_data, EC800M());
Data_parsing(gps_data, &gps_);
printf(" T: %s\n N: %s\n E: %s\n", gps_.time, gps_.N, gps_.E);
printf("\r\n");
sleep_ms(3000);
}
return 0;
}