Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/drain support #159

Merged
merged 3 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Port interface {
// Returns the number of bytes written.
Write(p []byte) (n int, err error)

// Wait until all data in the buffer are sent
Drain() error

// ResetInputBuffer Purges port read buffer
ResetInputBuffer() error

Expand Down
9 changes: 9 additions & 0 deletions serial_bsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build darwin || dragonfly || freebsd || netbsd || openbsd

package serial

import "golang.org/x/sys/unix"

func (port *unixPort) Drain() error {
return unix.IoctlSetInt(port.handle, unix.TIOCDRAIN, 0)
}
7 changes: 7 additions & 0 deletions serial_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,10 @@ func setTermSettingsBaudrate(speed int, settings *unix.Termios) (error, bool) {
settings.Ospeed = toTermiosSpeedType(baudrate)
return nil, false
}

func (port *unixPort) Drain() error {
// It's not super well documented, but this is the same as calling tcdrain:
// - https://git.musl-libc.org/cgit/musl/tree/src/termios/tcdrain.c
// - https://elixir.bootlin.com/linux/v6.2.8/source/drivers/tty/tty_io.c#L2673
return unix.IoctlSetInt(port.handle, unix.TCSBRK, 1)
cmaglie marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 4 additions & 0 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ func (port *windowsPort) Write(p []byte) (int, error) {
return int(writed), err
}

func (port *windowsPort) Drain() (err error) {
return syscall.FlushFileBuffers(port.handle)
}

const (
purgeRxAbort uint32 = 0x0002
purgeRxClear = 0x0008
Expand Down