STM32标准库怎么把指针变量 "lat" 和 "lon"中的经纬度值用SIM800C发送给用户手机的代码
时间: 2023-07-07 10:27:24 浏览: 67
以下是一个示例代码,可以使用STM32的标准库和SIM800C模块将经纬度数据发送给用户手机。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "stm32f10x.h"
#define PI 3.14159265358979323846
float degree2rad(float degree) {
return degree * PI / 180.0;
}
float rad2degree(float rad) {
return rad * 180.0 / PI;
}
void NMEA_GNRMC_Parse(char* buffer, float* lat, float* lon) {
char* token;
char* delimiter = ",";
int i = 0;
char lat_dir, lon_dir;
float lat_min, lon_min;
float lat_deg, lon_deg;
float lat_sec, lon_sec;
float speed, course;
char fix;
char timestamp[10];
token = strtok(buffer, delimiter);
while (token != NULL) {
switch (i) {
case 1:
strncpy(timestamp, token, 10);
break;
case 2:
fix = token[0];
break;
case 3:
lat_deg = atof(token);
break;
case 4:
lat_min = atof(token);
break;
case 5:
lat_dir = token[0];
break;
case 6:
lon_deg = atof(token);
break;
case 7:
lon_min = atof(token);
break;
case 8:
lon_dir = token[0];
break;
case 9:
speed = atof(token);
break;
case 10:
course = atof(token);
break;
}
token = strtok(NULL, delimiter);
i++;
}
// Convert latitude and longitude to decimal degrees
lat_sec = (lat_min - floor(lat_min)) * 60.0;
lat_min = floor(lat_min);
lat_deg = lat_deg + lat_min / 60.0 + lat_sec / 3600.0;
if (lat_dir == 'S') {
lat_deg = -lat_deg;
}
lon_sec = (lon_min - floor(lon_min)) * 60.0;
lon_min = floor(lon_min);
lon_deg = lon_deg + lon_min / 60.0 + lon_sec / 3600.0;
if (lon_dir == 'W') {
lon_deg = -lon_deg;
}
*lat = lat_deg;
*lon = lon_deg;
}
void SIM800C_SendSMS(char* phone_number, char* message) {
char buffer[100];
int len;
// Set the SMS message format to text mode
sprintf(buffer, "AT+CMGF=1\r\n");
len = strlen(buffer);
USART_SendData(USART1, (uint16_t) buffer);
while (len--) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, (uint16_t) *buffer++);
}
// Set the phone number to send the SMS to
sprintf(buffer, "AT+CMGS=\"%s\"\r\n", phone_number);
len = strlen(buffer);
USART_SendData(USART1, (uint16_t) buffer);
while (len--) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, (uint16_t) *buffer++);
}
// Send the SMS message
sprintf(buffer, "%s\r\n", message);
len = strlen(buffer);
USART_SendData(USART1, (uint16_t) buffer);
while (len--) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, (uint16_t) *buffer++);
}
// Send the CTRL+Z character to indicate the end of the message
sprintf(buffer, "%c", 0x1A);
len = strlen(buffer);
USART_SendData(USART1, (uint16_t) buffer);
while (len--) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, (uint16_t) *buffer++);
}
}
int main(void) {
float lat, lon;
char buffer[100] = "$GNRMC,170834.00,A,3723.46593,N,12202.21208,W,0.003,,130821,,,A*6D\r\n";
char phone_number[20] = "123456789";
char message[100];
NMEA_GNRMC_Parse(buffer, &lat, &lon);
// Create the SMS message
sprintf(message, "Latitude: %f\nLongitude: %f", lat, lon);
// Send the SMS message
SIM800C_SendSMS(phone_number, message);
return 0;
}
```
这个代码使用了一个名为 "SIM800C_SendSMS" 的函数来发送短信。它会将短信内容和接收方手机号码作为参数,并使用USART将命令发送给SIM800C模块。
在主函数中,我们将解析出的经纬度值转换为字符串,并将其作为短信内容发送给用户手机。需要注意的是,由于SIM800C模块使用的是ASCII码,因此我们需要使用sprintf函数将浮点数转换为字符串。