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++;
	}
}


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