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

when read 0 bytes not return EOF on windows #43

Open
xjiaj8 opened this issue Apr 17, 2016 · 3 comments
Open

when read 0 bytes not return EOF on windows #43

xjiaj8 opened this issue Apr 17, 2016 · 3 comments

Comments

@xjiaj8
Copy link

xjiaj8 commented Apr 17, 2016

c := &serial.Config{Name: "COM4", Baud: 115200, ReadTimeout: time.Second * 5}
conn,_:=serial.OpenPort(c)
buf:= bytes.NewBufferString("")

n, err := buf.ReadFrom(conn) //always block!!
fmt.Printf("% x,%d,%v\n",buf.Bytes(),n,err)
buf.Reset()

@tarm
Copy link
Owner

tarm commented Apr 22, 2016

Unfortunately I don't have a windows computer to test with right now. What do you see when you run that code? What do you see if you do

buf := make([]byte, 1<<10)
n, err := conn.Read(buf)
fmt.Println("% x,%d,%v\n",buf[:n],n,err)

I'm not sure that the package has documented and committed to a particular return code (EOF or something with a .Timeout() method) after a timeout.

The recommended way is to just use a plain []byte slice and call c.Read() directly.

@ohac
Copy link

ohac commented Sep 14, 2017

conn.Read(buf) returns 0 and nil when timeout.

io.Reader:

Implementations of Read are discouraged from returning a zero byte count with a nil error, except when len(p) == 0.

bufio.ReadFrom implementation is:

https://golang.org/src/bufio/bufio.go?s=16790:16849#L672

I think a zero byte with a nil error causes infinite loop.

@typecampo
Copy link

time.Millisecond works for me on Windows 8.1

ReadTimeout: time.Millisecond * 5000

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

4 participants