ATCommand 指令封裝

package com.magima.messenger.gsmmodem.job;

import java.util.HashMap;
import java.util.Map;

public class ATCommandFactory {
	public static String  ECHO_OFF ="echo_off";
	public static String  CGSN ="cgsn";
	public static String  CIMI ="cimi";
	public static String  CENTER_NUMBER ="center_number";
	public static String  STORE_USED ="store_used";
	public static String  INIT ="init";
	public static String  ITEM_OK = "OK";
	
	public static final int  MODE_PDU =1;
	public static final int  MODE_TEXT =2;
	public static final int  REC_UNREAD =3;
	public static final int  REC_READ =4;
	public static final int  DELETE_READ =5;
	public static final int  TEXT_END=6;
	
	private static Map<String,ATCommand> commandMap=new HashMap<String,ATCommand>();
	static {
		commandMap.put(ECHO_OFF,  new ATCommand("ATE0\r", "OK")); //禁止回顯,當初爲了這個搞了很久
		commandMap.put(CGSN,  new ATCommand("AT+CGSN\r", "OK"));
		commandMap.put(CIMI,  new ATCommand("AT+CIMI\r", "OK"));
		commandMap.put(CENTER_NUMBER,new ATCommand("AT+CSCA?\r","OK"));
		commandMap.put(STORE_USED,new ATCommand("AT+CPMS?\r", "+CPMS:"));
		commandMap.put(INIT,new ATCommand("AT S7=45 S0=0 L1 V1 X4 &c1 E1 Q0\r", "OK"));			
	}
	public static ATCommand getOtherCommand(int flag){
		ATCommand at=null;
		switch(flag){
		case MODE_PDU:
			at=new ATCommand("AT+CMGF=0\r", "OK");
			break;
		case MODE_TEXT:
			at=new ATCommand("AT+CMGF=1\r", "OK");
			break;
		case REC_UNREAD:
			at=new ATCommand("AT+CMGL=\"REC UNREAD\"\r", "OK");
			break;
		case REC_READ:
			at=new ATCommand("AT+CMGL=\"REC READ\"\r","OK");
			break;
		case DELETE_READ:
			at=new ATCommand("AT+CMGD=0,1\r", "OK");
			break;
		case TEXT_END:
			at=new ATCommand(ATCommand.getTextEnd() + "\r", "OK");
			break;
		}
		return at;
	}
	public static ATCommand getCommand(String key){
		return commandMap.get(key);
	}
	// AT+CMGR={index}-------+CMTI: "SM",4	
	
}



AT+CPMS=?

作用:

測試命令。用於得到手機所支持的儲存位置的列表。在我的SIEMENS M55手機上返回:

AT+CPMS=?

+CPMS: ("MT","SM","ME"),("MT","SM","ME"),("MT","SM","ME")

表示手機支持MT(手機終端),SM(SIM卡),ME(手機設備)

2、語句:

AT+CPMS=”MT”,”ME”,”SIM”

設置MEM1爲MT,MEM2爲ME,MEM3爲SIM

3、語句:

AT+CPMS?

作用:

得到當前的設置。

例如通過第二條語句的設置以後返回:

AT+CPMS?

+CPMS: "MT",7,150,"ME",7,100,"SM",0,50

表示MT裏面有7條短信,總共可以儲存150條。ME裏面有7條,共可以儲存150條。SIM卡上可以儲存50條

詳細的指令請查看http://archive.cnblogs.com/a/1523556/

package com.magima.messenger.gsmmodem.job;

import java.io.IOException;
import java.io.OutputStream;

public class ATCommand {
	
	private String[] items = new String[2];
	private String[] expectedItems = new String[2];
	private int itemIndex = 0;
	private int itemCount;

	public ATCommand(String item, String expected) {
		this.itemCount = 1;
		this.items[0] = item;
		this.expectedItems[0] = expected;
	}
       //每條指令結束,回車符,這個也是試了很久纔對
	public static String getTextEnd() {
		char[] buf = new char[16];
		buf[0] = 0x1A;
		buf[1] = '\0';
		String endStr = new String(buf);		
		return endStr;
	}

	public ATCommand(String s, String expected1, String text,
			String expected2) {
		this.itemCount = 2;
		this.items[0] = "AT+CMGS=" + s + "\r";
		this.expectedItems[0] = expected1;
		this.items[1] = text + getTextEnd();
		this.expectedItems[1] = expected2;
	}

	

	public String getNextItem() {
		String item = null;
		System.out.println("command---getNextItem---itemIndex:"+itemIndex+" itemCount:" +itemCount);
		if (itemIndex >= 0 && itemIndex < itemCount){
			System.out.println("command---getNextItem:"+item);
			item = items[itemIndex];
			}
		return item;
	}

	public String getNextExpectedItem() {
		String item = null;
		if (itemIndex >= 0 && itemIndex < itemCount)
			item = expectedItems[itemIndex];
		return item;
	}

	public void nextItem() {
		itemIndex++;
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章