How do you flush a Java serial InputStream?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 14:39:29

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4454961/how-do-you-flush-a-java-serial-inputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!