我是靠谱客的博主 善良唇膏,最近开发中收集的这篇文章主要介绍EC20 TCP/IP AT指令(安卓串口通信开发),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

指令过程

1.等待串口接收字符串"RDY"模块系统启动成功。
2.发送"AT+CPIN?r",等待"+CPIN: READY",SIM准备就绪。
3.发送" AT+CSQr",等待"+CSQ: 25",网络信号值要大于10通信才稳定。
4.发送" AT+CREG?r",等待" +CREG: 0,1",网络注册成功后才能上网。
5.发送"AT+QIACT=1r",等待" ok",激活移动场景。
6. AT+QIACT?r",等待" +QIACT: 1,1,1,",是否获取到了IP地址如:“+QIACT: 1,1,1,"10.189.40.187"”。
7.发送" AT+QWWAN=1",等待" OK",启用有线网络。

/安卓代码过程/

           //接收信息
                String receivedMsg =StringUtils.toHexString(bytes);
                String receivedString =StringUtils.hexStringToString(receivedMsg);
                if (receivedString.contains("RDY")){
                    //查询SIM卡是否正常,返回ready则表示SIM卡正常
                    Message msg = new Message();
                    msg.obj = "RDY==模块系统启动成功";
                    msg.what=0;
                    myHandler.sendMessage(msg);
                    mreceivedMsg="RDY";
                    String sendMsg =StringUtils.convertStringToHex("AT+CPIN?");
                    byte[] sendBytes =StringUtils.toByteArray(sendMsg);
                    mSerialPortManager.sendBytes(sendBytes);

                }else   if (receivedString.contains("CPIN: READY")){
                    if (receivedString.contains("OK")){
                        //成功,下一步   发送AT+CSQ检测信号
                        Message msg = new Message();
                        msg.obj = "CPIN: READY==SIM准备就绪";
                        msg.what=0;
                        myHandler.sendMessage(msg);
                        Log.d(TAG, "onDataReceived: SIM准备就绪");
                        String sendMsg =StringUtils.convertStringToHex("AT+CSQ");
                        byte[] sendBytes =StringUtils.toByteArray(sendMsg);
                        mSerialPortManager.sendBytes(sendBytes);
                        mreceivedMsg="CPIN: READY";
                    }else {
                        //失败,需要重新发送
                        String sendMsg =StringUtils.convertStringToHex("AT+CPIN?");
                        byte[] sendBytes =StringUtils.toByteArray(sendMsg);
                        mSerialPortManager.sendBytes(sendBytes);
                    }
                }
                else   if (receivedString.contains("CSQ")){

                    if (receivedString.contains("OK")){
                        String  numStr="99";
                        String regex = "\d*";
                        Pattern p = Pattern.compile(regex);
                        Matcher m = p.matcher(receivedString.replaceAll("n",""));
                        while (m.find()) {
                            if (!"".equals(m.group())){
                                numStr=m.group();
                                break;
                            }}

                        int num=Integer.parseInt(numStr);
                        //检测信号在11~31之间则发送AT+CREG? 等待CREG: 0,1
                        if (num>10&&num<32){
                            Message msg = new Message();
                            msg.obj ="网络信号值"+receivedString;
                            msg.what=0;
                            myHandler.sendMessage(msg);
                            String sendMsg =StringUtils.convertStringToHex("AT+CREG?");
                            byte[] sendBytes =StringUtils.toByteArray(sendMsg);
                            mSerialPortManager.sendBytes(sendBytes);
                            mreceivedMsg=receivedString;
                        }else {
                            Message msg = new Message();
                            msg.obj ="AT+CSQ值==="+numStr;
                            msg.what=0;
                            myHandler.sendMessage(msg);
                            String sendMsg =StringUtils.convertStringToHex("AT+CSQ");
                            byte[] sendBytes =StringUtils.toByteArray(sendMsg);
                            mSerialPortManager.sendBytes(sendBytes);
                        }
                    }else {
                        String sendMsg =StringUtils.convertStringToHex("AT+CSQ");
                        byte[] sendBytes =StringUtils.toByteArray(sendMsg);
                        mSerialPortManager.sendBytes(sendBytes);
                    }
                }
              else   if (receivedString.contains("CREG")){
                  if (receivedString.contains("OK")&&receivedString.contains("CREG: 0,1")){
                     // 收到 CREG: 0,1,则网络注册成功  发送AT+QIACT=1 激活移动场景
                      Message msg = new Message();
                      msg.obj = "网络注册成功:"+receivedString;
                      msg.what=0;
                      myHandler.sendMessage(msg);
                      String sendMsg =StringUtils.convertStringToHex("AT+QIACT=1");
                      byte[] sendBytes =StringUtils.toByteArray(sendMsg);
                      mSerialPortManager.sendBytes(sendBytes);
                      mreceivedMsg=receivedString;
                      mreceivedMsg="CREG";
                  }else {
                      String sendMsg =StringUtils.convertStringToHex("AT+CREG?");
                      byte[] sendBytes =StringUtils.toByteArray(sendMsg);
                      mSerialPortManager.sendBytes(sendBytes);
                      mreceivedMsg=receivedString;
                  }
                }

                else   if (receivedString.contains("AT+QIACT=1")){
                    mreceivedMsg="AT+QIACT=1";
                }
              else   if (receivedString.contains("AT+QIACT?")){
                  //是否获取到了IP地址 如:“+QIACT: 1,1,1,"10.189.40.187"”
                    if (receivedString.contains("OK")&&receivedString.contains("QIACT: 1,1,1,")){
                        Message msg = new Message();
                        msg.obj = "获取到了IP地址"+receivedString;
                        msg.what=0;
                        myHandler.sendMessage(msg);
                        String sendMsg =StringUtils.convertStringToHex("AT+QWWAN=1");
                        byte[] sendBytes =StringUtils.toByteArray(sendMsg);
                        mSerialPortManager.sendBytes(sendBytes);
                        mreceivedMsg="AT+QIACT?";

                    }else {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        String sendMsg =StringUtils.convertStringToHex("AT+QIACT?");
                        byte[] sendBytes =StringUtils.toByteArray(sendMsg);
                        mSerialPortManager.sendBytes(sendBytes);
                    }

                }
              else   if (receivedString.contains("AT+QWWAN=1")){
                    mreceivedMsg="AT+QWWAN=1";
                }else   if (receivedString.contains("POWERED DOWN")){
                    mreceivedMsg="POWERED DOWN";
                    tvInfo="============开始==================";
                    Message msg = new Message();
                    msg.obj ="关闭电源:===="+receivedString;
                    msg.what=0;
                    myHandler.sendMessage(msg);
                }
                else   if (receivedString.contains("PB DONE")){
                    //结束通信
                    Message msg = new Message();
                    msg.obj ="PB DONE";
                    msg.what=0;
                    myHandler.sendMessage(msg);
                }
                else {
                    if (mreceivedMsg.contains("AT+QIACT=1")&&receivedString.contains("OK")){
                        //激活移动场景
                        Message msg = new Message();
                        msg.obj = "AT+QIACT=1";
                        msg.what=0;
                        myHandler.sendMessage(msg);
                        String sendMsg =StringUtils.convertStringToHex("AT+QIACT?");
                        byte[] sendBytes =StringUtils.toByteArray(sendMsg);
                        mSerialPortManager.sendBytes(sendBytes);
                        mreceivedMsg="AT+QIACT?";
                    }
                    if (mreceivedMsg.contains("AT+QWWAN=1")&&receivedString.contains("OK")){
                        Log.d(TAG, "onDataReceived: ================QWWAN");
                        //启用有线网络。
                        Message msg = new Message();
                        msg.obj = "OK==启用4g网络成功";
                        msg.what=0;
                        myHandler.sendMessage(msg);
                        Log.d(TAG, "onDataReceived:启用有线网络。");
                        mreceivedMsg="OK";

                    }

                }

最后

以上就是善良唇膏为你收集整理的EC20 TCP/IP AT指令(安卓串口通信开发)的全部内容,希望文章能够帮你解决EC20 TCP/IP AT指令(安卓串口通信开发)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部