mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
platforms/firmata/client: use io.ReadFull
This CL uses io.ReadFull to make sure exactly n bytes are read, and correctly handle io.EOF and co. Fixes #343. Signed-off-by: Sebastien Binet <seb.binet@gmail.com> Conflicts: platforms/firmata/client/client.go
This commit is contained in:
parent
4ef57b6d72
commit
1b574b5d45
@ -315,21 +315,9 @@ func (b *Client) write(data []byte) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (b *Client) read(length int) (buf []byte, err error) {
|
||||
i := 0
|
||||
for length > 0 {
|
||||
tmp := make([]byte, length)
|
||||
if i, err = b.connection.Read(tmp); err != nil {
|
||||
if err.Error() != "EOF" {
|
||||
return
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
if i > 0 {
|
||||
buf = append(buf, tmp...)
|
||||
length = length - i
|
||||
}
|
||||
}
|
||||
func (b *Client) read(n int) (buf []byte, err error) {
|
||||
buf = make([]byte, n)
|
||||
_, err = io.ReadFull(b.connection, buf)
|
||||
return
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user