通过串口控制手机的简单程序

发表于:2007-07-01来源:作者:点击数: 标签:
Test.java package steeven.mobile; import javax.comm.*; /** * pTitle: steeven mobile/p * pDescription: /p * pCopyright: Copyright (c) 2002/p * pCompany: konamish/p * @author php me@citiz.net * @version 1.0 * * Software requirement: *http://j
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();
  }
}

原文转自:http://www.ltesting.net