Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you flush a Java serial InputStream?

I'm using JavaComm, and getting the inputStream from the serial port object. I have a problem in that sometimes when the system starts up there are noise characters in the buffer that I do not want. I want to initialize the serial port then somehow flush the input of all data before I begin my processing.

Is there any way to do that?

TY Fred

like image 297
fred basset Avatar asked Mar 11 '26 14:03

fred basset


2 Answers

How would a "flush()" method know what is garbage and what is real data? Only you know, and your program should be prepared to ignore leading garbage.

EDIT: If you can be absolutely certain that all data in the input buffer prior to sending the request is garbage, then just read until there is no more input data -- i.e. flush it yourself.

like image 182
Jim Garrison Avatar answered Mar 14 '26 04:03

Jim Garrison


Before sending any requests to your device read all that you can. Once there's nothing left to read you can start sending requests.

like image 33
Laurence Gonsalves Avatar answered Mar 14 '26 06:03

Laurence Gonsalves