找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1958|回复: 4
收起左侧

单片机与GPRS模块串口通信不稳定,如何解决

[复制链接]
ID:247342 发表于 2019-11-26 07:53 | 显示全部楼层 |阅读模式
在单片机与GPRS模块通信中,出现串口通信不稳定的问题,有时候指令发不出去,一直找不到原因,会不会是定时器中断的影响,请大神指教
回复

使用道具 举报

ID:648281 发表于 2019-11-26 10:40 | 显示全部楼层
没有程序,没有原理图,不好分析
回复

使用道具 举报

ID:546770 发表于 2019-11-26 11:31 | 显示全部楼层
会不会是你发送的频率太快了?????
回复

使用道具 举报

ID:247342 发表于 2019-11-26 22:08 | 显示全部楼层
if(sim_ready_flag==1&&csq_test_end_falg==0)  //判断SIM卡是否准备就绪,信号检测结束标识是否是0
                         {
                                 if(csq_send_flag==0&&csq_send_frequ!=0)  //判断指令发送标识及发送次数
                                         {
                                                 send_string(csq);   //发送网络信号检测指令
                                                 csq_send_frequ--;
                                                 csq_send_flag=1;
                                                 wait_2s=2;
                                                 timing_2s_end_flag=0;                                                                                 
                                                 }
                                        if(csq_send_flag==1&&timing_2s_end_flag==1)  //判断指令是否发出及计时是否结束
                                                {
                                                 if(strstr(Rec_buf,"OK")!=NULL)    //判断返回数据中是否包含‘OK’字符串
                                                        {
                                                                if(Rec_buf[8]=='9'&&Rec_buf[9]=='9')  //判断检测的信号强度是否是99
                                                                  {
                                                                    if(csq_send_frequ==0)    //判断发送是否结束
                                                                                        {
                                                                                                 csq_test_flag=0;    //信号测试标识值0
                                                                                                 net_connect_flag=0;   //关闭网络连接
                                                                                                        }
                                                                    }
                                                                else
                                                                  {
                                                                     csq_test_flag=1;   //信号测试标识值1
                                                                                 csq_send_frequ=0;
                                                                    }                                                               
                                                                csq_send_flag=0;                                                                                
                                                                sig_strength_num[0]=Rec_buf[8];  //提取信号强度值
                                                                sig_strength_num[1]=Rec_buf[9];               
                                                                Clr_Rec_buf();
                                                                }
                                                 else   //如果返回数据中不包含‘OK’字符串,则检测失败
                                                         {
                                                                 csq_send_flag=0;
                                                                 if(csq_send_frequ==0)  //判断发送是否结束
                                                                  {
                                                                         csq_test_flag=0;   //信号测试标识值0
                                                                   net_connect_flag=0;   //关闭网络连接
                                                                         gprs_p=0;
                                                                    }
                                                                  Clr_Rec_buf();
                                                                 }
                                                 if(csq_send_frequ==0)          //判断发送是否结束
                                                         {                                                
                                                                csq_test_end_falg=1;   //信号检测结束标识值1
                                                                sim_ready_flag=0;        
                                                                 }
                                                 }                                       
                                }
                        
/************检测网络注册状态************/
                        if(csq_test_flag==1&&creg_test_end_flag==0)   //判断信号检测是否成功,网络状态测试结束标识是否为0
                                {
                                        if(creg_send_flag==0&&creg_send_frequ!=0)  //判断指令是否发出及发送次数
                                         {
                                                 send_string(creg);   //发送网络注册状态指令
                                                 creg_send_frequ--;
                                                 creg_send_flag=1;
                                                 wait_2s=2;
                                                 timing_2s_end_flag=0;
                                                
                                                 }
                                         if(creg_send_flag==1&&timing_2s_end_flag==1)  //判断指令是否发出及计时是否结束
                                                {
                                                 if(strstr(Rec_buf,"OK")!=NULL&&(Rec_buf[11]=='1'||Rec_buf[11]=='5'))  //判断返回字符串是否包含‘OK’,网络注册状态是‘1’或‘5’
                                                        {
                                                                creg_succ_flag=1;   //注册成功标识值1
                                                                creg_send_frequ=0;  //发送次数值0
                                                                creg_send_flag=0;
                                                                Clr_Rec_buf();
                                                                }
                                                 else
                                                         {
                                                                 creg_send_flag=0;   
                                                                 if(creg_send_frequ==0)  //判断发送是否结束
                                                                        {
                                                                         creg_succ_flag=0;   //注册成功标识值0
                                                                         net_connect_flag=0;  //关闭连接
                                                                         gprs_p=0;
                                                                                }
                                                                 Clr_Rec_buf();
                                                                 }
                                                         if(creg_send_frequ==0)   //判断发送是否结束
                                                         {
                                                          creg_test_end_flag=1;          //网络注册状态测试结束标识值1
                                                          //csq_test_flag=0;
                                                           }
                                                        }                                                                                
                                        }
                                
