Added buffer flush methods

+ serial.Port.ResetInputBuffer()
+ serial.Port.ResetOutputBuffer()
This commit is contained in:
Veniamin Albaev
2017-03-09 11:11:06 +03:00
parent 4d886c1028
commit 8b7629bfe1
8 changed files with 48 additions and 0 deletions

View File

@@ -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

View File

@@ -13,3 +13,4 @@ const regexFilter = "^(cu|tty)\\..*"
const ioctlTcgetattr = unix.TIOCGETA
const ioctlTcsetattr = unix.TIOCSETA
const ioctlTcflsh = unix.TIOCFLUSH

View File

@@ -55,3 +55,4 @@ const tcCRTSCTS uint32 = tcCCTS_OFLOW
const ioctlTcgetattr = unix.TIOCGETA
const ioctlTcsetattr = unix.TIOCSETA
const ioctlTcflsh = unix.TIOCFLUSH

View File

@@ -62,3 +62,4 @@ const tcCRTSCTS uint32 = 0x80000000
const ioctlTcgetattr = unix.TCGETS
const ioctlTcsetattr = unix.TCSETS
const ioctlTcflsh = unix.TCFLSH

View File

@@ -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 {

View File

@@ -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

View File

@@ -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

View File

@@ -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
}