This commit is contained in:
2024-03-26 15:42:29 +08:00
parent 49919f3191
commit 20a974886c
3 changed files with 105 additions and 106 deletions

172
EC800M.c
View File

@@ -1,9 +1,15 @@
#include "EC800M.h"
char GPSDATA[GPSDATA_LENGTH];
int GPSDATA_LEN;
// 重置数据缓冲区
void reset_data()
{
memset(GPSDATA, 0, GPSDATA_LENGTH);
GPSDATA_LEN = 0;
}
void uart_send_string(const char *str)
{
size_t len = strlen(str);
@@ -26,8 +32,10 @@ void uart_read_string()
void uart_read_string_until_response(const char *expected_response)
{
reset_data();
int response_len = strlen(expected_response);
char response_buffer[1024]; // 假设响应不会超过 100 个字符
char response_buffer[270]; // 假设响应不会超过 270 个字符
int i = 0;
while (1) {
@@ -35,6 +43,7 @@ void uart_read_string_until_response(const char *expected_response)
char c = uart_getc(GPS_UART);
GPSDATA[i++] = c;
GPSDATA[i] = '\0'; // 添加字符串结束符
if (strstr(GPSDATA, expected_response) != NULL) {
return; // 找到了预期的响应,退出循环
}
@@ -43,22 +52,80 @@ void uart_read_string_until_response(const char *expected_response)
}
}
// 重置数据缓冲区
void reset_data()
// GPS 经纬度转10进制
long double convertToDecimal(long double coordinate)
{
memset(GPSDATA, 0, GPSDATA_LENGTH);
GPSDATA_LEN = 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);
GPS_DATA_[p1 - p - 2 + 1] = '\0';
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;
}
// 初始化EC800M串口
int EC800M_INIT()
{
uart_init(GPS_UART, 2400);
gpio_set_function(9, GPIO_FUNC_UART); // UART1_TX
gpio_set_function(8, GPIO_FUNC_UART); // UART1_RX
gpio_set_function(GPS_UART_TX, GPIO_FUNC_UART); // UART1_TX
gpio_set_function(GPS_UART_RX, GPIO_FUNC_UART); // UART1_RX
int __unused actual = uart_set_baudrate(GPS_UART, GPS_UART_BAUD_RATE);
uart_set_hw_flow(GPS_UART, false, false);
uart_set_format(GPS_UART, 8, 1, UART_PARITY_NONE);
uart_set_format(GPS_UART, GPS_DATA_BITS, GPS_STOP_BITS, UART_PARITY_NONE);
//uart_set_fifo_enabled(GPS_UART, false);
uart_send_string("AT+QGPSCFG=\"outport\",\"uartdebug\"\r\n");
@@ -95,81 +162,32 @@ int EC800M_INIT()
return 0;
}
// 读取GPS信息
char *EC800M()
void EC800M(void *p)
{
uart_send_string("AT+QGPSLOC=0\r\n");
uart_read_string_until_response("OK");
sleep_ms(100);
//printf("%s %d\n", GPSDATA, strlen(GPSDATA));
printf("EC800M\n");
//printf("\r\n");
EC800M_INIT();
GPS_ gps_;
memset(gps_.time, 0, 10);
memset(gps_.N, 0, 20);
memset(gps_.E, 0, 20);
return GPSDATA;
}
while (1) {
// GPS 经纬度转10进制
long double convertToDecimal(long double coordinate)
{
int degree = (int)(coordinate / 100);
long double minutes = coordinate - degree * 100;
return degree + minutes / 60;
}
uart_send_string("AT+QGPSLOC=0\r\n");
uart_read_string_until_response("OK");
sleep_ms(300);
printf("%s %d\n", GPSDATA, strlen(GPSDATA));
// 解释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_);
EC800M_GPS_DATA_PARSING(GPSDATA, &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);
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);
}
printf("\r\n");
token = strtok(NULL, ",");
i++;
watchdog_update(); // 喂狗
sleep_ms(3000);
}
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 ;
return;
}

View File

@@ -7,7 +7,14 @@
#include "pico/stdlib.h"
#include "hardware/uart.h"
#include "hardware/clocks.h"
#include "hardware/watchdog.h"
#define GPS_UART uart1
#define GPS_UART_TX 9 // 接EC800M TX 上
#define GPS_UART_RX 8 // 接EC800M RX 上
#define GPS_DATA_BITS 8
#define GPS_STOP_BITS 1
#define GPS_UART_BAUD_RATE 115200
#define GPSDATA_LENGTH 8192
@@ -17,11 +24,6 @@ typedef struct GPS_ {
char E[20];
} GPS_;
extern char GPSDATA[GPSDATA_LENGTH];
extern int GPSDATA_LEN;
extern int EC800M_INIT();
extern char *EC800M();
extern void EC800M_GPS_DATA_PARSING(char *GPS_DATA, GPS_ * gps_);
extern void EC800M(void *p);
#endif

23
main.c
View File

@@ -15,28 +15,7 @@ int main(void)
watchdog_enable(8300, 1); // 8秒检测是否重新加载看门狗计数器. (不更新计数器则重启硬件, 最高8秒)
watchdog_start_tick(12);
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());
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);
}
EC800M(NULL);
return 0;
}