SIM900A GPRS程序

 
#include "bsp_SysTick.h"
#include "bsp_usart1.h"
#include "bsp_usart3.h"
#include "sim900a.h"
char ime[16];//IMEI串号
char ccid[20];//CCID号码
char csq;//CSQ

发送命令

/**
 * @brief  sim900a_cmd  发送命令
 * @param  cmd:命令字符串指针,reply:希望得到的命令响应,waittime:命令等待的时间
 * @retval 命令是否得到正常响应,正常:SIM900A_TRUE,不正常:SIM900A_FALSE
 */
uint8_t sim900a_cmd(char *cmd, char *reply,uint32_t waittime )
{    
    SIM900A_CLEAN_RX();                 //清空了接收缓冲区数据

    SIM900A_TX(cmd);                    //发送命令

    if(reply == 0)                      //不需要接收数据
        return SIM900A_TRUE;
    
    SIM900A_DELAY(waittime);                 //延时
    
    return sim900a_cmd_check(reply);    //对接收数据进行处理
}

检测响应

/**
 * @brief  sim900a_cmd_check  检测命令的响应
 * @param  reply命令响应的字符串指针
 * @retval 检测到匹配的响应SIM900A_TRUE,没检测到匹配的响应SIM900A_FALSE
 */
sim900a_res_e sim900a_cmd_check(char *reply)
{
    char *redata;

    redata = SIM900A_RX();   //接收数据    

    if(strstr(redata,reply) != NULL) 
    {
        SIM900A_CLEAN_RX();
        return SIM900A_TRUE;        //查找到匹配的响应内容
    }
    else
    {
        SIM900A_CLEAN_RX();
        return SIM900A_FALSE;       
    }
}

等待应答

/**
 * @brief  sim900a_waitask  等待数据应答 
 * @param  waitask_hook,检测事件勾子函数,若产生事件,退出本函数。
 * @retval 接收到的应答数据指针
 */
