丢弃缓冲 reader 中的数据

Discarding data in a buffered reader

我正在使用 rxtx 库通过串口将传感器数据从我的 arduino 发送到 java 程序。问题是新值被如此快速地推送到 BufferedReader 中,以至于我无法实时处理这些值。我从基于事件的实现转向每 100 毫秒轮询一次,并希望丢弃其间的所有值。

是否可以在不知道我必须跳过多少字节的情况下清除缓冲的 reader?在缓冲区准备好时调用 read Line 的 while 循环看起来非常乏味,我不确定它是否足够快。

读取一个值清除缓冲reader,等待 100 毫秒并再次读取最旧的值会很好。另一种可能性是限制 arduino 发送数据的速度,但这是我最不喜欢的方法,因为将来我可能会依赖更高的速度。

A while loop with calling read Line while the buffer is ready seems very tedious and I am not sure if it would be fast enough.

电脑有多慢java运行这个速度跟你有关系吗? PC 通常比 Arduino 上的串行线快几个数量级。

Another possibility would be to limit the speed the arduino sends out the data but this is my least favorite approach as in the future I might be reliant on higher speeds.

可能是最好的选择。如果您以后需要更高的速度,请更新 Arduino 上的代码。