Skip to content

Conversation

@uamana
Copy link

@uamana uamana commented Dec 28, 2025

Explicit ReadTimeout Error for bufio.Reader Support

Summary

This PR ensures that read timeout errors are explicitly returned as PortError{code: ReadTimeout}, enabling proper timeout tracking.

Example Usage

port, _ := serial.Open("/dev/ttyUSB0", &serial.Mode{})
port.SetReadTimeout(1 * time.Second)
reader := bufio.NewReader(port)

for {
    line, err := reader.ReadLine()
    if err != nil {
        if portErr, ok := err.(*serial.PortError); ok {
            if portErr.Code() == serial.ReadTimeout {
                // Timeout occurred - check for buffered data
                if reader.Buffered() > 0 {
                    continue // Process buffered data
                }
                // No data available, timeout is expected
                continue
            }
        }
        // Other error occurred
        return err
    }
    // Process line
}

Related

This change improves integration with bufio.Reader by making timeout errors explicitly identifiable. Without it bufio.Reader returns error about multiple empty reads.

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

Successfully merging this pull request may close these issues.

1 participant