串口控制手机的简单程序

时间:2007-04-29
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:
* https://java.sun.com/products/javacomm (串口支持API)
* Hardware requirement:
* Moblie data line or IR interface (手机数据线或者红外线连接模拟成COM3/COM4)
*
* 近新购一机Siemens6618, 发现特别好用. 通过数据线发送AT指令, 可实现短消息, 通讯录等读写操作.
* 具体指令请参考手机厂商的资料.
* 本程序只是试验性质. 手机损坏后果自负.
*
* 参考工具:
* 串口监听: hdd serial monitor(https://www.hhdsoftware.com)
* 西门子6618 AT指令: https://www.my-siemens.com/com.aperto/MySiemens/Files/Addon/tt/hq/mw/hd/hd/gprs_at_commandset.pdf
* 分形手机工作室: https://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);
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()+" "+com.isCurrentlyOwned()+" "+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();
}
}


  
上一篇:积分电路
下一篇:74LS138译码器

免责声明: 凡注明来源本网的所有作品,均为本网合法拥有版权或有权使用的作品,欢迎转载,注明出处。非本网作品均来自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

相关技术资料