fred basset
fred basset

Reputation: 10082

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

Upvotes: 3

Views: 7218

Answers (2)

Jim Garrison
Jim Garrison

Reputation: 86774

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.

Upvotes: 5

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143154

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

Upvotes: 2

Related Questions