hal库stm32f103与Sim900A的信息发送
- 电脑调试
开始Sim900A与stm32通信之前,我们先使用电脑调试,验证Sim900A能够正常工作。
Sim900A与usb-ttl连接:
现在我们可以开始调试了:
1.模块开机后,推荐发送 AT 这条指令,将 SIM900A 模块和电脑的波特率同步一下。
首先将 AT 写入字符串输入框,然后点击发送。模块波特率默认是 9600。发送 AT 这条指令,SIM900A 模块返回 OK,说明此条指令执行正确,同时说明:电脑和模块通信正常。
2.发送 AT+CSQ 查询信号强度指令。模块返回如下图所示其中 19 为信号强度值,此
值在 0—31 之间,越大代表信号越强。0 代表无信号,一般此数要达到 10 以上,模块才能正确执行发短信等功能。
3.发送 AT+CPIN?查询模块是否检测到手机卡指令。READY 说明模块检测到了手机卡。如果返回 ERROR,说明模块没有检测到手机卡,请检查手机卡是否为 2G 卡,并且非电信卡。
4.发送 AT+COPS?指令,查询模块是否注册到网络。CHINA MOBILE 代表中国移动。
5.控制模块发送一条英文短信
发送英文短信指令包括:AT+CSCS=”GSM”,AT+CMGF=1,AT+CMGS=”****”(*表示电话号),十六进制下的 1A
-
stm32调试
1.通信逻辑
PC(串口助手) 发送消息---->stm32f1(usart1接收,usart2发送)---->Sim900A(返回数据)---->
stm32f1(usart2接收,usart1发送)---->PC(串口助手) 接收消息。2.硬件电路
5V_SIM900_ARXD ------USART2_TXD
5V_SIM900_ATXD ------USART2_RXD
注意:模块上的 VCC_MCU 管脚不是用来给模块供电的,此管脚是用来
控制模块的 TTL 电平的高电压的。V3.7 版本的供电接口在板子边上的白色端子。
其他版本的供电接口一般丝印标出 VCC_5 或 5V 等字样。即下图蓝色标记。
电源要求:5v,1A。如果电源当电源功率不足时,模块上的常亮的灯会闪烁起来,另一个闪烁灯可能常亮几秒钟然后又开始闪烁起来。建议额外供电,并且注意共地。
3.代码:
//mian.c
/* USER CODE BEGIN Includes */
#include <string.h>
#include "sim900a.h"
#define RXBUFFERSIZE 256 //最大接收字节数
char RxBuffer1[RXBUFFERSIZE]; //接收数据
char RxBuffer2[RXBUFFERSIZE]; //接收数据
uint8_t aRxBuffer1; //接收中断缓冲
uint8_t aRxBuffer2; //接收中断缓冲
uint8_t Uart1_Rx_Cnt = 0; //接收缓冲计数
uint8_t Uart2_Rx_Cnt = 0;
/* USER CODE END Includes */
-----------------------------------------------------------
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer1, 1);
HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer2, 1);
Sim900A_SendEnglishMsg("helloworld123!");
//中文需要先转为Unicode码
//Sim900A_SendChineseMsg("4E2D658753EF4EE55417006E0067006C006900730068");
/* USER CODE END 2 */
-----------------------------------------------------------
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(huart);
/* NOTE: This function Should not be modified, when the callback is needed,
the HAL_UART_TxCpltCallback could be implemented in the user file
*/
//usart1
if(huart->Instance == USART1){
if(Uart1_Rx_Cnt >= 255) //溢出判断
{
Uart1_Rx_Cnt = 0;
memset(RxBuffer1,0x00,sizeof(RxBuffer1));
HAL_UART_Transmit(&huart1, (uint8_t *)"数据溢出", 10,0xFFFF);
}
else
{
RxBuffer1[Uart1_Rx_Cnt++] = aRxBuffer1; //接收数据转存
if((RxBuffer1[Uart1_Rx_Cnt-1] == 0x0A)&&(RxBuffer1[Uart1_Rx_Cnt-2] == 0x0D)) //判断结束位
{
HAL_UART_Transmit(&huart2, (uint8_t *)&RxBuffer1, Uart1_Rx_Cnt,0xFFFF); //将收到的信息发送出去
while(HAL_UART_GetState(&huart2) == HAL_UART_STATE_BUSY_TX);//检测UART发送结束
Uart1_Rx_Cnt = 0;
memset(RxBuffer1,0x00,sizeof(RxBuffer1)); //清空数组
}
}
HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer1, 1); //再开启接收中断
}else if(huart->Instance == USART2){
//usart2
if(Uart2_Rx_Cnt >= 255) //溢出判断
{
Uart2_Rx_Cnt = 0;
memset(RxBuffer2,0x00,sizeof(RxBuffer2));
HAL_UART_Transmit(&huart1, (uint8_t *)"数据溢出", 10,0xFFFF);
}
else
{
RxBuffer2[Uart2_Rx_Cnt++] = aRxBuffer2; //接收数据转存
if((RxBuffer2[Uart2_Rx_Cnt-1] == 0x0A)&&(RxBuffer2[Uart2_Rx_Cnt-2] == 0x0D)) //判断结束位
{
HAL_UART_Transmit(&huart1, (uint8_t *)&RxBuffer2, Uart2_Rx_Cnt,0xFFFF); //将收到的信息发送出去
while(HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX);//检测UART发送结束
Uart2_Rx_Cnt = 0;
memset(RxBuffer2,0x00,sizeof(RxBuffer2)); //清空数组
}
}
HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer2, 1); //再开启接收中断
}
}
/* USER CODE END4 */
sim900a.h
#ifndef __SIM900A_H__
#define __SIM900A_H__
#include "stm32f1xx.h"
#include "usart.h"
void Sim900A_SendAtCmd(char* pCmd);//发送指令
void Sim900A_SendEnglishMsg(char* pMsg);//发送一条英文消息
void Sim900A_SendChineseMsg(char* pMsg);//发送一条中文消息
#endif
sim900a.c
#include "sim900a.h"
extern uint8_t Uart2_Rx_Cnt;
extern char RxBuffer2[256];
uint8_t ByteSend=0x1A;
//发送指令
void Sim900A_SendAtCmd(char* pCmd){
//uint16_t cnt = 0;
char c;
while(*pCmd!='\0'){
//cnt++;
c = *pCmd;
HAL_UART_Transmit(&huart2, (uint8_t *)&c, 1,0xFFFF);
pCmd++;
}
}
//发送英文消息
void Sim900A_SendEnglishMsg(char* Msg){
/*
AT+CSCS="GSM"
AT+CMGF=1
AT+CMGS="******"//电话号码
*/
//AT
Sim900A_SendAtCmd("AT+CSCS=\"GSM\"\r\n");
HAL_Delay(1000);
Sim900A_SendAtCmd("AT+CMGF=1\r\n");
HAL_Delay(1000);
Sim900A_SendAtCmd("AT+CSCA?\r\n");
HAL_Delay(1000);
Sim900A_SendAtCmd("AT+CSMP=17,167,0,241\r\n");
HAL_Delay(1000);
Sim900A_SendAtCmd("AT+CMGS=\"*********\"\r\n");
HAL_Delay(1000);
Sim900A_SendAtCmd(Msg);
HAL_Delay(1000);
HAL_UART_Transmit(&huart2, &ByteSend, 1,0xFFFF);
//
}
//发送中文消息
void Sim900A_SendChineseMsg(char* pMsg){
Sim900A_SendAtCmd("AT+CSCS=\"UCS2\"\r\n");
HAL_Delay(500);
Sim900A_SendAtCmd("AT+CMGF=1\r\n");
HAL_Delay(500);
Sim900A_SendAtCmd("AT+CSCA?\r\n");
HAL_Delay(500);
Sim900A_SendAtCmd("AT+CSMP=17,167,0,25\r\n");
HAL_Delay(500);
Sim900A_SendAtCmd("AT+CMGS=\"*********\"\r\n");//电话号码的unicode码
HAL_Delay(500);
Sim900A_SendAtCmd(pMsg);
HAL_Delay(500);
HAL_UART_Transmit(&huart2, &ByteSend, 1,0xFFFF);
}