EC20 TCP/IP AT指令(安卓串口通信开发)

指令过程

1.等待串口接收字符串"RDY"模块系统启动成功。
2.发送"AT+CPIN?\r",等待"+CPIN: READY",SIM准备就绪。
3.发送" AT+CSQ\r",等待"+CSQ: 25",网络信号值要大于10通信才稳定。
4.发送" AT+CREG?\r",等待" +CREG: 0,1",网络注册成功后才能上网。
5.发送"AT+QIACT=1\r",等待" 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指令(安卓串口通信开发))