diff --git a/serial.go b/serial.go index d61f658..46eb521 100644 --- a/serial.go +++ b/serial.go @@ -24,6 +24,12 @@ type Port interface { // Returns the number of bytes written. Write(p []byte) (n int, err error) + // ResetInputBuffer Purges port read buffer + ResetInputBuffer() error + + // ResetOutputBuffer Purges port write buffer + ResetOutputBuffer() error + // SetDTR sets the modem status bit DataTerminalReady SetDTR(dtr bool) error diff --git a/serial_darwin.go b/serial_darwin.go index 845b51f..cfbde8f 100644 --- a/serial_darwin.go +++ b/serial_darwin.go @@ -13,3 +13,4 @@ const regexFilter = "^(cu|tty)\\..*" const ioctlTcgetattr = unix.TIOCGETA const ioctlTcsetattr = unix.TIOCSETA +const ioctlTcflsh = unix.TIOCFLUSH diff --git a/serial_freebsd.go b/serial_freebsd.go index 0e54166..a7fde38 100644 --- a/serial_freebsd.go +++ b/serial_freebsd.go @@ -55,3 +55,4 @@ const tcCRTSCTS uint32 = tcCCTS_OFLOW const ioctlTcgetattr = unix.TIOCGETA const ioctlTcsetattr = unix.TIOCSETA +const ioctlTcflsh = unix.TIOCFLUSH diff --git a/serial_linux.go b/serial_linux.go index b41f25b..d05287c 100644 --- a/serial_linux.go +++ b/serial_linux.go @@ -62,3 +62,4 @@ const tcCRTSCTS uint32 = 0x80000000 const ioctlTcgetattr = unix.TCGETS const ioctlTcsetattr = unix.TCSETS +const ioctlTcflsh = unix.TCFLSH diff --git a/serial_unix.go b/serial_unix.go index 58ee9c0..69bbceb 100644 --- a/serial_unix.go +++ b/serial_unix.go @@ -74,6 +74,14 @@ func (port *unixPort) Write(p []byte) (n int, err error) { return unix.Write(port.handle, p) } +func (port *unixPort) ResetInputBuffer() error { + return ioctl(port.handle, ioctlTcflsh, unix.TCIFLUSH) +} + +func (port *unixPort) ResetOutputBuffer() error { + return ioctl(port.handle, ioctlTcflsh, unix.TCOFLUSH) +} + func (port *unixPort) SetMode(mode *Mode) error { settings, err := port.getTermSettings() if err != nil { diff --git a/serial_windows.go b/serial_windows.go index aabb7be..4a504ef 100644 --- a/serial_windows.go +++ b/serial_windows.go @@ -115,6 +115,21 @@ func (port *windowsPort) Write(p []byte) (int, error) { return int(writed), err } +const ( + purgeRxAbort uint32 = 0x0002 + purgeRxClear = 0x0008 + purgeTxAbort = 0x0001 + purgeTxClear = 0x0004 +) + +func (port *windowsPort) ResetInputBuffer() error { + return purgeComm(port.handle, purgeRxClear|purgeRxAbort) +} + +func (port *windowsPort) ResetOutputBuffer() error { + return purgeComm(port.handle, purgeTxClear|purgeTxAbort) +} + const ( dcbBinary uint32 = 0x00000001 dcbParity = 0x00000002 diff --git a/syscall_windows.go b/syscall_windows.go index 529b4df..989a61a 100644 --- a/syscall_windows.go +++ b/syscall_windows.go @@ -23,3 +23,6 @@ package serial // import "go.bug.st/serial.v1" //sys resetEvent(handle syscall.Handle) (err error) = ResetEvent //sys getOverlappedResult(handle syscall.Handle, overlapEvent *syscall.Overlapped, n *uint32, wait bool) (err error) = GetOverlappedResult + +//sys purgeComm(handle syscall.Handle, flags uint32) (err error) = PurgeComm + diff --git a/zsyscall_windows.go b/zsyscall_windows.go index 896fd40..f6005ea 100644 --- a/zsyscall_windows.go +++ b/zsyscall_windows.go @@ -49,6 +49,7 @@ var ( procCreateEventW = modkernel32.NewProc("CreateEventW") procResetEvent = modkernel32.NewProc("ResetEvent") procGetOverlappedResult = modkernel32.NewProc("GetOverlappedResult") + procPurgeComm = modkernel32.NewProc("PurgeComm") ) func regEnumValue(key syscall.Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, value *uint16, valueLen *uint32) (regerrno error) { @@ -161,3 +162,15 @@ func getOverlappedResult(handle syscall.Handle, overlapEvent *syscall.Overlapped } return } + +func purgeComm(handle syscall.Handle, flags uint32) (err error) { + r1, _, e1 := syscall.Syscall(procPurgeComm.Addr(), 2, uintptr(handle), uintptr(flags), 0) + if r1 == 0 { + if e1 != 0 { + err = errnoErr(e1) + } else { + err = syscall.EINVAL + } + } + return +}