Fix UART hang after master restart from the dashboard.
ReadFrame now returns on serial timeout instead of looping forever, and serve closes and reopens the port after a master reboot so polling and API commands can resume. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+11
-1
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -98,13 +99,22 @@ func (p *Parser) Feed(b byte) (payload []byte, ok bool, err error) {
|
||||
}
|
||||
|
||||
// ReadFrame reads bytes from r until one full frame is parsed or an error occurs.
|
||||
func ReadFrame(r io.Reader, buf []byte) ([]byte, error) {
|
||||
// maxWait bounds total wait time; zero means no limit (serial read timeouts retry forever).
|
||||
func ReadFrame(r io.Reader, buf []byte, maxWait time.Duration) ([]byte, error) {
|
||||
if buf == nil {
|
||||
buf = make([]byte, 256)
|
||||
}
|
||||
parser := NewParser()
|
||||
|
||||
var deadline time.Time
|
||||
if maxWait > 0 {
|
||||
deadline = time.Now().Add(maxWait)
|
||||
}
|
||||
|
||||
for {
|
||||
if !deadline.IsZero() && !time.Now().Before(deadline) {
|
||||
return nil, ErrTimeout
|
||||
}
|
||||
n, err := r.Read(buf)
|
||||
if n > 0 {
|
||||
for i := 0; i < n; i++ {
|
||||
|
||||
Reference in New Issue
Block a user