Skip to content

Commit

Permalink
Manually merge pull request tarm#62 from tarm/serial by @jaguar
Browse files Browse the repository at this point in the history
  • Loading branch information
winsock committed Apr 6, 2018
1 parent eaafced commit a0ef255
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ var (
nCreateEvent,
nResetEvent,
nPurgeComm,
nEscapeCommFunction,
nGetCommModemStatus,
nFlushFileBuffers uintptr
)

Expand All @@ -161,6 +163,80 @@ func init() {
nResetEvent = getProcAddr(k32, "ResetEvent")
nPurgeComm = getProcAddr(k32, "PurgeComm")
nFlushFileBuffers = getProcAddr(k32, "FlushFileBuffers")
nEscapeCommFunction = getProcAddr(k32, "EscapeCommFunction")
nGetCommModemStatus = getProcAddr(k32, "GetCommModemStatus")
}

func (p *Port) SetDtrOn() error {
const SETDTR = 0x0005

r, _, err := syscall.Syscall(nEscapeCommFunction, 2, uintptr(p.fd), SETDTR, 0)
if r == 0 {
return err
}
return nil
}
func (p *Port) SetDtrOff() error {
const CLRDTR = 0x0006
r, _, err := syscall.Syscall(nEscapeCommFunction, 2, uintptr(p.fd), CLRDTR, 0)
if r == 0 {
return err
}
return nil
}

func (p *Port) SetRtsOff() error {
const CLRRTS = 0x0004
r, _, err := syscall.Syscall(nEscapeCommFunction, 2, uintptr(p.fd), CLRRTS, 0)
if r == 0 {
return err
}
return nil
}

func (p *Port) SetRtsOn() error {
const SETRTS = 0x0003
r, _, err := syscall.Syscall(nEscapeCommFunction, 2, uintptr(p.fd), SETRTS, 0)
if r == 0 {
return err
}
return nil
}

func (p *Port) GetCommModemStatus() (err error, cts_on, dsr_on, ring_on, rlsd_on bool) {

// The CTS (clear-to-send) signal is on.
const MS_CTS_ON = 0x0010
// The DSR (data-set-ready) signal is on.
const MS_DSR_ON = 0x0020
// The ring indicator signal is on.
const MS_RING_ON = 0x0040
// The RLSD (receive-line-signal-detect) signal is on.
const MS_RLSD_ON = 0x0080

var statusval byte

cts_on, dsr_on, ring_on, rlsd_on = false, false, false, false

r, _, err := syscall.Syscall(nGetCommModemStatus, 2, uintptr(p.fd), uintptr(unsafe.Pointer(&statusval)), 0)
if r == 0 {
return err, cts_on, dsr_on, ring_on, rlsd_on
}

if statusval&MS_CTS_ON != 0 {
cts_on = true
}
if statusval&MS_DSR_ON != 0 {
dsr_on = true
}
if statusval&MS_RING_ON != 0 {
ring_on = true
}
if statusval&MS_RLSD_ON != 0 {
rlsd_on = true
}

return nil, cts_on, dsr_on, ring_on, rlsd_on
}

func getProcAddr(lib syscall.Handle, name string) uintptr {
Expand Down

0 comments on commit a0ef255

Please sign in to comment.