From 168cb3879d2bf562c8b37cfda7cd9a815d366bab Mon Sep 17 00:00:00 2001 From: Christopher Collins Date: Sat, 6 Jul 2024 18:57:03 -0700 Subject: [PATCH] serial_unix.go: Keep writing until complete Fixes #191. Change unix port.Write() implementation to match io.Writer requirement: > Write must return a non-nil error if it returns n < len(p) Keep executing write syscall until failure or entire buffer is written. --- serial_unix.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/serial_unix.go b/serial_unix.go index 54e55a8..338c5b6 100644 --- a/serial_unix.go +++ b/serial_unix.go @@ -109,12 +109,25 @@ func (port *unixPort) Read(p []byte) (int, error) { } } -func (port *unixPort) Write(p []byte) (n int, err error) { - n, err = unix.Write(port.handle, p) +func (port *unixPort) writeOnce(p []byte) (int, error) { + n, err := unix.Write(port.handle, p) if n < 0 { // Do not return -1 unix errors + if err == nil { + err = fmt.Errorf("unix.Write() returned n<0,err==nil: n=%d", n) + } n = 0 } - return + return n, err +} + +func (port *unixPort) Write(p []byte) (n int, err error) { + for n < len(p) && err == nil { + var n2 int + n2, err = port.writeOnce(p[n:]) + n += n2 + } + + return n, err } func (port *unixPort) Break(t time.Duration) error {