-->

RXTX java, inputStream does not return all the buf

2019-09-01 04:16发布

问题:

This is my code, I'm using rxtx.

public void Send(byte[] bytDatos) throws IOException {
        this.out.write(bytDatos);
    }

    public byte[] Read() throws IOException {

        byte[] buffer = new byte[1024];

        int len = 20;

        while(in.available()!=0){
            in.read(buffer);
        }

        System.out.print(new String(buffer, 0, len) + "\n");

        return buffer;
    }

the rest of code is just the same as this, i just changed 2 things.

InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();

They are global variables now and...

(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();

not exist now...

I'm sending this (each second)

Send(("123456789").getBytes());

And this is what i got:

123456789123
456789
123456789
1234567891
23456789

can anybody help me?

EDIT

Later, i got the better way to solve it. Thanks, this was the Read Code

public byte[] Read(int intEspera) throws IOException {

        try {
            Thread.sleep(intEspera);
        } catch (InterruptedException ex) {
            Logger.getLogger(COM_ClComunica.class.getName()).log(Level.SEVERE, null, ex);
        }//*/

        byte[] buffer = new byte[528];

        int len = 0;


        while (in.available() > 0) {
            len = in.available();
            in.read(buffer,0,528);
        }

        return buffer;
    }

It was imposible for me to erase that sleep but it is not a problem so, thanks veer

回答1:

You should indeed note that InputStream.available is defined as follows...

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

As you can see, this is not what you expected. Instead, you want to check for end-of-stream, which is indicated by InputStream.read() returning -1.

In addition, since you don't remember how much data you have already read in prior iterations of your read loop, you are potentially overwriting prior data in your buffer, which is again not something you likely intended.

What you appear to want is something as follows:

private static final int MESSAGE_SIZE = 20;

public byte[] read() throws IOException {
  final byte[] buffer = new byte[MESSAGE_SIZE];
  int total = 0;
  int read = 0;
  while (total < MESSAGE_SIZE
            && (read = in.read(buffer, total, MESSAGE_SIZE - total)) >= 0) {
    total += read;
  }
  return buffer;
}

This should force it to read up to 20 bytes, less in the case of reaching the end of the stream.

Special thanks to EJP for reminding me to maintain the quality of my posts and make sure they're correct.



回答2:

Get rid of the available() test. All it is doing is telling you whether there is data ready to be read without blocking. That isn't the same thing as telling you where an entire message ends. There are few correct uses for available(), and this isn't one of them.

And advance the buffer pointer when you read. You need to keep track of how many bytes you have read so far, and use that as the 2nd parameter to read(), with buffer.length as the third parameter.