SIM800C模块(2G)连接千寻

发布时间 2023-12-05 13:49:57作者: 妖岭

1. 测试

//ATK-SIM900A 各项测试(拨号测试、短信测试、GPRS测试)共用代码
//sim900a发送命令后,检测接收到的应答
//str:期待的应答结果
//返回值:0,没有得到期待的应答结果
//    其他,期待应答结果的位置(str的位置)
uint8_t* sim900a_check_cmd(uint8_t *str)
{
    char *strx=0;
    if(UART8__RX_STA&0X8000)        //接收到一次数据了
    { 
        UART8_RX_BUF[UART8__RX_STA&0X7FFF]=0;//添加结束符
        printf("%s",UART8_RX_BUF);
        strx=strstr((const char*)UART8_RX_BUF,(const char*)str);
    } 
    return (uint8_t*)strx;
}
//向sim900a发送命令
//cmd:发送的命令字符串(不需要添加回车了),当cmd<0XFF的时候,发送数字(比如发送0X1A),大于的时候发送字符串.
//ack:期待的应答结果,如果为空,则表示不需要等待应答
//waittime:等待时间(单位:10ms)
//返回值:0,发送成功(得到了期待的应答结果)
//       1,发送失败
uint8_t sim900a_send_cmd(uint8_t *cmd,uint8_t *ack,uint16_t waittime)
{
    uint8_t res=0;
    UART8__RX_STA=0;
    UART8_RX_REC_ATCOMMAD=1;
    if((u32)cmd<=0XFF)
    {
        while(READ_BIT(UART8->CR1,1U<<7) != 0);    //等待通道7传输完成
        UART8->TDR=(u32)cmd;
    }
    else
    {
        UART8SendCmd(cmd, strlen((char*)cmd));
    }
    if(ack&&waittime)        //需要等待应答
    {
        while(--waittime)    //等待倒计时
        {
//            uint32_t n = 409600;
//            while(n--);
            HAL_Delay(10);
            if(UART8__RX_STA&0X8000)//接收到期待的应答结果
            {
                if(sim900a_check_cmd(ack))break;//得到有效数据
                UART8__RX_STA=0;
            } 
        }
        if(waittime==0)
        {
            res=1;
        }
    }
    UART8_RX_REC_ATCOMMAD=0;
    return res;
} 

uint8_t sim900a_work_test(void)
{
    if(sim900a_send_cmd((uint8_t *)"AT",(uint8_t *)"OK",100))
    {
        if(sim900a_send_cmd((uint8_t *)"AT",(uint8_t *)"OK",100))
        {
            printf("sim900a SIM_COMMUNTION_ERR !\r\n");
            return SIM_COMMUNTION_ERR;    //通信不上
        }
    }
    printf("sim900a SIM_COMMUNTION_OK !\r\n");

    if(sim900a_send_cmd((uint8_t *)"AT+CPIN?",(uint8_t *)"READY",400))
    {
        printf("sim900a SIM_CPIN_ERR !\r\n");
        return SIM_CPIN_ERR;    //没有SIM卡
    }
    else
    {
        printf("sim900a SIM_CPIN_OK !\r\n");
    }
    if(sim900a_send_cmd((uint8_t *)"AT+CREG?",(uint8_t *)"0,1",400))
    {
        if(strstr((const char*)UART8_RX_BUF,"0,5")==NULL)
        {
            printf("sim900a SIM_NO_NET !\r\n");
             if(!sim900a_send_cmd((uint8_t *)"AT+CSQ",(uint8_t *)"OK",200))
             {
                    memcpy(SIM900_CSQ,UART8_RX_BUF+15,2);
             }
             return SIM_CREG_FAIL;    //等待附着到网络
        }
    }
    printf("sim900a OK!\r\n");
    return SIM_OK;
}
uint8_t GSM_Dect(void)
{
    uint8_t res;
    res=sim900a_work_test();    
    switch(res)
    {
        case SIM_OK:
            USART6SendString("GSM Test Self OK\r\n",strlen("GSM Test Self OK\r\n"));
            break;
        case SIM_COMMUNTION_ERR:
            USART6SendString("with GSM Communicate Fail,Waiting\r\n",strlen("with GSM Communicate Fail,Waiting\r\n"));
            break;
        case SIM_CPIN_ERR:
            USART6SendString("NO SIM Card\r\n",strlen("NO SIM Card\r\n"));
            break;
        case SIM_CREG_FAIL:
            USART6SendString("Registering on the network...\r\n",strlen("Registering on the network...\r\n"));
            USART6SendString("Current signal value: ",strlen("Current signal value: "));
            USART6SendString(SIM900_CSQ,2);
            USART6SendString("\r\n",2);
            break;        
        default:
            break;
    }
    return res;
}

