stm32f103c8t6串口2发送AT指令给EC01g连接阿里云,并发送AT+LOCATION=1指令给EC01g,得到GPS,解析GPS数据,stm32库函数版本的代码
时间: 2023-12-06 16:39:36 浏览: 115
以下是基于STM32库函数版本的代码,实现串口2发送AT指令给EC01g连接阿里云,并发送AT+LOCATION=1指令给EC01g,解析GPS数据。需要注意的是,该代码仅供参考,具体实现还需要根据具体的硬件和需求进行调整。
```c
#include "stm32f10x.h"
#define USART2_Buffer_Size 256
uint8_t USART2_RxBuffer[USART2_Buffer_Size];
uint8_t USART2_TxBuffer[USART2_Buffer_Size];
uint8_t USART2_RxCounter = 0;
void USART2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//配置USART2_TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置USART2_RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
uint8_t data = USART_ReceiveData(USART2);
if (data == '\n')
{
USART2_RxBuffer[USART2_RxCounter] = '\0';
USART2_RxCounter = 0;
}
else
{
USART2_RxBuffer[USART2_RxCounter++] = data;
if (USART2_RxCounter >= USART2_Buffer_Size)
{
USART2_RxCounter = 0;
}
}
}
}
void USART2_SendString(char* str)
{
while (*str)
{
USART_SendData(USART2, *str++);
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}
}
void USART2_SendATCommand(char* cmd)
{
USART2_SendString("AT+");
USART2_SendString(cmd);
USART2_SendString("\r\n");
}
void USART2_SendATLocationCommand(void)
{
USART2_SendATCommand("LOCATION=1");
}
float parseGpsData(char* gpsData)
{
char* token = strtok(gpsData, ",");
float latitude = atof(token);
token = strtok(NULL, ",");
float longitude = atof(token);
return latitude;
}
int main(void)
{
USART2_Init();
while (1)
{
USART2_SendATCommand("CGATT=1"); // 连接GPRS网络
USART2_SendATCommand("CSTT=\"CMNET\""); // 设置APN
USART2_SendATCommand("CIICR"); // 启动GPRS连接
USART2_SendATCommand("CIFSR"); // 查询IP地址
USART2_SendATCommand("CIPSTART=\"TCP\",\"mqtt-cn-shanghai.aliyuncs.com\",1883"); // 连接阿里云MQTT服务器
USART2_SendATCommand("CIPSEND"); // 进入发送模式
USART2_SendString("GET / HTTP/1.1\r\nHost: mqtt-cn-shanghai.aliyuncs.com\r\nConnection: close\r\n\r\n"); // 发送HTTP请求
USART2_SendString("\r\n");
USART2_SendATLocationCommand(); // 发送获取GPS数据的AT指令
// 等待接收到GPS数据
while (1)
{
if (strstr(USART2_RxBuffer, "+LOCATION:") != NULL)
{
float latitude = parseGpsData(USART2_RxBuffer);
// 处理收到的GPS数据
break;
}
}
}
}
```
阅读全文