 |
建站必读 |
 |
|
|
 |
|
 |
|
| |
| 当前位置:首页 -> 建站必读 -> JSP技术 |
|
通过串口控制手机的简单程序 |
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: "+byte2hex(input)+"
"+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 += " ";
out.write((cmd).getBytes());
System.out.println("write: "+byte2hex(cmd.getBytes())+" "+cmd);
}
private void init() throws Exception{
javax.comm.CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName);
port = (SerialPort)portId.open(this.getClass().getName(), 2000);
&nbs |
| |
|
| |
本站关键词: |
|
|
|
|
 |
|
 |
|