bugst / go-serial

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

stop reading #76

Closed lukasbeckercode closed 4 years ago

lukasbeckercode commented 4 years ago

In the example Project, there is this block of code:

// Read and print the response
buff := make([]byte, 100)
for {
    // Reads up to 100 bytes
    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]))
}

However, if n==0 never becomes true on my machine, so the reading doesn´t stop

xiegeo commented 4 years ago

If nothing is printed, then you are not reading any data. If printing does not stop, then there is aways data coming in, so there is no sense of stopping.

You probably want an application-specific condition for stopping, there is no general solution.

For the example code, you have to exist the program manually.

cmaglie commented 4 years ago

Probably the example is misleading. the condition n==0 should never happen in this example (unless a timeout happens, but we do not support timeout as of now, or the port is disconnected, but this should not happen in normal conditions).

I will produce a better example...

lukasbeckercode commented 4 years ago

I changed the example myself and opened a pull request. Please tell me if i did something wrong as I´m new to github and still inexperienced. But i hope i was able to help you. Thanks for the awesome code btw!