?? listenth.java
字號:
package GEOSMS;
import java.io.InputStream;
import java.io.OutputStream;
public class ListenTh extends Thread{
private InputStream input;
private OutputStream out;
private GeoSmsDM dm;
public boolean thread_contral;
private SMS sms;
public ListenTh(InputStream sinput,OutputStream soutput)
{
input=sinput; //串口讀入流
out=soutput; //串口寫出流
thread_contral=true; //線程循環(huán)控制參數(shù)
dm=new GeoSmsDM(); //創(chuàng)建數(shù)據(jù)操作對象
sms=new SMS(input,out); //創(chuàng)建短信操作對象
}
public void setThreadClose()
{
thread_contral=false; //關(guān)閉線程循環(huán)控制參數(shù)
}
/**在本線程中監(jiān)控串口,若有SMSR命令,則表示dtss800模塊接受到新短信;
* 使用AT+RSMS=0讀取并刪除該短信;
* 同時將短信內(nèi)容發(fā)送到數(shù)據(jù)處理對象中處理;
* 將處理的結(jié)果發(fā)送回源號碼。
*/
public void run()
{
char c;
byte[] buffer=new byte[200];
String str="";
int i=0,j=0,ptr=0,len;
String Msg="";
while(thread_contral){ //線程主循環(huán)開始
try{
len=input.read(buffer); //從串口讀取字節(jié)流
str=new String(buffer);
for(i=0;i<buffer.length;i++)
System.out.print(Integer.toHexString(buffer[i])+" ");
System.out.println(" ");
System.out.println(str);
Thread.sleep(20);
if(ptr==1) //判斷短信讀取狀態(tài)
{j=1;ptr=0;}
if(str.indexOf("SMSR")>0) //讀取到SMSR,即轉(zhuǎn)換為接收短信狀態(tài)
{
System.out.println("SMSR");
String cmd="AT+RSMS\r"; //發(fā)送讀取短信命令
// String cmd="AT+SMSR\r";
System.out.println(cmd);
out.write(cmd.getBytes());
ptr=1; //轉(zhuǎn)換短信讀取狀態(tài)為:預(yù)備讀取態(tài)
}
System.out.println(" ");
//j=1;
if(j==1){
sms.ReadSmsByte(buffer);
String result="";
if(sms.getCharSet()=="UNICODE") //判斷短信字符集
result=new String(sms.getSmsData(buffer,len),"utf-16BE");
if(sms.getCharSet()=="ASCII")
result=new String(sms.getSmsData(buffer,len));
System.out.println("result="+result);
String StrNum=sms.getTelNumber(); //獲取源電話號碼
System.out.println("number="+StrNum);
String SMS=dm.lookingfor(result);//執(zhí)行數(shù)據(jù)服務(wù)邏輯
sms.SendSms(StrNum,SMS); //發(fā)送結(jié)果
j=0; //重置控制標識
}
}catch(Exception e){e.printStackTrace();}
} //線程主循環(huán)結(jié)束
System.out.println("thread end");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -