tarm / serial

BSD 3-Clause "New" or "Revised" License
1.6k stars 453 forks source link

Windows ReadTimeout no effect #81

Closed MiyamuraMiyako closed 6 years ago

MiyamuraMiyako commented 6 years ago

when serial readbuffer haven't any data. Read return immediately, not wait setting's timeout.

xiegeo commented 6 years ago

This shouldn't be, do you have sample code to reproduce?

MiyamuraMiyako commented 6 years ago

Just simple code. @xiegeo

sc := &serial.Config{Name: "COM3", Baud: 9600, Size: 8, Parity: serial.ParityNone, StopBits: serial.Stop1, ReadTimeout: time.Millisecond * 50}
sp, err := serial.OpenPort(sc)
if err != nil {
    fmt.Println(err.Error())
    return false
}
buf := make([]byte, 128)
sp.Read(buf)
return true

if serial device not send any data, Read will return immediately.

xiegeo commented 6 years ago

50 milliseconds is very short, so it's unlikely to catch anything before timing out.

Also, your read doesn't do anything, you should check for errors and the number returned to know if you have read any data.

As your code is written, the behavior is expected.

MiyamuraMiyako commented 6 years ago

@xiegeo sorry,My mistake.