Search code examples
javaarduinorxtx

Discarding data in a buffered reader


I am sending sensor data via serial from my arduino to a java program using the rxtx library. The issue being that new values are pushed so fast on into a BufferedReader that I am not able to process these in real time. I moved away from event based implementation over to polling every 100 ms and want to discard all the values in between.

Is there a possibility to clear a buffered reader without knowing exactly how many bytes I have to skip? 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.

It would be fine to read a value clear the buffered reader, wait for 100 ms and read the oldest values again. 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.


Solution

  • 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.

    How slow is the computer java is running on that this speed concerns you? PCs are generally several orders of magnitude faster than the serial line on an 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.

    Probably the best option. If you need higher speeds later then update the code on the Arduino.