tarm / serial

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

There is no error ReadTimeout #128

Closed gitchander closed 1 year ago

gitchander commented 1 year ago
package main

import (
    "fmt"
    "log"
    "time"

    "github.com/tarm/serial"
)

func checkError(err error) {
    if err != nil {
        log.Fatal(err)
    }
}

func main() {

    config := &serial.Config{
        //Name:        "COM1",
        Name:        "/dev/ttyUSB0",
        Baud:        115200,
        ReadTimeout: 2 * time.Second,
    }
    p, err := serial.OpenPort(config)
    checkError(err)
    defer p.Close()

    data := []byte("ping")
    _, err = p.Write(data)
    checkError(err)

    res := make([]byte, 1024)

    n, err := p.Read(res) // ???
    checkError(err)

    fmt.Printf("readed %d bytes, hex: [%x]\n", n, res[:n])
}

If serial port not return bytes: for Linux it returns EOF; for windows it returns readed 0 bytes error is nil. If I used bufio reader ReadByte() - operation is blocking because serial Read retuns -> (0 , nil). Here need a ReadTimeout error.

FObersteiner commented 1 year ago

I had the same problem lately; unfortunately this repo here does not seem to be actively maintained.

However, I think some of the forks implement useful extensions. For example https://github.com/urbanminded/serial seems to have what we're looking for in 59f58d4f6eb90c61c481fff26c5040f2bf2a43d7

gitchander commented 1 year ago

Thanks for comment!