diff --git a/serial.go b/serial.go index e072d42..2bb7f3e 100644 --- a/serial.go +++ b/serial.go @@ -48,6 +48,9 @@ type Port interface { // Close the serial port Close() error + + // Break sends a break for a determined time + Break(time.Duration) error } // NoTimeout should be used as a parameter to SetReadTimeout to disable timeout. diff --git a/serial_darwin.go b/serial_darwin.go index f30dc8d..6c448a7 100644 --- a/serial_darwin.go +++ b/serial_darwin.go @@ -14,6 +14,8 @@ const regexFilter = "^(cu|tty)\\..*" const ioctlTcgetattr = unix.TIOCGETA const ioctlTcsetattr = unix.TIOCSETA const ioctlTcflsh = unix.TIOCFLUSH +const ioctlTioccbrk = unix.TIOCCBRK +const ioctlTiocsbrk = unix.TIOCSBRK func setTermSettingsBaudrate(speed int, settings *unix.Termios) (error, bool) { baudrate, ok := baudrateMap[speed] diff --git a/serial_freebsd.go b/serial_freebsd.go index ca1ef76..1ccaf33 100644 --- a/serial_freebsd.go +++ b/serial_freebsd.go @@ -56,6 +56,8 @@ const tcCRTSCTS uint32 = tcCCTS_OFLOW const ioctlTcgetattr = unix.TIOCGETA const ioctlTcsetattr = unix.TIOCSETA const ioctlTcflsh = unix.TIOCFLUSH +const ioctlTioccbrk = unix.TIOCCBRK +const ioctlTiocsbrk = unix.TIOCSBRK func toTermiosSpeedType(speed uint32) uint32 { return speed diff --git a/serial_linux.go b/serial_linux.go index 1d6edb3..116474d 100644 --- a/serial_linux.go +++ b/serial_linux.go @@ -63,6 +63,8 @@ const tcCRTSCTS uint32 = unix.CRTSCTS const ioctlTcgetattr = unix.TCGETS const ioctlTcsetattr = unix.TCSETS const ioctlTcflsh = unix.TCFLSH +const ioctlTioccbrk = unix.TIOCCBRK +const ioctlTiocsbrk = unix.TIOCSBRK func toTermiosSpeedType(speed uint32) uint32 { return speed diff --git a/serial_openbsd.go b/serial_openbsd.go index e9d71a4..bf38bba 100644 --- a/serial_openbsd.go +++ b/serial_openbsd.go @@ -56,6 +56,8 @@ const tcCRTSCTS uint32 = tcCCTS_OFLOW const ioctlTcgetattr = unix.TIOCGETA const ioctlTcsetattr = unix.TIOCSETA const ioctlTcflsh = unix.TIOCFLUSH +const ioctlTioccbrk = unix.TIOCCBRK +const ioctlTiocsbrk = unix.TIOCSBRK func toTermiosSpeedType(speed uint32) int32 { return int32(speed) diff --git a/serial_unix.go b/serial_unix.go index e171001..0e83018 100644 --- a/serial_unix.go +++ b/serial_unix.go @@ -4,6 +4,7 @@ // license that can be found in the LICENSE file. // +//go:build linux || darwin || freebsd || openbsd // +build linux darwin freebsd openbsd package serial @@ -117,6 +118,20 @@ func (port *unixPort) Write(p []byte) (n int, err error) { return } +func (port *unixPort) Break(t time.Duration) error { + if err := unix.IoctlSetInt(port.handle, ioctlTiocsbrk, 0); err != nil { + return err + } + + time.Sleep(t) + + if err := unix.IoctlSetInt(port.handle, ioctlTioccbrk, 0); err != nil { + return err + } + + return nil +} + 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 1a3db7a..e84ea08 100644 --- a/serial_windows.go +++ b/serial_windows.go @@ -381,6 +381,10 @@ func (port *windowsPort) SetReadTimeout(timeout time.Duration) error { return nil } +func (port *windowsPort) Break(d time.Duration) error { + return &PortError{code: FunctionNotImplemented} +} + func createOverlappedEvent() (*syscall.Overlapped, error) { h, err := createEvent(nil, true, false, nil) return &syscall.Overlapped{HEvent: h}, err