⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 at_cgpaddr.java

📁 自己实现的基于串口通讯发送短消息的低层协议JAVA代码
💻 JAVA
字号:
package com.jzl.sirius.at.command;

import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Iterator;

import com.jzl.sirius.at.ATConstants;
import com.jzl.sirius.at.util.StringUtils;

/**
 * @author xujian
 * @version 2.0
 * @see com.jzl.sirius.at.ATCommand
 * @see com.jzl.sirius.at.command.ATCommandBase
 */
public class AT_CGPADDR extends ATCommandBase {

	private StringBuffer command = new StringBuffer();

	private int commandType;
	private HashMap<Integer, String> addresses = new HashMap<Integer, String>();
	private int[] definedCids = new int[0];

	public AT_CGPADDR(boolean tested) {
		commandType = tested ? COMMAND_TYPE_TEST : COMMAND_TYPE_ACTION;
		command.append(tested ? "AT+CGPADDR=?" : "AT+CGPADDR");
		command.append(ATConstants.CR);
	}

	public AT_CGPADDR() {
		this(false);
	}

	public AT_CGPADDR(int cid1) {
        commandType = COMMAND_TYPE_ACTION;
        command.append("AT+CGPADDR=");
        command.append(cid1);
        command.append(ATConstants.CR);
	}

	public AT_CGPADDR(int cid1, int cid2) {
        commandType = COMMAND_TYPE_ACTION;
        command.append("AT+CGPADDR=");
        command.append(cid1);
        command.append(",");
        command.append(cid2);
        command.append(ATConstants.CR);
	}

	public AT_CGPADDR(int cid1, int cid2, int cid3) {
        commandType = COMMAND_TYPE_ACTION;
        command.append("AT+CGPADDR=");
        command.append(cid1);
        command.append(",");
        command.append(cid2);
        command.append(",");
        command.append(cid3);
        command.append(ATConstants.CR);
	}

	public AT_CGPADDR(int cid1, int cid2, int cid3, int cid4) {
        commandType = COMMAND_TYPE_ACTION;
        command.append("AT+CGPADDR=");
        command.append(cid1);
        command.append(",");
        command.append(cid2);
        command.append(",");
        command.append(cid3);
        command.append(",");
        command.append(cid4);
        command.append(ATConstants.CR);
	}
    
    public String buildCommand() {
        return command.toString();
    }
    
    private void decodeAddresses(String str) {
        String startToken = ATConstants.CRLF + "+CGPADDR:";
        
        while(true) {
            int pos = str.indexOf(startToken);
            if (pos == -1)
                return;
            str = str.substring(pos + startToken.length());
            pos = str.indexOf(ATConstants.CRLF);
            if (pos == -1)
                return;
            
            String addr = str.substring(0, pos);
            String[] arry = addr.split(",");
            addresses.put(Integer.decode(arry[0].trim()), arry[1].trim());
            
            str = str.substring(pos);
        }
    }
    
    protected void decodeOkResponse(CharBuffer response) {
        String str = response.toString();
        try {
            if (commandType == COMMAND_TYPE_ACTION) {
                // <CR><LF>
                // +CGPADDR: <cid>,<PDP_addr>
                // <CR><LF>
                // [+CGPADDR: <cid>,<PDP_addr>[…]]
                // <CR><LF>
                // <CR><LF>
                // OK
                // <CR><LF>
                decodeAddresses(str);
                
                
            } else if (commandType == COMMAND_TYPE_TEST) {
                // <CR><LF>
                // +CGPADDR: (list of defined <cid>s)
                // <CR><LF>
                // <CR><LF>
                // OK
                // <CR><LF>
                str = StringUtils.substring(str, "+CGPADDR:", ATConstants.CRLF).trim();
                str = StringUtils.substring(str, "(", ")");
                String[] arry = str.split(",");
                definedCids = new int[arry.length];
                for (int i = 0; i < arry.length; i++) {
                    definedCids[i] = Integer.parseInt(arry[i].trim());
                }
            }
        }
        catch(Exception ex) {
            
        }
    }

	public int[] getCids() {
        int[] cids = new int[addresses.keySet().size()];
        
        
        Iterator itr = addresses.keySet().iterator();
        int i = 0;
        while(itr.hasNext()) {
            cids[i++] = ((Integer)itr.next()).intValue();
        }
        
		return cids;
	}

	public String getAddress(int cid) {
		return addresses.get(cid);
	}

	public int[] getDefinedCids() {
		return definedCids;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -