Open lu-xiaoliang opened 1 year ago
Unfortunately, this is a breaking change. I'm collecting all the breaking changes to be implemented in a future 2.0 release, BTW it's a low priority, for now, no estimates for when it will be done.
This is a simple workaround:
var errTimeout = errors.New("timeout")
type timeoutReader struct {
r io.Reader
}
func NewTimeoutReader(r io.Reader) timeoutReader {
return timeoutReader{r}
}
func (t timeoutReader) Read(p []byte) (n int, err error) {
n, err = t.r.Read(p)
if n == 0 && err == nil {
err = errTimeout
}
return
}
You only have to wrap your port in a NewTimeoutReader(port)
and io.ReadFull and Scanner work as expected.
Updated with the suggestion from @ccollins476ad
This is a simple workaround: [...]
if n == 0 { err = errTimeout }
Good suggestion. Just be sure not to lose the original error if there is one!
if n == 0 && err == nil {
err = errTimeout
}
Good suggestion. Just be sure not to lose the original error if there is one!
Good catch, oops. Thanks!
when i write a command, then i use io.ReadFull to read an response. But if the device do not response, it will STW. whether to return a read timeout error?