inputstream.available() is 0 always

感情迁移 提交于 2019-11-29 13:27:41
Peter Knego

.available() can not be used in inter-process communication (serial included), since it only checks if there is data available (in input buffers) in current process.

In serial communication, when you send a messaga and then immediately call available() you will mostly get 0 as serial port did not yet reply with any data.

The solution is to use blocking read() in a separate thread (with interrupt() to end it):

Thread interrupt not ending blocking call on input stream read

To partially answer your question.

From the javadocs

The available method for class InputStream always returns 0.

http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html#available()

So that part at least is as expected

By using a PrintStream you are suppressing exceptions that you need to know about in any request/response scenario.

Most probably you haven't even sent anything yet.

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