Skip to content

InputStream either hangs forever or SerialPortTimeputException is thrown #607

Description

@Nathipha

I'm using the code from this tutorial to read the answer from a COM device:

StringBuilder sb = new StringBuilder();
try {
    for(int j = 0; j < 1000; ++j) {
        char c = (char) in.read(); //InputStream in = port.getInputStream();
        System.out.print(c);
        sb.append(c);
    }
    in.close();
} catch (Exception e) {
    e.printStackTrace();
}
return sb.toString();

The print for my first command looks like this (yes, there are return characters):


OK

The problem is that the InputStream hangs/stalls after this (already tried to give it a minute), so the answer is never returned.

I've also tried a version with a ByteArrayOutputStream:
for(int length; (length = in.read(buffer)) != -1;)

Same thing here, it just stalls and never leaves the loop. The answer contains a bunch of \r or \n for some reason, so I can't just break once one of those appears (at j!=0).

I was using

port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);

and changed it to

port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 5*1000, 5*1000);

Now it still prints "\nOK\n" and returns "\nOK\n" but also throws a SerialPortTimeoutException after 5 seconds.

With TIMEOUT_NONBLOCKING: Empty answer and instant exception.

What can I do here?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions