Skip to content

Commit

Permalink
Merge pull request #145 from cmaglie/send_break
Browse files Browse the repository at this point in the history
Added support for sending break (unix + windows)
  • Loading branch information
cmaglie authored Jan 2, 2023
2 parents f85edb4 + 893b2eb commit 0c6a048
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 74 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/creack/goselect v0.1.2
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
3 changes: 3 additions & 0 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions serial_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions serial_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions serial_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions serial_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions serial_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,20 @@ func (port *windowsPort) SetReadTimeout(timeout time.Duration) error {
return nil
}

func (port *windowsPort) Break(d time.Duration) error {
if err := setCommBreak(port.handle); err != nil {
return &PortError{causedBy: err}
}

time.Sleep(d)

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
4 changes: 4 additions & 0 deletions syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ package serial
//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

//sys setCommBreak(handle syscall.Handle) (err error) = SetCommBreak

//sys clearCommBreak(handle syscall.Handle) (err error) = ClearCommBreak
131 changes: 58 additions & 73 deletions zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c6a048

Please sign in to comment.