网站首页
IC库存
IC展台
电子资讯
技术资料
PDF文档
我的博客
IC72论坛
ic72 logo
搜索关键字: 所有资讯 行业动态 市场趋势 政策法规 新品发布 技术资讯 价格快报 展会资讯
  • 达普IC芯片交易网 > 新闻中心 > 技术信息 > 正文
  • RSS
  • 串口控制手机的简单程序
    http://www.ic72.com 发布时间:2007/4/29 11:01:50
    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:
    * (串口支持API)
    * Hardware requirement:
    * Moblie data line or IR interface (手机数据线或者红外线连接模拟成COM3/COM4)
    *
    * 最近新购一机Siemens6618, 发现特别好用. 通过数据线发送AT指令, 可实现短消息, 通讯录等读写操作.
    * 具体指令请参考手机厂商的资料.
    * 本程序只是试验性质. 手机损坏后果自负.
    *
    * 参考工具:
    * 串口监听: hdd serial monitor()
    * 西门子6618 AT指令:
    * 分形手机工作室:
    */

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



    www.ic72.com 达普IC芯片交易网
  • 行业动态
  • 市场趋势
  • 政策法规
  • 新品发布
  • Baidu

    IC快速检索:abcdefghijklmnopqrstuvwxyz0123456789
    COPYRIGHT:(1998-2010) IC72 达普IC芯片交易网
    客户服务:service@IC72.com 库存上载:IC72@IC72.com
    (北京)联系方式: 在线QQ咨询:点击这里给我发消息 联系电话:010-82614113 传真:010-82614123
    京ICP备06008810号-21 京公网安备 11010802032910 号 企业资质