byte[] readBuffer = new byte[2048]; int n = 0;
try { while (inputStream.available() > 0) {
if (inputStream.available() > 0 == false) {
continue;
} else {
}
n = inputStream.read(readBuffer);
}主要就是这里,本人目前做串口通信,下位机发送数据是;40毫秒,260字节左右的速度发送上来,但是试了很多方法都是InputStream处理不来,导致数据错乱;求破除;

解决方案 »

  1.   

    不要用inputStream.available() > 0来判断数据大小
    你可以参考下面的方法转换一下
    http://blog.csdn.net/hjgzj/article/details/78831241
    最后用dataBytes.length来获取大小
      

  2.   

    试试这样的
    public static byte[] readStream(InputStream inStream) throws Exception {  
        int count = 0;  
        while (count == 0) {  
            count = inStream.available();  
        }  
        byte[] b = new byte[count];  
        inStream.read(b);  
        return b;  
    }  
      

  3.   

    public static byte[] readStream(InputStream inStream) throws Exception {  
        int count = 0;  
        while (count == 0) {  
            count = inStream.available();  
        }  
        byte[] b = new byte[count];  
        inStream.read(b);  
        return b;  
    }   
    //引用楼上的回答,因为在你数据传输时,可能是分批传送的,像楼主你那么写,就可能产生数据错乱,所以你每次估计读到多少字节数,就输入多少。
      

  4.   

    用BufferInstream实时
      

  5.   

    能具体点吗?串口的 inStream只能放byte数组
      

  6.   

    用NIO
      

  7.   

    试过了,串口用NIO报错。。还是说我打开方式不对?