tarm / serial

BSD 3-Clause "New" or "Revised" License
1.61k stars 452 forks source link

cannot read a whole message #63

Closed joyqat closed 7 years ago

joyqat commented 7 years ago
buf := make([]byte, 128)
for {
    n, err = s.Read(buf)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("%q", buf[:n])
}

result of code above: when receive "abcdef" it will print "a" and "bcdef" always a single byte followed by reminded bytes. what is the problem?

btw, I use windows 10 build1703, Go 1.8, VSPD 6.9(virtual serial port).

Robin3D commented 7 years ago

Me too, I have the same problem

paocalvi commented 7 years ago

My two cents..... IMHO, this approach to the reading is not correct. Like in TCP, you should not relay on a bunch of bytes arriving all together just because they have been sent together, because of fragmentation, when it comes to serial, you should be prepared to received half (or part) of a message, and then the rest, or many messages together. Even if in the case of a directly connected serial-to-serial your expectations is correct, it is certainly not when you for exemple, pass through some serial-over-ip device or SW. If you try in any case to write SW not relying on "all message delivered at once" the result will be more robust and reusable.

Paolo

On Thu, Aug 24, 2017 at 8:36 AM, Robin3D notifications@github.com wrote:

Me too, I have the same problem

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tarm/serial/issues/63#issuecomment-324546848, or mute the thread https://github.com/notifications/unsubscribe-auth/APDv1UfRLIueib7HDCGgV8lECvpS7Ekwks5sbRnSgaJpZM4O8-ik .

joyqat commented 7 years ago

thanks for your opinion