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

Timeout : return an error compatible with os.IsTimeout() #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ package serial_test
import (
"fmt"
"log"
"os"
"strings"
"time"

"go.bug.st/serial"
)
Expand Down Expand Up @@ -53,11 +55,20 @@ func Example_sendAndReceive() {

// Read and print the response

if err := port.SetReadTimeout(1 * time.Minute); err != nil {
fmt.Printf("failed to set read timeout: %v\n", err)
}

buff := make([]byte, 100)
for {
// Reads up to 100 bytes
n, err := port.Read(buff)
if err != nil {
if os.IsTimeout(err) {
fmt.Println("timeout")
// TODO do something on read timeout
continue
}
log.Fatal(err)
}
if n == 0 {
Expand Down
9 changes: 9 additions & 0 deletions serial.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think case Timeout:; return "Timeout" should also be added before default: (bcaab3f#diff-ad4fb56affb62e9f5024c32ea8a857376661813e1bc22b7dfef64f21ff16c34fR176)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ const (
PortClosed
// FunctionNotImplemented the requested function is not implemented
FunctionNotImplemented
// Timeout when an operation has timed out
Timeout
)

// EncodedErrorString returns a string explaining the error code
Expand Down Expand Up @@ -194,6 +196,8 @@ func (e PortError) EncodedErrorString() string {
return "Port has been closed"
case FunctionNotImplemented:
return "Function not implemented"
case Timeout:
return "Timeout"
default:
return "Other error"
}
Expand All @@ -211,3 +215,8 @@ func (e PortError) Error() string {
func (e PortError) Code() PortErrorCode {
return e.code
}

// Timeout returns true if is is a timeout (usable with os.IsTimeout)
func (e PortError) Timeout() bool {
return e.code == Timeout
}
2 changes: 1 addition & 1 deletion serial_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (port *unixPort) Read(p []byte) (int, error) {
}
if !res.IsReadable(port.handle) {
// Timeout happened
return 0, nil
return 0, &PortError{code: Timeout}
}
n, err := unix.Read(port.handle, p)
if err == unix.EINTR {
Expand Down
2 changes: 1 addition & 1 deletion serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (port *windowsPort) Read(p []byte) (int, error) {
}

// Timeout
return 0, nil
return 0, &PortError{code: Timeout}
}

func (port *windowsPort) Write(p []byte) (int, error) {
Expand Down