SIM900A高效完整的STM32代码

一、问题

最近使用SIM900A做一个STM32的项目,用的正点原子的代码,单独对demo测试没有问题,但是当集成到整个项目中时,效果就非常不理想,主要问题:

1、后台服务器接收到的数据时而密集,时而分散,即并不是按照代码中的设计来呈现
2、会有卡死的现象
3、效率低,有时候连接上服务器,但就是没有数据,实际上板子已经发送出来数据

介于以上问题,查看了众多的手册(22篇GPRS无线模块SIM900软硬件设计技术资料合集),并进过多次测试,最终运行效果很好:
最终效果

二、代码

1,正点原子的demo代码 (不建议使用)

void Landing_Init(int id,int num,char *jiaoben, u8 *humiture_buff )
{
   
	u8 ch = 0x1A;//结束输入,发送数据
	int i=1000;
	
	if(sim900a_send_cmd("AT", "OK", 500)) return;		//模块有效性检查,无效时候就不进行联网配置
	USART2_RX_STA = 0;
	sim900a_send_cmd("AT+CIPCLOSE=1", "CLOSE OK", 500);	

	sim900a_send_cmd("AT+CGCLASS=\"B\"", "OK", 500);
	USART2_RX_STA = 0;
	sim900a_send_cmd("AT+CGDCONT=1,\"IP\",\"CMNET\"", "OK", 500);
	USART2_RX_STA = 0;
	sim900a_send_cmd("AT+CGATT=1", "OK", 500);
	USART2_RX_STA = 0;		
	sim900a_send_cmd("AT+CIPCSGP=1,\"CMNET\"", "OK", 500);
	USART2_RX_STA = 0;
	sim900a_send_cmd("AT+CLPORT=\"TCP\",\"2002\"", "OK",500);
	USART2_RX_STA = 0;
	if(sim900a_send_cmd("AT+CIPSTART=\"TCP\",\"194.233.101.180\",\"7890\"", "OK", 500)) //服务端不在线时候则重新建立发送请求
	{
   
		sim900a_send_cmd("AT+CIPCLOSE=1", "CLOSE OK", 500);
		return;
	}
	
	USA
  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是一个简单的 sim900A 模块使用 STM32代码示例,可以作为参考: ```c #include "stm32f10x.h" #include "stdio.h" #include "string.h" #define SIM900A_USART USART1 #define SIM900A_TX_PIN GPIO_Pin_9 #define SIM900A_RX_PIN GPIO_Pin_10 #define SIM900A_USART_GPIO GPIOA #define SIM900A_USART_GPIO_CLK RCC_APB2Periph_GPIOA #define SIM900A_PWR_PIN GPIO_Pin_11 #define SIM900A_RST_PIN GPIO_Pin_12 #define SIM900A_CTRL_GPIO GPIOB #define SIM900A_CTRL_GPIO_CLK RCC_APB2Periph_GPIOB #define SIM900A_BUFFER_SIZE 1024 #define SIM900A_TIMEOUT 1000 static uint8_t sim900a_buffer[SIM900A_BUFFER_SIZE]; static uint16_t sim900a_buffer_index = 0; static uint8_t sim900a_rx_flag = 0; void sim900a_init(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | SIM900A_USART_GPIO_CLK | SIM900A_CTRL_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = SIM900A_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SIM900A_USART_GPIO, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SIM900A_RX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(SIM900A_USART_GPIO, &GPIO_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(SIM900A_USART, &USART_InitStructure); USART_Cmd(SIM900A_USART, ENABLE); GPIO_InitStructure.GPIO_Pin = SIM900A_PWR_PIN | SIM900A_RST_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SIM900A_CTRL_GPIO, &GPIO_InitStructure); GPIO_SetBits(SIM900A_CTRL_GPIO, SIM900A_PWR_PIN); GPIO_SetBits(SIM900A_CTRL_GPIO, SIM900A_RST_PIN); } void sim900a_send_byte(uint8_t data) { USART_SendData(SIM900A_USART, data); while (USART_GetFlagStatus(SIM900A_USART, USART_FLAG_TXE) == RESET); } void sim900a_send_string(char *str) { while (*str) { sim900a_send_byte(*str++); } } uint8_t sim900a_receive_byte(void) { while (USART_GetFlagStatus(SIM900A_USART, USART_FLAG_RXNE) == RESET); return USART_ReceiveData(SIM900A_USART); } void sim900a_receive_data(void) { uint8_t data; while (USART_GetFlagStatus(SIM900A_USART, USART_FLAG_RXNE) != RESET) { data = sim900a_receive_byte(); if (sim900a_buffer_index < SIM900A_BUFFER_SIZE) { sim900a_buffer[sim900a_buffer_index++] = data; if (data == '\n' || data == '\r') { sim900a_rx_flag = 1; } } else { sim900a_buffer_index = 0; } } } uint8_t sim900a_send_command(const char *command, const char *response, uint16_t timeout) { uint16_t i; sim900a_buffer_index = 0; sim900a_rx_flag = 0; sim900a_send_string((char *)command); sim900a_send_string("\r\n"); for (i = 0; i < timeout; i++) { sim900a_receive_data(); if (sim900a_rx_flag) { if (strstr((char *)sim900a_buffer, response)) { return 1; } sim900a_rx_flag = 0; sim900a_buffer_index = 0; } delay_ms(1); } return 0; } int main(void) { sim900a_init(); while (1) { if (sim900a_send_command("AT", "OK", SIM900A_TIMEOUT)) { printf("SIM900A is ready.\n"); } else { printf("SIM900A is not responding.\n"); } delay_ms(1000); } } ``` 需要注意的是,以上代码仅作为示例,实际使用时需要根据具体的硬件连接和需求进行修改。同时,使用 sim900A 模块需要注意其电源、复位等控制引脚的使用方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值