From 94305a758a2e626ab6f1c37183d1a715351b8d33 Mon Sep 17 00:00:00 2001 From: James P Date: Fri, 10 Nov 2023 17:09:59 +0000 Subject: [PATCH] added ability to just set or unset break condition without specifying a time limit --- serial.go | 3 +++ serial_unix.go | 17 +++++++++++++++++ serial_windows.go | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/serial.go b/serial.go index abfd7f9..39a9e01 100644 --- a/serial.go +++ b/serial.go @@ -54,6 +54,9 @@ type Port interface { // Break sends a break for a determined time Break(time.Duration) error + + // Set or unset break condition, allows timing to be done more precisely outside of this module + SetBreak(bool) error } // NoTimeout should be used as a parameter to SetReadTimeout to disable timeout. diff --git a/serial_unix.go b/serial_unix.go index f6ec896..ba5a4fe 100644 --- a/serial_unix.go +++ b/serial_unix.go @@ -118,12 +118,14 @@ func (port *unixPort) Write(p []byte) (n int, err error) { } func (port *unixPort) Break(t time.Duration) error { + // set break if err := unix.IoctlSetInt(port.handle, ioctlTiocsbrk, 0); err != nil { return err } time.Sleep(t) + // clear break if err := unix.IoctlSetInt(port.handle, ioctlTioccbrk, 0); err != nil { return err } @@ -131,6 +133,21 @@ func (port *unixPort) Break(t time.Duration) error { return nil } +func (port *unixPort) SetBreak(setBreak bool) error { + if setBreak { + // set break + if err := unix.IoctlSetInt(port.handle, ioctlTiocsbrk, 0); err != nil { + return err + } + } else { + // clear break + 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 287856a..70dbaf0 100644 --- a/serial_windows.go +++ b/serial_windows.go @@ -386,12 +386,14 @@ func (port *windowsPort) SetReadTimeout(timeout time.Duration) error { } func (port *windowsPort) Break(d time.Duration) error { + // set break if err := setCommBreak(port.handle); err != nil { return &PortError{causedBy: err} } time.Sleep(d) + // unset break if err := clearCommBreak(port.handle); err != nil { return &PortError{causedBy: err} } @@ -399,6 +401,22 @@ func (port *windowsPort) Break(d time.Duration) error { return nil } +func (port *windowsPort) SetBreak(setBreak bool) error { + if setBreak { + // set break + if err := setCommBreak(port.handle); err != nil { + return &PortError{causedBy: err} + } + } else { + // unset break + if err := clearCommBreak(port.handle); err != nil { + return &PortError{causedBy: err} + } + } + + return nil +} + func createOverlappedEvent() (*syscall.Overlapped, error) { h, err := createEvent(nil, true, false, nil) return &syscall.Overlapped{HEvent: h}, err