Skip to content

Commit

Permalink
serial_unix.go: Keep writing until complete
Browse files Browse the repository at this point in the history
Fixes bugst#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.
  • Loading branch information
ccollins476ad committed Jul 7, 2024
1 parent 0996f84 commit 0d5660d
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions serial_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0d5660d

Please sign in to comment.