Skip to content

Commit d5a5a6a

Browse files
committed
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.
1 parent 0996f84 commit d5a5a6a

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

serial_unix.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,25 @@ func (port *unixPort) Read(p []byte) (int, error) {
109109
}
110110
}
111111

112-
func (port *unixPort) Write(p []byte) (n int, err error) {
113-
n, err = unix.Write(port.handle, p)
112+
func (port *unixPort) writeOnce(p []byte) (int, error) {
113+
n, err := unix.Write(port.handle, p)
114114
if n < 0 { // Do not return -1 unix errors
115+
if err == nil {
116+
err = fmt.Errorf("unix.Write() returned n<0,err==nil: n=%d", n)
117+
}
115118
n = 0
116119
}
117-
return
120+
return n, err
121+
}
122+
123+
func (port *unixPort) Write(p []byte) (n int, err error) {
124+
for n < len(p) && err != nil {
125+
var n2 int
126+
n2, err = port.writeOnce(p[n:])
127+
n += n2
128+
}
129+
130+
return n, err
118131
}
119132

120133
func (port *unixPort) Break(t time.Duration) error {

0 commit comments

Comments
 (0)