diff --git a/serial.go b/serial.go index 2bb7f3e..4420100 100644 --- a/serial.go +++ b/serial.go @@ -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 diff --git a/serial_bsd.go b/serial_bsd.go new file mode 100644 index 0000000..4db5b3d --- /dev/null +++ b/serial_bsd.go @@ -0,0 +1,10 @@ +//go:build darwin || dragonfly || freebsd || netbsd || openbsd +// +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) +} diff --git a/serial_linux.go b/serial_linux.go index 116474d..0ac0f39 100644 --- a/serial_linux.go +++ b/serial_linux.go @@ -85,3 +85,13 @@ func setTermSettingsBaudrate(speed int, settings *unix.Termios) (error, bool) { settings.Ospeed = toTermiosSpeedType(baudrate) return nil, false } + +func (port *unixPort) Drain() error { + // simulate drain with change settings with TCSETSW + settings, err := port.getTermSettings() + if err != nil { + return err + } + + return unix.IoctlSetTermios(port.handle, unix.TCSETSW, settings) +} diff --git a/serial_windows.go b/serial_windows.go index e393d4e..5c4a576 100644 --- a/serial_windows.go +++ b/serial_windows.go @@ -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