Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read timeout becomes immediate after a number of timed out read attempts #575

Open
craigraw opened this issue Oct 7, 2024 · 1 comment

Comments

@craigraw
Copy link

craigraw commented Oct 7, 2024

I am finding that a configured read timeout is not respected when communicating with an ESP32 device. The code is fairly straightforward:

serialPort.openPort();
serialPort.setComPortParameters(115200, 8, 1, 0);
serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 500, 1000);
serialPort.clearRTS();
serialPort.clearDTR();

//write to port, then wait for response as follows:

InputStream in = serialPort.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] readBuffer = new byte[64];
int numBytes;

while(true) {
    long time = System.currentTimeMillis();
    try {
        while((numBytes = in.read(readBuffer)) != -1) {
            if(numBytes > 0) {
                baos.write(readBuffer, 0, numBytes);
            }
        }
    } catch(SerialPortTimeoutException e) {
        System.out.println("Timed out after " + (System.currentTimeMillis() - time) + " ms");
    }
}

This results in the following output:

Timed out after 578 ms true
Timed out after 609 ms true
Timed out after 502 ms true
Timed out after 502 ms true
Timed out after 501 ms true
Timed out after 502 ms true
Timed out after 501 ms true
Timed out after 501 ms true
Timed out after 277 ms
Timed out after 0 ms
Timed out after 0 ms
Timed out after 0 ms
Timed out after 0 ms
Timed out after 0 ms
<repeated endlessly until the ESP32 sends data>
@craigraw
Copy link
Author

craigraw commented Oct 7, 2024

Update: This issue appears to be caused by the nature of the USB connection. I was able to avoid it by connecting directly rather than through a USB hub, although it's still a mystery why this would cause immediate read timeouts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant