inputstream.available() is 0 always

后端 未结 3 684
感动是毒
感动是毒 2020-12-11 16:17

I have no idea of what is happening to my code. i am getting no errors and no response as well. I am writing the data to the serialport and waiting for the response by activ

相关标签:
3条回答
  • 2020-12-11 16:48

    .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

    0 讨论(0)
  • 2020-12-11 17:00

    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

    0 讨论(0)
  • 2020-12-11 17:10

    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.

    0 讨论(0)
提交回复
热议问题