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

📄 at_cmgs.java

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

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

import com.jzl.sirius.at.ATConstants;
import com.jzl.sirius.at.util.StringMatch;
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_CMGS extends ATCommandBase {
    private final static String RESPONSE_START = ATConstants.CRLF + "+CMGS:"; 
    
    private boolean foundFirstEnd  = false;
    
    private StringMatch firstMatch    = new StringMatch(ATConstants.CRLF + ">" + ATConstants.SP);
    

    private StringMatch errorMatch    = new StringMatch(ATConstants.RESPONSE_ERROR);
    private StringMatch cmeErrorMatch = new StringMatch(ATConstants.RESPONSE_CME_ERROR);
    private StringMatch cmsErrorMatch = new StringMatch(ATConstants.RESPONSE_CMS_ERROR);
    
    private boolean errorFlag = false;
    
    private StringBuffer error = new StringBuffer();
    
    //  text: 1, pdu: 0
    private int format;
    private String number;
    private String content;    
    
    
    private int mr;
    
    public AT_CMGS(String number, String text) {
        super(new StringMatch(RESPONSE_START));
        this.number = number;
        this.format = ATConstants.SMS_FORMAT_TEXT;
        this.content = text;
    }
    
    public AT_CMGS(String pdu) {
        super(new StringMatch(RESPONSE_START));
        this.format = ATConstants.SMS_FORMAT_PDU;
        this.content = pdu;
    }
    
    public String buildCommand() {
        StringBuffer command = new StringBuffer();
        if (format == ATConstants.SMS_FORMAT_TEXT) {
            // text
            command.append("AT+CMGS=\"");
            command.append(number);
            command.append("\"");
            command.append(ATConstants.CR);
        }
        else {
            // pdu
            int len = content.length() / 2;
            len--;
            
            command.append("AT+CMGS=");
            command.append(len);
            command.append(ATConstants.CR);
        }
        
        return command.toString();
    }

    public boolean readResponse(ByteBuffer buffer) {
        if (foundFirstEnd) {
            return super.readResponse(buffer);
        }
        else {
            while(buffer.hasRemaining()) {
                char ch = (char)buffer.get();
                
                if (firstMatch.match(ch)) {
                    foundFirstEnd = true;
                    buffer.clear();
                    
                    if (errorFlag) {
                        decodeErrorCode();
                        return true;
                    }
                    else {
                        writeContent();
                        break;
                    }
                }
                
                if (errorFlag) {
                    error.append(ch);
                }
                else {
                    if (errorMatch.match(ch)) {
                        errorFlag = true;
                    }
                    
                    if (cmeErrorMatch.match(ch)) {
                        error.append(ATConstants.RESPONSE_CME_ERROR);
                        errorFlag = true;
                    }
                    
                    if (cmsErrorMatch.match(ch)) {
                        error.append(ATConstants.RESPONSE_CMS_ERROR);
                        errorFlag = true;
                    }
                }
            }
        }
        
        return false;
    }
    
    protected void decodeOkResponse(CharBuffer response) {
        String resultStr = StringUtils.substring(response.toString(), RESPONSE_START, ATConstants.CRLF);
        
        if (resultStr != null && resultStr.trim().length() > 0) {
            mr = Integer.parseInt(resultStr.trim());
        }
        
    }
    
    private void decodeErrorCode() {
        String errorStr = error.toString();
        int idx = -1;
        String errorCode = "";
        if (errorStr.startsWith(ATConstants.RESPONSE_CME_ERROR)) {
            idx = errorStr.indexOf(ATConstants.CRLF, ATConstants.RESPONSE_CME_ERROR.length());
            if (idx != -1) {
                errorCode = errorStr.substring(ATConstants.RESPONSE_CME_ERROR.length(), idx);
                setCmeErrorCode(parseCode(errorCode));
            }
        }
        else if (errorStr.startsWith(ATConstants.RESPONSE_CMS_ERROR)) {
            idx = errorStr.indexOf(ATConstants.CRLF, ATConstants.RESPONSE_CMS_ERROR.length());
            if (idx != -1) {
                errorCode = errorStr.substring(ATConstants.RESPONSE_CMS_ERROR.length(), idx);
                setCmsErrorCode(parseCode(errorCode));
            }
        }
        else
            setResultCode(ATConstants.ERROR);
    }
    
    private int parseCode(String str) {
        try {
            return Integer.parseInt(str.trim());
        }
        catch(Exception ex) {
            return -1;
        }
    }
    
    
    
    private void writeContent() {
        try {
            StringBuffer sb = new StringBuffer();
            sb.append(content);
            sb.append(ATConstants.CHAR_SUB);
            engine.write(sb.toString());
        }
        catch(Exception ex) {
            
        }
    }

    public int getMr() {
        return mr;
    }

}

⌨️ 快捷键说明

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