test.java
package steeven.mobile;
import javax.comm.*;
/**
* <p>title: steeven mobile</p>
* <p>description: </p>
* <p>copyright: copyright (c) 2002</p>
* <p>company: konamish</p>
* @author phpme@citiz.net
* @version 1.0
*
* software requirement:
* http://java.sun.com/products/javacomm (串口支持api)
* hardware requirement:
* moblie data line or ir interface (手机数据线或者红外线连接模拟成com3/com4)
*
* 最近新购一机siemens6618, 发现特别好用. 通过数据线发送at指令, 可实现短消息, 通讯录等读写操作.
* 具体指令请参考手机厂商的资料.
* 本程序只是试验性质. 手机损坏后果自负.
*
* 参考工具:
* 串口监听: hdd serial monitor(http://www.hhdsoftware.com)
* 西门子6618 at指令: http://www.my-siemens.com/com.aperto/mysiemens/files/addon/tt/hq/mw/hd/hd/gprs_at_commandset.pdf
* 分形手机工作室: http://fractal.longcity.net
*/
public class test implements javax.comm.serialporteventlistener{
private string portname = "com1";
private javax.comm.serialport port = null;
private java.io.inputstream in;
private java.io.outputstream out;
private boolean debug = true;
public test() throws exception{
init();
write("at+cgmi"); //厂商
readwait();
write("at+cgmm"); //型号
readwait();
write("at+cpbr=1"); //第一条通讯录
readwait();
//从控制台输入指令, 回车结束, exit退出
java.io.bufferedreader r = new java.io.bufferedreader(new java.io.inputstreamreader(system.in));
string input;
do{
input = r.readline();
if (input.equalsignorecase("exit"))
break;
write(input);
readwait();
}while(true);
close();
}
public string read() throws exception{
int size = in.available();
if (size<1)
return null;
byte[] input = new byte[size];
in.read(input,0,size);
string ret = new string(input);
system.out.print("read: \t"+byte2hex(input)+"\n"+ret);
system.out.println("=================================");
return ret;
}
public string readwait()throws exception{
while (in.available()<1)
thread.sleep(20);
string input = read();
return input;
}
public void write(string cmd) throws exception{
cmd += "\r";
out.write((cmd).getbytes());
system.out.println("write: "+byte2hex(cmd.getbytes())+"\t"+cmd);
}
private void init() throws exception{
javax.comm.commportidentifier portid = commportidentifier.getportidentifier(portname);
port = (serialport)portid.open(this.getclass().getname(), 2000);
in = port.getinputstream();
out = port.getoutputstream();
port.setserialportparams(115200,port.databits_8,port.stopbits_1,port.parity_none);
// port.enablereceiveframing(10);
port.enablereceivetimeout(2000);
// port.addeventlistener(this);
mobileinit();
}
private void mobileinit() throws exception{
write("at");
string output;
do{
thread.sleep(20);
output = read();
if (output != null){
system.out.println("output:"+output);
break;
}
write("at");
}while(true);
}
public void close() throws java.io.ioexception{
in.close();
out.close();
port.close();
}
public static void showports(){
java.util.enumeration all = javax.comm.commportidentifier.getportidentifiers();
while (all.hasmoreelements()){
javax.comm.commportidentifier com = (javax.comm.commportidentifier)all.nextelement();
system.out.println(com.getname()+"\t"+com.iscurrentlyowned()+"\t"+com.getcurrentowner());
}
}
//字节码转换成16进制字符串
public static string byte2hex(byte[] b) {
string hs="";
string stmp="";
for (int n=0;n<b.length;n++){
stmp=(java.lang.integer.tohexstring(b[n] & 0xff));
if (stmp.length()==1)
hs=hs+"0"+stmp;
else hs=hs+stmp;
if (n<b.length-1) hs=hs+":";
}
return hs.touppercase();
}
public void serialevent(serialportevent event){
system.out.println("event:"+event.geteventtype());
try{
if (event.geteventtype() == event.data_available){
system.out.println("read string:"+read());
}
}catch(exception e){
e.printstacktrace();
}
}
public static void main(string[] args) throws exception {
new test();
}
}
