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

📄 cmtievent.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;

/**
 * 收到类型为"+CMTI:"短信事件
 * 
 * @author xujian
 * @version 2.0
 * @see com.jzl.sirius.at.event.SMSEvent
 */
public class CMTIEvent extends MutiableEvent {
    
    private StringMatch startMatch = new StringMatch(ATConstants.CRLF + "+CMTI:");
    private StringMatch endMatch   = new StringMatch(ATConstants.CRLF);
    private boolean eventStarted = false;
    
    private StringBuffer sb = new StringBuffer();
	
	private String mem;
	private int index = -1;

    public void reset() {
        super.reset();
        eventStarted = false;
        mem = "";
        index = -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)) {
                    
                    // 事件结束,进行结果解析
                    decodeResponse();
                        
                    finish();
                    return true;
                    
                }
            }
            else 
                match(ch);
            
        }
        
        return false;
    }
    
    private void decodeResponse() {
        int pos = sb.indexOf(ATConstants.CRLF);
        if (pos == -1) 
            return;
        
        try {
            String str = sb.substring(0, pos).trim();
            pos = str.indexOf(',');
            if (pos == -1) {
                return;
            }
            
            mem   = str.substring(1, pos - 1).trim();
            index = Integer.parseInt(str.substring(pos + 1).trim());
        }
        catch(Exception ex) {
        }
    }

	public String getMem() {
		return mem;
	}

	public int getIndex() {
		return index;
	}
    
    public Object clone() throws CloneNotSupportedException {
        CMTIEvent obj = (CMTIEvent)super.clone();
        obj.sb.append(sb);
        obj.eventStarted = eventStarted;
        obj.mem = mem;
        obj.index = index;
        
        return obj;
    }

}

⌨️ 快捷键说明

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