STM32发送AT控制NB模块的代码
时间: 2024-04-30 16:18:14 浏览: 133
NB-IOT模组驱动代码 STM32
3星 · 编辑精心推荐
以下是基于HAL库的STM32发送AT指令控制NB模块的示例代码:
```c
#include "stdio.h"
#include "string.h"
#include "stm32f1xx_hal.h"
#define NB_UART huart1 // NB模块使用的UART
#define NB_BUFFER_SIZE 128 // NB模块通信缓冲区大小
UART_HandleTypeDef NB_UART;
uint8_t nb_buffer[NB_BUFFER_SIZE];
void nb_send_cmd(char* cmd) {
HAL_UART_Transmit(&NB_UART, (uint8_t*)cmd, strlen(cmd), HAL_MAX_DELAY);
}
void nb_send_data(uint8_t* data, uint16_t len) {
HAL_UART_Transmit(&NB_UART, data, len, HAL_MAX_DELAY);
}
void nb_receive_data(uint8_t* data, uint16_t len) {
HAL_UART_Receive(&NB_UART, data, len, HAL_MAX_DELAY);
}
void nb_reset(void) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
}
int nb_init(void) {
nb_reset();
nb_send_cmd("AT\r\n");
nb_receive_data(nb_buffer, NB_BUFFER_SIZE); // 接收AT响应
if (strstr((char*)nb_buffer, "OK") == NULL) {
return -1;
}
nb_send_cmd("AT+CGATT=1\r\n"); // 激活PS网络
nb_receive_data(nb_buffer, NB_BUFFER_SIZE); // 接收AT响应
if (strstr((char*)nb_buffer, "OK") == NULL) {
return -1;
}
nb_send_cmd("AT+CGDCONT=1,\"IP\",\"cmnet\"\r\n"); // 设置APN
nb_receive_data(nb_buffer, NB_BUFFER_SIZE); // 接收AT响应
if (strstr((char*)nb_buffer, "OK") == NULL) {
return -1;
}
nb_send_cmd("AT+CNMP=38\r\n"); // 设置NB-IoT网络
nb_receive_data(nb_buffer, NB_BUFFER_SIZE); // 接收AT响应
if (strstr((char*)nb_buffer, "OK") == NULL) {
return -1;
}
nb_send_cmd("AT+CMNB=1\r\n"); // 设置NB模式
nb_receive_data(nb_buffer, NB_BUFFER_SIZE); // 接收AT响应
if (strstr((char*)nb_buffer, "OK") == NULL) {
return -1;
}
return 0;
}
int nb_connect(char* host, int port) {
char cmd[32];
sprintf(cmd, "AT+QNACT=1\r\n");
nb_send_cmd(cmd); // 激活PDP上下文
nb_receive_data(nb_buffer, NB_BUFFER_SIZE); // 接收AT响应
if (strstr((char*)nb_buffer, "OK") == NULL) {
return -1;
}
sprintf(cmd, "AT+QIOPEN=1,0,\"TCP\",\"%s\",%d,0,1\r\n", host, port);
nb_send_cmd(cmd); // 建立TCP连接
nb_receive_data(nb_buffer, NB_BUFFER_SIZE); // 接收AT响应
if (strstr((char*)nb_buffer, "OK") == NULL) {
return -1;
}
return 0;
}
void nb_disconnect(void) {
nb_send_cmd("AT+QICLOSE=0\r\n"); // 关闭TCP连接
nb_receive_data(nb_buffer, NB_BUFFER_SIZE); // 接收AT响应
nb_send_cmd("AT+QNACT=0\r\n"); // 停止PDP上下文
nb_receive_data(nb_buffer, NB_BUFFER_SIZE); // 接收AT响应
}
int nb_send(char* data) {
char cmd[32];
int len = strlen(data);
sprintf(cmd, "AT+QISEND=0,%d\r\n", len);
nb_send_cmd(cmd); // 准备发送数据
nb_receive_data(nb_buffer, NB_BUFFER_SIZE); // 接收AT响应
if (strstr((char*)nb_buffer, ">") == NULL) {
return -1;
}
nb_send_data((uint8_t*)data, len); // 发送数据
nb_receive_data(nb_buffer, NB_BUFFER_SIZE); // 接收AT响应
if (strstr((char*)nb_buffer, "SEND OK") == NULL) {
return -1;
}
return 0;
}
int main(void) {
HAL_Init();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_USART1_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
NB_UART.Instance = USART1;
NB_UART.Init.BaudRate = 115200;
NB_UART.Init.WordLength = UART_WORDLENGTH_8B;
NB_UART.Init.StopBits = UART_STOPBITS_1;
NB_UART.Init.Parity = UART_PARITY_NONE;
NB_UART.Init.Mode = UART_MODE_TX_RX;
NB_UART.Init.HwFlowCtl = UART_HWCONTROL_NONE;
NB_UART.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&NB_UART);
nb_init(); // 初始化NB模块
nb_connect("www.example.com", 80); // 连接服务器
nb_send("Hello, World!"); // 发送数据
nb_disconnect(); // 断开连接
while (1) {
// 你的代码
}
}
```
注意:以上代码仅供参考,实际使用时需要根据NB模块和STM32的具体型号进行相应的修改。
阅读全文