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

📄 cmtevent.java

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

import java.nio.ByteBuffer;

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

/**
 * 收到类型为"+CMT:"短信事件
 * 
 * @author xujian
 * @version 2.0
 * @see com.jzl.sirius.at.event.MutiableEvent
 */
public class CMTEvent extends MutiableEvent {

    private StringMatch startMatch = new StringMatch(ATConstants.CRLF + "+CMT:");
    private StringMatch endMatch   = new StringMatch(ATConstants.CRLF);
    private boolean eventStarted = false;
    
    private StringBuffer sb = new StringBuffer();
    
    private int mode = ATConstants.SMS_FORMAT_TEXT;
    private int crlfCount = 0;

	private int length = -1;
	private int alpha  = -1;
	private String pdu;
    
	private String oa;
	private String scts;
	private int tooa = -1;
	private int fo = -1;
	private int pid = -1;
	private int dcs = -1;
	private int tosca = -1;
	private String sca;
	private String data;

	/**
	 * <pre>
	 * mode=1,Text mode;mode=0,pdu mode.
	 * For mt=2 and PDU mode
	 * 	Unsolicited response
	 * 	+CMT: [alpha],length
	 * 	pdu
	 * For mt=2 and text mode
	 * 	Unsolicited response
	 * 	+CMT: oa,[alpha],scts[,tooa,fo,pid,dcs,sca,tosca,length]
	 * 	data
	 * </pre>
	 * 
	 * @param mode
	 *			mode=1,Text mode;mode=0,pdu mode.
	 */
	public CMTEvent(int mode) {
		this.mode = mode;
	}
    
    public void reset() {
        super.reset();
        eventStarted = false;
        crlfCount = 0;
        pdu = "";
        length   = -1;
        alpha = -1;
        sb = new StringBuffer();
    }
    
    public boolean match(char ch) {
        eventStarted = startMatch.match(ch);
        return eventStarted;
    }
    
    public boolean readResponse(ByteBuffer buffer) {
        while(buffer.hasRemaining()){
            
            char ch = (char)buffer.get();
            if (eventStarted) {
                sb.append(ch);
                if (endMatch.match(ch)) {
                    crlfCount++;
                    
                    if (crlfCount == 2) {
                        // 事件结束,进行结果解析
                        if (mode == ATConstants.SMS_FORMAT_TEXT)
                            decodeTextResponse();
                        else
                            decodePduResponse();
                        
                        finish();
                        return true;
                    }
                    
                }
            }
            else 
                match(ch);
            
        }
        
        return false;
    }
    
    private void decodeTextResponse(){
        
    }
    
    private void decodePduResponse(){
        int index = sb.indexOf(ATConstants.CRLF);
        if (index == -1) 
            return;
        
        try {
            String str = sb.substring(0, index).trim();
            int idx = str.indexOf(',');
            if (idx == -1) {
                return;
            }
            
            if (idx != 0)
                alpha  = Integer.parseInt(str.substring(1, idx - 1).trim());
            length = Integer.parseInt(str.substring(idx + 1).trim());
            
            str = sb.substring(index + ATConstants.CRLF.length());
            index = str.indexOf(ATConstants.CRLF);
            
            if (index == -1)
                return;
            
            pdu = str.substring(0, index).trim();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
	

	public int getLength() {
		return length;
	}

	public int getAlpha() {
		return alpha;
	}

	public String getPdu() {
		return pdu;
	}

	public String getOa() {
		return oa;
	}

	public String getScts() {
		return scts;
	}

	public int getTooa() {
		return tooa;
	}

	public int getFo() {
		return fo;
	}

	public int getPid() {
		return pid;
	}

	public int getDcs() {
		return dcs;
	}

	public int getTosca() {
		return tosca;
	}

	public String getSca() {
		return sca;
	}

	public String getData() {
		return data;
	}
	
    public Object clone() throws CloneNotSupportedException {
        CMTEvent obj = (CMTEvent)super.clone();
        obj.sb.append(sb);
        obj.eventStarted = eventStarted;
        obj.mode = mode;
        obj.alpha = alpha;
        obj.pdu = pdu;
        obj.length = length;
        obj.crlfCount = crlfCount;
        
        return obj;
    }

}

⌨️ 快捷键说明

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