2.TCP连接

uint8_t SIM900A_CONNECT_SERVER(uint8_t *IP_ADD,uint8_t *PORT)
{        
    if(sim900a_send_cmd((uint8_t *)"AT+CGATT?",(uint8_t *)": 1",100))     return 1;

    if(sim900a_send_cmd((uint8_t *)"AT+CSTT",(uint8_t *)"OK",600))    return 3;

    if(sim900a_send_cmd((uint8_t *)"AT+CIICR",(uint8_t *)"OK",600))    return 4;

    if(!sim900a_send_cmd((uint8_t *)"AT+CIFSR",(uint8_t *)"ERROR",600))    return 5;

    sprintf((char*)dtbuf,"AT+CIPSTART=\"TCP\",\"%s\",\"%s\"",IP_ADD,PORT);
    if(sim900a_send_cmd((uint8_t *)dtbuf,(uint8_t *)"CONNECT OK",600))    return 6;

    memset(dtbuf, 0, sizeof(dtbuf));
    return 0;
}    
uint8_t SIM900A_CONNECT_SERVER_SEND_INFOR(uint8_t *IP_ADD,uint8_t *PORT)
{
    uint8_t res;
    res=SIM900A_CONNECT_SERVER(IP_ADD,PORT);
    switch(res)
    {
        case 0:
            USART6SendString("CONNECT_SERVER_OK\r\n",strlen("CONNECT_SERVER_OK\r\n"));
            break;
        case 1:
            USART6SendString("Waiting for GSM module to attach to the network\r\n",strlen("Waiting for GSM module to attach to the network\r\n"));
            break;
        case 2:
            USART6SendString("Scene closing failed\r\n",strlen("Scene closing failed\r\n"));
            break;
        case 3:
            USART6SendString("CSTT Fail\r\n",strlen("CSTT Fail\r\n"));
            break;
        case 4:
            USART6SendString("CIICR Fail\r\n",strlen("CIICR Fail\r\n"));
            break;
        case 5:
            USART6SendString("CIFSR Fail\r\n",strlen("CIFSR Fail\r\n"));
            break;
        case 6:
            USART6SendString("CONNECT_SERVER_FAIL\r\n",strlen("CONNECT_SERVER_FAIL\r\n"));
            break;
        default:
            break;
    }
    return res;
}
uint8_t SIM900A_GPRS_SEND_DATA(uint8_t *temp_data)
{

    if(sim900a_send_cmd("AT+CIPSEND",">",100))     return 1;
    if(sim900a_send_cmd(temp_data,NULL,0))    return 2;
    if(sim900a_send_cmd((uint8_t *)0x1a,(uint8_t *)"SEND OK",1500))    return 3;
    return 0;
}

uint8_t SIM900A_GPRS_CLOSE(void)
{
     if(sim900a_send_cmd("AT+CIPCLOSE", (uint8_t *)"OK", 100))     return 1;
     return 0;
}

uint8_t GetRTCMData(uint8_t *data)
{
    uint8_t res;
connect:
    res = SIM900A_CONNECT_SERVER_SEND_INFOR((uint8_t*)IP_ADDRESS, (uint8_t*)IP_PORT);
    if(0 != res)
    {
        while(sim900a_send_cmd((uint8_t *)"AT+CIPSHUT",(uint8_t *)"OK",100));
        USART6SendString("CONNECT_SERVER_RESTART!\r\n",strlen("CONNECT_SERVER_RESTART!\r\n"));
        goto connect;
    }
send_data:
    res = SIM900A_GPRS_SEND_DATA(data);
    if(0 != res)
    {
        USART6SendString("RESTART_SEND_DATA!\r\n",strlen("RESTART_SEND_DATA!\r\n"));
        goto send_data;
    }
send_gga:
    res = SIM900A_GPRS_SEND_DATA(SendGGA);
    if(0 != res)
    {
        USART6SendString("RESTART_SEND_GGA!\r\n",strlen("RESTART_SEND_GGA!\r\n"));
        goto send_gga;
    }
    return res;
}