import   java.io.*;   
  import   java.util.*;   
  import   javax.comm.*;   
      
    
  public   class   ReadCard   implements   Runnable,   SerialPortEventListener   {   
          static   CommPortIdentifier   portId;   
          static   Enumeration   portList;   
    
          InputStream   inputStream;   
          SerialPort   serialPort;   
          Thread   readThread;   
    
          public   static   void   main(String[]   args)   {   
                  portList   =   CommPortIdentifier.getPortIdentifiers();   
      
                  while   (portList.hasMoreElements())   {   
                          portId   =   (CommPortIdentifier)   portList.nextElement();   
                          if   (portId.getPortType()   ==   CommPortIdentifier.PORT_SERIAL)   {   
                                    if   (portId.getName().equals("COM2"))   {   
                                            ReadCard   reader   =   new   ReadCard();   
                                  }   
                          }   
                  }   
          }   
    
          public   ReadCard()   {   
                  try   {   
                          serialPort   =   (SerialPort)   portId.open("ReadCardApp",   2000);   
                  }   catch   (PortInUseException   e)   {}   
                  try   {   
                      
                          inputStream   =   serialPort.getInputStream();   
                  }   catch   (IOException   e)   {}   
  try   {   
                          serialPort.addEventListener(this);   
  }   catch   (TooManyListenersException   e)   {}   
                  serialPort.notifyOnDataAvailable(true);   
                  try   {   
                          serialPort.setSerialPortParams(9600,   
                                  SerialPort.DATABITS_8,   
                                  SerialPort.STOPBITS_1,   
                                  SerialPort.PARITY_NONE);   
                  }   catch   (UnsupportedCommOperationException   e)   {}   
                  readThread   =   new   Thread(this);   
                  readThread.start();   
          }   
    
          public   void   run()   {   
                  try   {   
                          Thread.sleep(20000);   
                  }   catch   (InterruptedException   e)   {}   
          }   
    
          public   void   serialEvent(SerialPortEvent   event)   {   
                  switch(event.getEventType())   {   
                  case   SerialPortEvent.BI:   
                  case   SerialPortEvent.OE:   
                  case   SerialPortEvent.FE:   
                  case   SerialPortEvent.PE:   
                  case   SerialPortEvent.CD:   
                  case   SerialPortEvent.CTS:   
                  case   SerialPortEvent.DSR:   
                  case   SerialPortEvent.RI:   
                  case   SerialPortEvent.OUTPUT_BUFFER_EMPTY:   
                          break;   
                  case   SerialPortEvent.DATA_AVAILABLE:   
                          byte[]   readBuffer   =   new   byte[20];   
      
                          try   {   
                                  while   (inputStream.available()   >   0)   {   
                                          int   numBytes   =   inputStream.read(readBuffer);   
                                      }   
                                    System.out.println(   new   String(readBuffer)   +   "end");   
                                      
                          }   catch   (IOException   e)   {}   
                          break;   
                  }   
          }   
  } 
小弟在CSDN上找的例子 例子上的错误是每次接收数据时没有等到接收完全部数据 就已经结束了
参考 之前没有设enableReceiveThreshold(int   x) 所以程序在自动读取几个字符后就返回一次了、、
小弟新手 没有弄懂 请指教...

解决方案 »

  1.   

    我得好像也是lz的这个问题,网上说有可能是两种问题:
    comm。jar文件配置不正确,但是经过我仔细验证,不大像。
    还有便是enableReceiveThreshold(int x)及enableReceiveTimeout(int x)语句,可我加上之后还是存在这样的问题。
    也有说就是一次只能接受8byte数据一次,多的要重复读入。
    我现在的暂时处理方法是设置一个缓冲,通过程序逻辑将重复读入的数据组织一起放在缓冲中。在前台读去缓冲之后,将缓冲清掉,等待下次的输入。
      

  2.   

    我最后的理解是按照这个帖自上面的:http://topic.csdn.net/u/20090121/11/68EC5ADF-8EC2-4C30-B9C4-7749D843AAE7.html