文件名称:java发送短信之AT指令
文件大小:43KB
文件格式:RAR
更新时间:2015-11-29 15:50:20
AT 短信猫
短信内容的存储类
/***
* CommonSms 短信用于全局变量
*/
public class CommonSms{
/** id */
private int id;
/**短信内容*/
private String smstext;
/**短信发送方*/
private String sender;//短信发送方
/**短信接收发*/
private String recver;//短信接收发
/**时间*/
private Date date;
public String getSmstext() {
return smstext;
}
public void setSmstext(String smstext) {
this.smstext = smstext;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getRecver() {
return recver;
}
public void setRecver(String recver) {
this.recver = recver;
}
}
串口操纵实现类
/***
* 串口操纵实现类
*/
public class Port {
private CommPortIdentifier portId;
private SerialPort serialPort;
private OutputStreamWriter out;
private InputStreamReader in;
private String COMname;
private static char symbol1 = 13;
public String getCOMname() {
return COMname;
}
public void setCOMname(String mname) {
COMname = mname;
}
public CommPortIdentifier getPortId() {
return portId;
}
public void setPortId(CommPortIdentifier portId) {
this.portId = portId;
}
public SerialPort getSerialPort() {
return serialPort;
}
public void setSerialPort(SerialPort serialPort) {
this.serialPort = serialPort;
}
public OutputStreamWriter getOut() {
return out;
}
public void setOut(OutputStreamWriter out) {
this.out = out;
}
public InputStreamReader getIn() {
return in;
}
public void setIn(InputStreamReader in) {
this.in = in;
}
public boolean isused =true;
public boolean isIsused() {
return isused;
}
public void setIsused(boolean isused) {
this.isused = isused;
}
/**
* 打开com口
* @param portName
* @return
*/
public Port(String portName) {
try {
portId = CommPortIdentifier.getPortIdentifier(portName);
if (portId == null) {
System.out.println("port is null");
}
try {
serialPort = (SerialPort) portId.open(portName,100000);
} catch (PortInUseException e) {
System.gc();
e.printStackTrace();
}
// 下面是得到用于和COM口通讯的输进、输出流。
try {
in = new InputStreamReader(serialPort.getInputStream());
out = new OutputStreamWriter(serialPort.getOutputStream());
} catch (IOException e) {
System.gc();
System.out.println("IOException");
}
// 下面是初始化COM口的传输参数,如传输速率:9600等。
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
setCOMname(portId.getName());
setIsused(true);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
System.gc();
}
} catch (NoSuchPortException e) {
e.printStackTrace();
System.gc();
}
}
/**
* 检查SIM是否存在
* @return
*/
public boolean chakanPort() {
try {
String atCommand = "AT+ccid";
String strReturn = sendAT(atCommand);
if (strReturn.indexOf("OK", 0) != -1) {
return true;
}
return false;
} catch (Exception ex) {
System.gc();
ex.printStackTrace();
return false;
}
}
/**
* 封闭COM口
* @return boolean
*/
public void close() {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
serialPort.close();
System.gc();
setIsused(false);
}
/**
* 向串口中写进字符串命令
* @param s 字符串命令
* @throws Exception 异常
*/
public void writeln(String s) throws Exception {
out.write(s);
out.write('\r');
out.flush();
}
/**
* 读取COM命令的返回字符串
* @return 结果字符串
* @throws Exception
*/
public String read() throws Exception {
int n, i;
char c;
String answer = "";
for (i = 0; i < 100; i++) {
while (in.ready()) {
n = in.read();
if (n != -1) {
c = (char) n;
answer = answer + c;
Thread.sleep(1);
} else
break;
}
if (answer.indexOf("OK") != -1) {
break;
}
Thread.sleep(100);
}
return answer;
}
/**
* 向串口发送AT指令
* @param atcommand 指令内容
* @return 指令返回结果
* @throws java.rmi.RemoteException
*/
public String sendAT(String atcommand) throws java.rmi.RemoteException {
String s = "";
try {
Thread.sleep(100);
writeln(atcommand);
Thread.sleep(80);
s = read();
Thread.sleep(100);
} catch (Exception e) {
System.gc();
System.out.println("ERROR: send AT command failed; " + "Command: "
+ atcommand + "; Answer: " + s + " " + e);
}
return s;
}
}
短信操纵类
/***
* 短信操纵类
*/
public class Sms{
private CommonSms commonsms;
private static char symbol1 = 13;
private static String strReturn = "", atCommand = "";
public boolean SendSms(Port myport) {
if(!myport.isIsused())
{
System.out.println("COM通讯端口未正常打开!");
return false;
}
setMessageMode(myport,1);
// 空格
char symbol2 = 34;
// ctrl~z 发送指令
char symbol3 = 26;
try {
atCommand = "AT+CSMP=17,169,0,08" + String.valueOf(symbol1);
strReturn = myport.sendAT(atCommand);
System.out.println(strReturn);
if (strReturn.indexOf("OK", 0) != -1) {
atCommand = "AT+CMGS=" + commonsms.getRecver()
+ String.valueOf(symbol1);
strReturn = myport.sendAT(atCommand);
atCommand = StringUtil.encodeHex(commonsms.getSmstext().trim())
+ String.valueOf(symbol3) + String.valueOf(symbol1);
strReturn = myport.sendAT(atCommand);
if (strReturn.indexOf("OK") != -1
&& strReturn.indexOf("+CMGS") != -1) {
System.out.println("短信发送成功...");
return true;
}
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("短信发送失败...");
return false;
}
System.out.println("短信发送失败...");
return false;
}
/**
* 设置消息模式
* @param op
* 0-pdu 1-text(默认1 文本方式 )
* @return
*/
public boolean setMessageMode(Port myport,int op) {
try {
String atCommand = "AT+CMGF=" + String.valueOf(op)
+ String.valueOf(symbol1);
String strReturn = myport.sendAT(atCommand);
if (strReturn.indexOf("OK", 0) != -1) {
System.out.println("*************文本方式设置成功************");
return true;
}
return false;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
/**
* 读取所有短信
* @return CommonSms集合
*/
public List
【文件预览】:
AT指令_sms
----.project(1KB)
----.mymetadata(282B)
----src()
--------com()
----WebRoot()
--------WEB-INF()
--------index.jsp(834B)
--------META-INF()
----.myeclipse()
----.settings()
--------org.eclipse.wst.jsdt.ui.superType.container(49B)
--------org.eclipse.wst.jsdt.ui.superType.name(6B)
--------org.eclipse.core.resources.prefs(176B)
--------.jsdtscope(500B)
----.classpath(429B)