tarm / serial

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

read function always interrupted system call #99

Open miniboom360 opened 5 years ago

miniboom360 commented 5 years ago

Hello, I running on macOS sierra 10.12.6. I tried exactly as the example (except the port name, port name is cu.usbserial1 in my macBook). I sucessfully write string to the port but failed to read from it. it always return error. the error is called "read /dev/cu.usbserial1: interrupted system call", i've tried several times to send, but Disappointing results. any suggestions?

here is the code:

`import ( "fmt" "github.com/tarm/serial" "testing" )

func TestTarmSerialDataRecvAndSend(t *testing.T) { c := &serial.Config{ Name: "/dev/cu.usbserial1", Baud: 9600, }

s, err := serial.OpenPort(c)
if err != nil {
    fmt.Println(err)
    return
}

for i := 0; i < 10; i++ {
    n, err := s.Write([]byte("test"))
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Printf("send byte : %v",n)
}

fmt.Println("send over")

buf := make([]byte, 128)
n, err := s.Read(buf)
if err != nil{
    fmt.Println(err)
    return
}

fmt.Printf("%q", buf[:n])

} `

polomsky commented 4 years ago

I have the same problem on linux arm with Go 1.14. Handling of syscall error EINTR should be ignored.

for {
    n, err := s.Read(buf)
    switch err := err.(type) {
        case syscall.Errno:
            if err == syscall.EINTR {
                // interruption by signal - ignore it (e.g. caused by Go)
                continue
            }
        }
    }
    break;
}