各位大虾:
    你们好,小弟有一事相求。小弟现在在做一个关于串口通信的软件,现在遇到了一点接收串口发过来的数据的问题!如果用事件监听的方法接收数据,如
readBuffer=new byte[29];   
      try {    
              while (comInputStream.available() > 0) {    
//                  comInputStream.read(readBuffer,0,comInputStream.available());
               int data=comInputStream.read(readBuffer);
//                  stringBuffer.append(data);
              }    
      } catch (IOException ioe) {
        System.out.println("Exception " + ioe);
      }
那么inputStream一次只能读8个字节,也就是说字节数组一次只存了inputstream读取的八个字节,如果是20几个字节就分了4次来读了,已经分段了。我的设备是一次发20几个字节的,而且都是十六进制的数据,我想请教一下各位大虾,有什么方法可以一次把所有数据读完..!急啊,谢谢各位大虾的帮忙啊,小弟不胜感激

解决方案 »

  1.   

    comInputStream.read(readBuffer,0,comInputStream.available());这个不行吗?
      

  2.   

    不行,还是分段了。具体的事件代码是:public void serialEvent(SerialPortEvent event) {

    switch (event.getEventType()) {
        case SerialPortEvent.BI:
          System.out.println("SerialPortEvent.BI occurred");
        case SerialPortEvent.OE:
          System.out.println("SerialPortEvent.OE occurred");
        case SerialPortEvent.FE:
          System.out.println("SerialPortEvent.FE occurred");
        case SerialPortEvent.PE:
          System.out.println("SerialPortEvent.PE occurred");
        case SerialPortEvent.CD:
          System.out.println("SerialPortEvent.CD occurred");
        case SerialPortEvent.CTS:
          System.out.println("SerialPortEvent.CTS occurred");
        case SerialPortEvent.DSR:
          System.out.println("SerialPortEvent.DSR occurred");
        case SerialPortEvent.RI:
          System.out.println("SerialPortEvent.RI occurred");
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
          System.out.println("SerialPortEvent.OUTPUT_BUFFER_EMPTY occurred");
          break;
        case SerialPortEvent.DATA_AVAILABLE:
          System.out.println("SerialPortEvent.DATA_AVAILABLE occurred");
          readBuffer=new byte[29];   
          try {    
                  while (comInputStream.available() > 0) {    
                      comInputStream.read(readBuffer,0,comInputStream.available());
    //               int data=comInputStream.read(readBuffer);
    //                  stringBuffer.append(data);
                  }    
          } catch (IOException ioe) {
            System.out.println("Exception " + ioe);
          }
          System.out.println(stringBuffer.toString());
          dataHandler.onReceive(readBuffer, readBuffer.length);
          break;
        }

    }
    设备发送的数据是:A3 01 1A F1 11 00 00 00 00 00 12 00 00 00 00 00 13 00 00 00 00 00 10 00 00 00 00 00 37
    接收的结果确是:
    com.nikey.SerialManager.SerialApp--receive data: A3011AF111000000000000000000000000000000000000000000000000
    com.nikey.SerialManager.SerialApp--receive data: 0000120000000000000000000000000000000000000000000000000000
    com.nikey.SerialManager.SerialApp--receive data: 1300000000001000000000000000000000000000000000000000000000
    com.nikey.SerialManager.SerialApp--receive data: 0000000037000000000000000000000000000000000000000000000000
    分成四次了.....
      

  3.   

    dataHandler.onReceive(readBuffer, readBuffer.length);方法里有一句打印语句:System.out.println(getClass().getName()+"--receive data: "+ByteArrayToHex.arryToHex(bytes));
    ByteArrayToHex.arryToHex(bytes)是我自己写的转换十六进制字符串的方法
      

  4.   

    available慎重使用 去掉调试一下 再可以试试装饰上BufferedInputStream
      

  5.   

     不用 while (comInputStream.available() > 0)  直接读取 试试
      

  6.   

    我也正在做 Java串口的项目,就是连不上,谁有例子啊,给一个
      

  7.   

    A3011AF1110000
    00001200000000
    13000000000010
    00000000370000
    既然分成了四次了,你那就分四次的去读啊...从中可以看到每次接收的是14个字符,可能设备就是14个字符一发呢?再试试吧.
      

  8.   

    case SerialPortEvent.DATA_AVAILABLE:// 当有可用数据时读取数据,并且给串口返回数据
    byte[] readBuffer = new byte[4096];
    try {
    Thread.sleep(200);//硬件发送是分段的,加一个延时就行了
    int numBytes = inputStream.read(readBuffer);