Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to just set or unset break condition #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions serial_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,36 @@ 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
}

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 {
Expand Down
18 changes: 18 additions & 0 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,19 +386,37 @@ 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}
}

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
Expand Down