Skip to content

Commit

Permalink
Add RTS and DTR to GetModemStatusBits for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
niondir committed Jul 28, 2017
1 parent 1ba1196 commit 29730ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type ModemStatusBits struct {
DSR bool // DataSetReady status
RI bool // RingIndicator status
DCD bool // DataCarrierDetect status
RTS bool // ReadyToSend status (outgoing)
DTR bool // DataTerminalReady status (outgoing)
}

// Open opens the serial port using the specified modes
Expand Down
25 changes: 16 additions & 9 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const (
dcbInX = 0x00000200
dcbErrorChar = 0x00000400
dcbNull = 0x00000800
dcbRTSControlDisbaleMask = ^uint32(0x00003000)
dcbRTSControlDisableMask = ^uint32(0x00003000)
dcbRTSControlEnable = 0x00001000
dcbRTSControlHandshake = 0x00002000
dcbRTSControlToggle = 0x00003000
Expand Down Expand Up @@ -303,7 +303,7 @@ func (port *windowsPort) SetRTS(rts bool) error {
if err := getCommState(port.handle, params); err != nil {
return &PortError{causedBy: err}
}
params.Flags &= dcbRTSControlDisbaleMask
params.Flags &= dcbRTSControlDisableMask
if rts {
params.Flags |= dcbRTSControlEnable
}
Expand All @@ -314,15 +314,22 @@ func (port *windowsPort) SetRTS(rts bool) error {
}

func (port *windowsPort) GetModemStatusBits() (*ModemStatusBits, error) {
var bits uint32
if !getCommModemStatus(port.handle, &bits) {
var modemStatus uint32
if !getCommModemStatus(port.handle, &modemStatus) {
return nil, &PortError{}
}
var commState dcb
if err := getCommState(port.handle, &commState); err != nil {
return nil, &PortError{causedBy: err}
}

return &ModemStatusBits{
CTS: (bits & msCTSOn) != 0,
DCD: (bits & msRLSDOn) != 0,
DSR: (bits & msDSROn) != 0,
RI: (bits & msRingOn) != 0,
CTS: (modemStatus & msCTSOn) != 0,
DCD: (modemStatus & msRLSDOn) != 0,
DSR: (modemStatus & msDSROn) != 0,
RI: (modemStatus & msRingOn) != 0,
RTS: (commState.Flags & dcbRTSControlEnable) != 0,
DTR: (commState.Flags & dcbDTRControlEnable) != 0,
}, nil
}

Expand Down Expand Up @@ -369,7 +376,7 @@ func nativeOpen(portName string, mode *Mode) (*windowsPort, error) {
port.Close()
return nil, &PortError{code: InvalidSerialPort}
}
params.Flags &= dcbRTSControlDisbaleMask
params.Flags &= dcbRTSControlDisableMask
params.Flags |= dcbRTSControlEnable
params.Flags &= dcbDTRControlDisableMask
params.Flags |= dcbDTRControlEnable
Expand Down

0 comments on commit 29730ad

Please sign in to comment.