首页 > 其他分享 >SIM800C模块(2G)连接千寻

SIM800C模块(2G)连接千寻

时间:2023-12-05 13:56:06浏览次数:53  
标签:return cmd uint8 send 千寻 SIM800C 2G OK sim900a

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;
}

 

标签:return,cmd,uint8,send,千寻,SIM800C,2G,OK,sim900a
From: https://www.cnblogs.com/boring-luobo/p/17877039.html

相关文章

  • CF1692G 2^Sort 题解
    题意:思路:必要性:对于任意一个符合条件的区间$[l,r]$,任意相邻两项,满足$a_i<2*a_{i+1}(l\lei\ler-1)$。充分性:对于任意一个长度为$k+1$的区间$[l,r]$,如果任意相邻两项满足$a_i<2*a_{i+1}(l\lei\ler-1)$,那么该区间即为所求区间。......
  • 【全球首发】双核[email protected],仅99元起?含税?哇!!
          ......
  • 16位微控制器PIC24FJ256GL405-I/PT、PIC24FJ128GU408-I/PT、PIC24FJ32GB002T-I/ML(MCU)
    一、PIC24FJ256GL405-I/PT、PIC24FJ128GU408-I/PT16位微控制器PIC24FJ-GU4/GL416位微控制器(MCU)提供高达512KB的双分区闪存,支持实时无线(OTA)更新和EEPROM仿真。除了若干内核独立外设(CIP),PIC24FJ-GU4/GL4MCU还包括一个全速USB和一个支持动画的分段LCD控制器。这些器件......
  • 对 .NET程序2G虚拟地址紧张崩溃 的最后一次反思
    一:背景1.讲故事最近接连遇到了几起2G虚拟地址紧张导致的程序崩溃,基本上90%都集中在医疗行业,真的很无语,他们用的都是一些上古的XP,Windows7x86,我也知道技术人很难也基本无法推动硬件系统和设备的升级,这里蕴含了巨大的人情世故。写这一篇的目的是想系统化的整理一下如何配......
  • Tenzing and Random Operations CF1842G 题解
    设\(m\)次选的位置分别为\(b_{1\simm}\)。于是答案为\(\mathbbE(\prod\limits_{i=1}^{n}(a_i+\sum\limits_{j=1}^{m}[b_j\lei]\cdotv))=\frac{S}{n^m}\)。首先考虑期望很难做,希望将期望转化为概率形式,发现这样有点困难。再考虑因为所有方案等概率,将求期望转化......
  • CF1842G
    第一次听没听懂,补个笔记。弄懂这种奇妙拆贡献后感觉非常厉害。答案的形式为:\(\prod(a_i+k\cdotv)\),这些\(v\)是前面的操作带来的影响。我们考虑一个个加入这个\((a_i+k\cdotv)\),并且维护很多个等价类,使得这个值可以根据分开等价类的那个标志被确认。\(k\),不过是前......
  • 12Go语言基础之结构体
    Go语言中没有“类”的概念,也不支持“类”的继承等面向对象的概念。Go语言中通过结构体的内嵌再配合接口比面向对象具有更高的扩展性和灵活性。类型别名和自定义类型自定义类型在Go语言中有一些基本的数据类型,如string、整型、浮点型、布尔等数据类型,Go语言中可以使用type关键......
  • Codeforces 1862G 题解
    传送门题解因为有这个操作:将序列\(a\)加上\(\{n,n-1,\cdots,1\}\),考虑差分。那么显然每次操作会将差分数组中的每个元素减去\(1\),如果差分数组中有\(0\),就会把\(0\)删除。所以可以发现差分数组中剩下的一定是操作前的最大值。由于操作后最大值还是最大值,最小值仍......
  • OpenCV4.1.0编译时提示“CV_BGR2GRAY”: 未声明的标识符
    OpenCV版本为4.1.0使用CV_BGR2GRAY时报错:“CV_BGR2GRAY”:未声明的标识符解决方法一:添加头文件:#include<opencv2/imgproc/types_c.h>解决方法二:在新版本中,CV_BGR2GRAY被COLOR_BGR2GRAY替换,只需将CV改成COLOR即可。翻译搜索复制......
  • 无涯教程-Matplotlib - Subplot2grid函数
    Subplot2grid函数为在特定位置创建轴对象提供了更大的灵活性,它还允许axis对象跨越多个行或列。Plt.subplot2grid(shape,location,rowspan,colspan)在以下示例中,图形对象的3X3网格填充行和列跨度大小不同的轴对象,每个对象都显示不同的图。importmatplotlib.pyplotasplt......