char * sim900a_waitask(uint8_t waitask_hook(void))  //等待有数据应答
{
    uint8_t len;
    char *redata;
    do{
        redata = SIM900A_RX_LEN(len);  //接收数据
        if(waitask_hook!=0)
        {
            if(waitask_hook()==SIM900A_TRUE)  //返回 SIM900A_TRUE 表示检测到事件,需要退出
            {
                redata = 0;
                return redata;               
            }
        }
    }while(len==0);  //接收数据为0时一直等待


​ SIM900A_DELAY(20); //延时,确保能接收到全部数据(115200波特率下,每ms能接收11.52个字节) ​ return redata; ​ }

获取号码

/**
 * @brief  sim900a_cnum 
 * @param  num:存储本机号码的指针
 * @retval 返回SIM900A_FALSE表示获取失败,返回SIM900A_TRUE表示成功
 */
uint8_t sim900a_cnum(char *num)
{
    char *redata;
    uint8_t len;
    
    if(sim900a_cmd("AT+CNUM\r","OK", 1000) != SIM900A_TRUE)
    {
        return SIM900A_FALSE;
    }
    
    redata = SIM900A_RX();  //接收数据
    
    if(len == 0)
    {
        return SIM900A_FALSE;
    }
    
    //第一个逗号后面的数据为:"本机号码"
    while(*redata != ',')
    {
        len--;
        if(len==0)
        {
            return SIM900A_FALSE;
        }
        redata++;
    }
    redata+=2;
    
    while(*redata != '"')
    {
        *num++ = *redata++;
    }
    *num = 0;  //字符串结尾需要清0
    return SIM900A_TRUE;
}

GPRS初始化

/**
 * @brief  sim900a_gprs_init GPRS¹¦Äܳõʼ»¯
 * @param  ÎÞ
 * @retval ÎÞ
 */
void sim900a_gprs_init(void)
{
    sim900a_tx_printf("AT+CGCLASS=\"B\"\r");  //设置移动台类别为"B"
    SIM900A_DELAY(100);
    sim900a_tx_printf("AT+CGDCONT=1,\"IP\",\"CMNET\"\r");  //PDP上下文标识1,互联网协议,接入点名称:CMNET
    SIM900A_DELAY(100);
    sim900a_tx_printf("AT+CGATT=1\r");  //附着 GPRS 网络
    SIM900A_DELAY(100);
    printf(SIM900A_RX());
    SIM900A_CLEAN_RX();
}

建立TCP连接

/**
 * @brief  sim900a_gprs_tcp_link 建立TCP连接
 * @param  无
 * @retval 无
 */
sim900a_res_e sim900a_gprs_tcp_link(char * serverip,char * serverport)
{
    SIM900A_CLEAN_RX();
  sim900a_tx_printf("AT+CIPSTART=\"TCP\",\"%s\",\"%s\"\r",serverip,serverport);
    Delay_ms(2000);//延时
    printf(SIM900A_RX());//输出返回信息
    if (sim900a_cmd_check("CONNECT OK") == SIM900A_TRUE)//连接成功
    {
        SIM900A_CLEAN_RX();
        return SIM900A_TRUE;		
    }
    else
    {
        SIM900A_CLEAN_RX();
        return SIM900A_FALSE;
    }
}

检测网络状态

/**
 * @brief  sim900a_gprs_status 判断网络连接状态 
 * @param  无
 * @retval 返回1表示已连接  返回0表示未连接
 */
sim900a_res_e sim900a_gprs_status(void)
{
    char redata[256];
    char *p;
    SIM900A_CLEAN_RX();
    sim900a_tx_printf("AT+CIPSTATUS\r");
    Delay_ms(100);
  strcpy(redata, SIM900A_RX());   //接收数据 
    SIM900A_CLEAN_RX();
    p = strstr(redata,"STATE");//返回STATE后的字符串
    if (p)
        printf(p);
    else
    {
        printf(redata);
        SIM900A_SEND_ENDCHAR();//没有返回  可能是处于发送但未完成的状态
    }
    
    if(strstr(redata,"STATE: CONNECT OK") != NULL)//成功
        return SIM900A_TRUE;
    else
        return SIM900A_FALSE;
}

发送数据

/**
 * @brief  sim900a_gprs_send 通过GPRS发送数据
 * @param  str:数据指针
 * @retval 无
 */
sim900a_res_e sim900a_gprs_send(char * str)
{
    SIM900A_CLEAN_RX();                 //清空了接收缓冲区数据
  sim900a_tx_printf("AT+CIPSEND\r");
    SIM900A_DELAY(100);                 //延时
  if (sim900a_cmd_check(">") == SIM900A_TRUE)
    {
        sim900a_tx_printf("%s",str);
        SIM900A_DELAY(100);
        SIM900A_SEND_ENDCHAR();
        SIM900A_DELAY(2000);
        return sim900a_cmd_check("SEND OK");
    }
    else
    {
        SIM900A_SEND_ENDCHAR();
        return SIM900A_FALSE;
    }
}

断开连接

/**
 * @brief  sim900a_gprs_link_close 断开网络连接
 * @param  无
 * @retval 无
 */
void sim900a_gprs_link_close(void) 
{
    sim900a_tx_printf("AT+CIPCLOSE=1\r");
}

发送命令

/**
 * @brief  sim900a_gprs_checkIP 查IP
 * @param  无
 * @retval 无
 */
char* sim900a_gprs_checkIP(void) 
{
    char redata[256];
    char *p = redata;
    SIM900A_CLEAN_RX();
    sim900a_tx_printf("AT+CIFSR\r");
    SIM900A_DELAY(100);
    strcpy(redata, SIM900A_RX());
    SIM900A_CLEAN_RX();
    p += 11;
    return p;
}

获取IMEI

void get_IMEI(char num[15])
{
    u8 	i;
    char str[16];
    char redata[256];
    SIM900A_CLEAN_RX();
  sim900a_tx_printf("AT+GSN\r");
    SIM900A_DELAY(100);
    strcpy(redata, SIM900A_RX());
    SIM900A_CLEAN_RX();
    for (i = 0; i < 15; i++)
    {
        num[i] = redata[i+9];
        str[i] = num[i];
    }
    str[15] = '\0';
    printf("IMEI:%s\r\n",str);
}

判断状态

/**
 * @brief  gprs_reg_status 判断网络注册状态 
 * @param  无
 * @retval 
 */
sim900a_res_e gprs_reg_status(void)
{
    char redata[256];
    SIM900A_CLEAN_RX();
    sim900a_tx_printf("AT+CGREG?\r");
    SIM900A_DELAY(100);
   strcpy(redata, SIM900A_RX());  //接收数据 
    SIM900A_CLEAN_RX();
    
  if (redata[22] == '1')
    {
        printf("已注册本地网络\r\n");
        return SIM900A_TRUE;
    }
    else if  (redata[22] == '5')
    {
        printf("已注册漫游网络\r\n");
        return SIM900A_TRUE;
    }
    else
    {	
        printf("未注册 ERR:%s\r\n",redata[22]);
        return SIM900A_FALSE;
    }
}

获取CCID

void get_CCID(char num[20])
{
    u8 i;
    char str[21];
    char redata[256];
    SIM900A_CLEAN_RX();
    sim900a_tx_printf("AT+CCID\r");
    Delay_ms(100);
    strcpy(redata, SIM900A_RX());
    SIM900A_CLEAN_RX();
    for (i = 0; i < 20; i++)
    {
        num[i] = redata[i+10];
        str[i] = num[i];
    }
    str[20] = '\0';
    printf("CCID:%s\r\n",str);
}

获取CSQ

void get_CSQ(char *num)
{
    char redata[256];
    SIM900A_CLEAN_RX();
        sim900a_tx_printf("AT+CSQ\r");
    SIM900A_DELAY(100);
    strcpy(redata, SIM900A_RX());
    SIM900A_CLEAN_RX();

    *num = redata[15] - '0';
    if (redata[16] == ',')
        *num = redata[15] - '0';
    else if (redata[17] == ',')
        *num = (redata[15] - '0') * 10 + (redata[16] - '0');
    else
        *num = 99;
    printf("CSQ:%d\r\n",*num);
}