/************GPRS附着状态************/               
                 if(creg_succ_flag==1&&cgatt_test_end_falg==0)  //判断网络注册是否成功,GPRS附着状态检测结束标识是否为0
                         {
                                 if(cgatt_send_flag==0&&cgatt_send_frequ!=0)  //判断指令是否发送及发送次数
                                         {
                                                 //Clr_Rec_buf();
                                                 send_string(cgatt);   //发送测试指令
                                                  cgatt_send_frequ--;
                                                 cgatt_send_flag=1;
                                                 wait_2s=4;
                                                 timing_2s_end_flag=0;
                                                
                                                 }
                                        if(cgatt_send_flag==1&&timing_2s_end_flag==1)  //判断指令是否发出及计时是否结束
                                                {
                                                 if(strstr(Rec_buf,"OK")!=NULL&&Rec_buf[10]=='1')
                                                        {
                                                                cgatt_test_flag=1;  //GPRS附着检测标识值1
                                                                cgatt_send_frequ=0;  //发送次数值0
                                                                cgatt_send_flag=0;
                                                                Clr_Rec_buf();
                                                                }
                                                 else
                                                         {
                                                                 cgatt_send_flag=0;
                                                                 if(cgatt_send_frequ==0)  //判断发送是否结束
                                                                  {
                                                                         cgatt_test_flag=0;  //GPRS附着检测标识值0
                                                                   net_connect_flag=0;  //关闭网络连接
                                                                         gprs_p=0;
                                                                    }
                                                                 Clr_Rec_buf();
                                                                 }
                                           if(cgatt_send_frequ==0)
                                                   {
                                                                        creg_succ_flag=0;
                                                                        cgatt_test_end_falg=1;         //GPRS附着检测结束标识值1
                                                      }
                                                        }
                                        }        
                        
/************设置APN************/                        
                 if(cgatt_test_flag==1&&cstt_set_end_falg==0)   //判断GPRS附着检测标识值
                         {
                                 if(cstt_send_flag==0&&cstt_send_frequ!=0)  //判断指令是否发送及发送次数
                                         {
                                                 send_string(cstt);  //发送设置APN指令
                                                 cstt_send_frequ--;
                                                 cstt_send_flag=1;
                                                 wait_2s=3;
                                                 timing_2s_end_flag=0;
                                                 ;
                                                 }
                                        if(cstt_send_flag==1&&timing_2s_end_flag==1)  //判断指令是否发送及计时是否结束
                                                {
                                                 if(strstr(Rec_buf,"OK")!=NULL)  //判断返回字符串是否包含“OK”
                                                        {
                                                                cstt_set_flag=1;  //APN设置标识值1
                                                                cstt_send_frequ=0;
                                                                cstt_send_flag=0;
                                                                Clr_Rec_buf();
                                                                }
                                                        else
                                                         {
                                                                 cstt_send_flag=0;
                                                                 if(cstt_send_frequ==0)  //判断发送是否结束
                                                                  {
                                                                         cstt_set_flag=0;  //APN设置标识值0
                                                                   net_connect_flag=0;
                                                                         gprs_p=0;        
                                                                    }
                                                                 Clr_Rec_buf();
                                                                 }
                                                        if(cstt_send_frequ==0)        
                                                          {
                                                                         cgatt_test_flag=0;  //GPRS附着检测标识值0
                                                                         cstt_set_end_falg=1;         
                                                             }
                                                        }
                                        }        
                        
/*****建立无线链路(GPRS或者CSD)*****/               
                        if(cstt_set_flag==1&&ciicr_set_end_falg==0)  //判断APN设置状态标识
                         {
                                 if(ciicr_send_flag==0&&ciicr_send_frequ!=0)  //判断指令是否发送及发送次数
                                         {
                                                 send_string(ciicr);  //发送建立无线链路指令
                                                
                                                 ciicr_send_frequ--;
                                                 ciicr_send_flag=1;
                                                 wait_2s=3;
                                                 t_1s=0;
                                                 timing_2s_end_flag=0;
                                                
                                                 }
                                        if(ciicr_send_flag==1&&timing_2s_end_flag==1)  //判断指令是否发送及计时是否结束
                                                {
                                                 if(strstr(Rec_buf,"OK")!=NULL)
                                                        {
                                                                ciicr_set_flag=1;  //无线链路设置标识值1
                                                                ciicr_send_frequ=0;
                                                                cstt_set_flag=0;    //APN设置状态标识值0
                                                                ciicr_set_end_falg=1;
                                                                ciicr_send_flag=0;
                                                                for(i=0;i<24;i++)
                                                                   str2[i]=Rec_buf[i];
                                                                Clr_Rec_buf();
                                                                }
                                                        else
                                                         {
                                                                 n++;        
                                                                 if(n<4)
                                                                  {
                                                                          wait_2s=3;
                                                                                t_1s=0;
                                                                                timing_2s_end_flag=0;
                                                                                 }                                         
                                                                 if(n==4)  //判断发送是否结束
                                                                  {                 
                                                                  
                                                                         ciicr_send_flag=0;
                                                                         //Clr_Rec_buf();
                                                                         n=0;
                                                                    }        

                                                                        if(ciicr_send_frequ==0)
                                                                          {
                                                                            ciicr_set_flag=0;   //无线链路设置标识值0
                                                                                        cstt_set_flag=0;    //APN设置状态标识值0
                                                                            ciicr_set_end_falg=1;        
                                                                                         net_connect_flag=0;  //关闭网络连接
                                                                                        gprs_p=0;
                                                                            }                                                                                
                                                                        for(i=0;i<24;i++)
                                                                     str2[i]=Rec_buf[i];
                  Clr_Rec_buf();                                                                        
                                                                 }                                                                                                                 
                                                        }
                                        }      

ciicr这条指令就是发不出去,其他的都正常
回复

使用道具 举报

ID:546770 发表于 2019-11-27 09:09 | 显示全部楼层
我室友之前也调过一个GPRS的模块,也是很不稳定,一方面是那个模块是个便宜货,60一个,其次是舍内信号不好,天线都是那衣叉顶出窗户外面去的,如果你这个一直无法发送的话,可以把你程序里面写一个串口发送的字符串神马的,用串口助手看看进到这个if里面没有,程序没问题再找找硬件的问题,要对照在使用手册,放平心态一步一步地检查;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表