本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7630200
建立时间:2006年5月29日




[J2ME]通过串口控制手机的简单程序
软件技术,  电脑与网络

lhwork 发表于 2006/6/8 17:48:08

Test.javapackage 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();  }}


阅读全文(3925) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 1.000 second(s), page refreshed 144811058 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号