Stream-decode msgpack responses instead of waiting for read timeout
This commit is contained in:
@@ -2,6 +2,7 @@ package picoserial
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"go.bug.st/serial"
|
||||
@@ -38,20 +39,12 @@ func (t *SerialTransport) Send(data []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *SerialTransport) Receive(timeout time.Duration) ([]byte, error) {
|
||||
func (t *SerialTransport) SetReadTimeout(timeout time.Duration) {
|
||||
t.port.SetReadTimeout(timeout)
|
||||
var resp []byte
|
||||
buf := make([]byte, 256)
|
||||
for {
|
||||
n, err := t.port.Read(buf)
|
||||
if n > 0 {
|
||||
resp = append(resp, buf[:n]...)
|
||||
}
|
||||
if err != nil || n == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (t *SerialTransport) Reader() io.Reader {
|
||||
return t.port
|
||||
}
|
||||
|
||||
func (t *SerialTransport) Close() error {
|
||||
|
||||
Reference in New Issue
Block a user