look at here,
http://www-900.ibm.com/developerWorks/cn/java/joy-comm/index.shtml

解决方案 »

  1.   

    谢谢webwing(kevin)
    那篇文章是您写的吗?真是太棒啦!
    有并口控制的吗?如果有就更好啦!
      

  2.   

    哪儿有javax.comm.*呢?
    我的jdk1.4 apidocs好象没有看到这个包.
      

  3.   

    我找到啦:
    http://java.sun.com/products/javacomm/javadocs/packages.html
      

  4.   

    COMM API 可以从SUN的站点上下载。我作过串口通信的程序,用的是GSM MODEM。
    做串口或者是并口操作时,关键是作好串口或者并口事件的处理。以串口为例说明:
    //这是其中关键的代码,并不全,希望你能获益
    import java.io.*;
    import javax.comm.*;
    import java.util.*;
    public class SerialPortHandle extends Thread implements SerialPortEventListener {
       BufferedReader br;
       boolean OVER=false;
       Vector index=new Vector(5,1);
       Vector response=new Vector(10,1);
      public SerialPortHandle() {
      }
       //得到计算机的串口
      public   SerialPort getSerialPort(String com)
      {
         SerialPort sPort=null;
         CommPortIdentifier portID;
         String owner=new String("modemn");
         int keeptime=1000;
         Enumeration portList;
         portList=CommPortIdentifier.getPortIdentifiers();     //如果有多个端口
         while(portList.hasMoreElements())
          {
              portID=(CommPortIdentifier)portList.nextElement();
              if(portID.getName().equals(com))
                   try{sPort=(SerialPort)portID.open(owner,keeptime);}//打开一个串口
                      catch(PortInUseException e){System.out.println(e.getMessage());}     }//while   // System.out.println("serial name is :"+sPort.getName());
        try{
          //设置串口的参数
          sPort.setSerialPortParams(9600,//波特率
                     SerialPort.DATABITS_8,//数据位数
                    SerialPort.STOPBITS_1, //停止位
                    SerialPort.PARITY_NONE);//奇偶位
            }catch (UnsupportedCommOperationException e) {System.out.println(e.getMessage());}
    /*
          int baudrate=sPort.getBaudRate();
          int databits=sPort.DATABITS_8;
          int parity=sPort.getParity();
          int stopbits=sPort.getStopBits();
           int flowcontrolMode=sPort.getFlowControlMode();
          System.out.println("baudrate:"+baudrate);
          System.out.println("databits:"+databits);
          System.out.println("parity:"+parity);
          System.out.println("stopbits:"+stopbits);
          System.out.println("flow control mode is:"+flowcontrolMode);
    */
        return sPort;
       }public void sendMsg(String cmd ,SerialPort sPort)
      {    PrintWriter pw;    //write command(including msg)into SIM card
       try{       pw=new PrintWriter(sPort.getOutputStream());
           pw.println(cmd);
           pw.flush();
           pw.close();
           System.out.println("msg has been send from Modemn");
           }
          catch(IOException e){System.out.println("catch exception when send msg from Modemn:"+e.getMessage());}
        try{
           br=new BufferedReader(new InputStreamReader(sPort.getInputStream()));       sPort.addEventListener(this);
           sPort.notifyOnDataAvailable(true);
           }
          catch(TooManyListenersException e){System.out.println(e.getMessage());}
          catch(IOException e){}  }public void  getMsgFromModemn(String cmd,SerialPort sPort)
       {    PrintWriter pw=null;    //把让数据猫接收数据的命令传到数据猫
        try{
            pw=new PrintWriter(sPort.getOutputStream());
            pw.println(cmd);
            pw.flush();
            pw.close();
            // System.out.println("the cmd that read msg from modemn had been send");        br=new BufferedReader(new InputStreamReader(sPort.getInputStream()));
           sPort.addEventListener(this);
           sPort.notifyOnDataAvailable(true);
          }
          catch(IOException e){System.out.println(e.getMessage());}
          catch(TooManyListenersException e){
                        System.out.println("catch exception when receiving msg from modemn");
                        System.out.println(e.getMessage());}   }
     public void run()
    {
    //代码省略
     } //处理侦听到的串口事件
       public synchronized void serialEvent(SerialPortEvent ev)
       {      //如果有串口事件发生
        if(ev.getEventType()==ev.DATA_AVAILABLE)     try{
              while(true){
                respmsg=br.readLine();//在打开的读数据流中读一行数据
                //System.out.println("repmsg:"+respmsg);
                  //如果读到字符串“OK”,说明数据猫的响应已经结束
                if(respmsg.equals("OK"))
                 {              // System.out.println("the  end!");
                   break;}//关闭读数据流          }//while
         }//try
        catch(IOException e){System.out.println("catch exception when receive msg from modemn");
                         System.out.println(e.getMessage());}
    }
      

  5.   

    COMM API 可以从SUN的站点上下载。我作过串口通信的程序,用的是GSM MODEM。
    做串口或者是并口操作时,关键是作好串口或者并口事件的处理。以串口为例说明:
    //这是其中关键的代码,并不全,希望你能获益
    import java.io.*;
    import javax.comm.*;
    import java.util.*;
    public class SerialPortHandle extends Thread implements SerialPortEventListener {
       BufferedReader br;
       boolean OVER=false;
       Vector index=new Vector(5,1);
       Vector response=new Vector(10,1);
      public SerialPortHandle() {
      }
       //得到计算机的串口
      public   SerialPort getSerialPort(String com)
      {
         SerialPort sPort=null;
         CommPortIdentifier portID;
         String owner=new String("modemn");
         int keeptime=1000;
         Enumeration portList;
         portList=CommPortIdentifier.getPortIdentifiers();     //如果有多个端口
         while(portList.hasMoreElements())
          {
              portID=(CommPortIdentifier)portList.nextElement();
              if(portID.getName().equals(com))
                   try{sPort=(SerialPort)portID.open(owner,keeptime);}//打开一个串口
                      catch(PortInUseException e){System.out.println(e.getMessage());}     }//while   // System.out.println("serial name is :"+sPort.getName());
        try{
          //设置串口的参数
          sPort.setSerialPortParams(9600,//波特率
                     SerialPort.DATABITS_8,//数据位数
                    SerialPort.STOPBITS_1, //停止位
                    SerialPort.PARITY_NONE);//奇偶位
            }catch (UnsupportedCommOperationException e) {System.out.println(e.getMessage());}
    /*
          int baudrate=sPort.getBaudRate();
          int databits=sPort.DATABITS_8;
          int parity=sPort.getParity();
          int stopbits=sPort.getStopBits();
           int flowcontrolMode=sPort.getFlowControlMode();
          System.out.println("baudrate:"+baudrate);
          System.out.println("databits:"+databits);
          System.out.println("parity:"+parity);
          System.out.println("stopbits:"+stopbits);
          System.out.println("flow control mode is:"+flowcontrolMode);
    */
        return sPort;
       }public void sendMsg(String cmd ,SerialPort sPort)
      {    PrintWriter pw;    //write command(including msg)into SIM card
       try{       pw=new PrintWriter(sPort.getOutputStream());
           pw.println(cmd);
           pw.flush();
           pw.close();
           System.out.println("msg has been send from Modemn");
           }
          catch(IOException e){System.out.println("catch exception when send msg from Modemn:"+e.getMessage());}
        try{
           br=new BufferedReader(new InputStreamReader(sPort.getInputStream()));       sPort.addEventListener(this);
           sPort.notifyOnDataAvailable(true);
           }
          catch(TooManyListenersException e){System.out.println(e.getMessage());}
          catch(IOException e){}  }public void  getMsgFromModemn(String cmd,SerialPort sPort)
       {    PrintWriter pw=null;    //把让数据猫接收数据的命令传到数据猫
        try{
            pw=new PrintWriter(sPort.getOutputStream());
            pw.println(cmd);
            pw.flush();
            pw.close();
            // System.out.println("the cmd that read msg from modemn had been send");        br=new BufferedReader(new InputStreamReader(sPort.getInputStream()));
           sPort.addEventListener(this);
           sPort.notifyOnDataAvailable(true);
          }
          catch(IOException e){System.out.println(e.getMessage());}
          catch(TooManyListenersException e){
                        System.out.println("catch exception when receiving msg from modemn");
                        System.out.println(e.getMessage());}   }
     public void run()
    {
    //代码省略
     } //处理侦听到的串口事件
       public synchronized void serialEvent(SerialPortEvent ev)
       {      //如果有串口事件发生
        if(ev.getEventType()==ev.DATA_AVAILABLE)     try{
              while(true){
                respmsg=br.readLine();//在打开的读数据流中读一行数据
                //System.out.println("repmsg:"+respmsg);
                  //如果读到字符串“OK”,说明数据猫的响应已经结束
                if(respmsg.equals("OK"))
                 {              // System.out.println("the  end!");
                   break;}//关闭读数据流          }//while
         }//try
        catch(IOException e){System.out.println("catch exception when receive msg from modemn");
                         System.out.println(e.getMessage());}
    }
      

  6.   

    http://www-900.ibm.com/developerWorks/cn/java/joy-comm/index.shtml这片文章我也看过,但是我觉得作者根本就没有实际的在机器上实验过,我那他的原程序实验结果根本得不到想要的结果。最后我查资料发现,他根本就没有多串口事件做处理,不知道他说的直接一连是怎样实现的。