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

Add RTS and DTR to GetModemStatusBits for Windows #37

Open
wants to merge 1 commit into
base: v1
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
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