bugst / go-serial

A cross-platform serial library for go-lang.
BSD 3-Clause "New" or "Revised" License
620 stars 189 forks source link

Reading error on windows #77

Open matteonistri opened 4 years ago

matteonistri commented 4 years ago

Hello, using this library on windows, I encountered an error on reading. I know for sure that there is something to read, but the Read method is blocking (even after the read timeout is elapsed) and it never returns. After a debug session, I found out that it's waiting for overlapped I/O to complete (here) that apparently never happens. Here's my code:

mode := &serial.Mode{
        BaudRate: 115200,
        Parity:   serial.OddParity,
        DataBits: 8,
        StopBits: serial.TwoStopBits,
    }

port, err := serial.Open("COM1", mode)
n, err := port.Write([]byte("data\r"))
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Sent %v bytes\n", n)

buff := make([]byte, 1024)
for {
    n, err = port.Read(buff)
    if err != nil {
        log.Fatal(err)
        break
    }
    if n == 0 {
        fmt.Println("\nEOF")
        break
    }
    fmt.Printf("%v", string(buff[:n]))
    fmt.Println(n)
}

Am I doing something wrong?

Also I found out that on handle creation (here), passing 0 instead of syscall.FILE_FLAG_OVERLAPPED, will make Read work.