RXTX java,inputStream 不返回所有缓冲区 [英] RXTX java, inputStream does not return all the buffer

查看:47
本文介绍了RXTX java,inputStream 不返回所有缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我使用的是 rxtx.>

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;
    }

其余代码与这个相同,我只是改变了 2 件事.

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();

现在不存在...

我正在发送这个(每秒)

I'm sending this (each second)

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

这就是我得到的:

123456789123
456789
123456789
1234567891
23456789

有人可以帮我吗?

编辑

后来,我找到了更好的方法来解决它.谢谢,这是阅读代码

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;
    }

我无法消除睡眠,但这不是问题,谢谢veer

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

推荐答案

你确实应该注意到 InputStream.available 定义如下...

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.

如您所见,这不是您所期望的.相反,您要检查流结束,这由 InputStream.read() 返回 -1.

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;
}

这应该会强制它读取最多 20 个字节,在到达流末尾的情况下会减少.

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

特别感谢 EJP 提醒我保持帖子的质量并确保它们是正确的.

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

这篇关于RXTX java,inputStream 不返回所有缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