Skip to content

Commit

Permalink
Merge pull request #36 from Lobaro/v1
Browse files Browse the repository at this point in the history
Windows: Set DTS via setCommState
  • Loading branch information
cmaglie authored Jul 28, 2017
2 parents 86b770d + 04b4ab1 commit eae1344
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,41 @@ func (port *windowsPort) SetMode(mode *Mode) error {
}

func (port *windowsPort) SetDTR(dtr bool) error {
var res bool
// Like for RTS there are problems with the escapeCommFunction
// observed behaviour was that DTR is set from false -> true
// when setting RTS from true -> false
// 1) Connect -> RTS = true (low) DTR = true (low) OKAY
// 2) SetDTR(false) -> RTS = true (low) DTR = false (heigh) OKAY
// 3) SetRTS(false) -> RTS = false (heigh) DTR = true (low) ERROR: DTR toggled
//
// In addition this way the CommState Flags are not updated
/*
var res bool
if dtr {
res = escapeCommFunction(port.handle, commFunctionSetDTR)
} else {
res = escapeCommFunction(port.handle, commFunctionClrDTR)
}
if !res {
return &PortError{}
}
return nil
*/

// The following seems a more reliable way to do it

params := &dcb{}
if err := getCommState(port.handle, params); err != nil {
return &PortError{causedBy: err}
}
params.Flags &= dcbDTRControlDisableMask
if dtr {
res = escapeCommFunction(port.handle, commFunctionSetDTR)
} else {
res = escapeCommFunction(port.handle, commFunctionClrDTR)
params.Flags |= dcbDTRControlEnable
}
if !res {
return &PortError{}
if err := setCommState(port.handle, params); err != nil {
return &PortError{causedBy: err}
}

return nil
}

Expand All @@ -283,6 +309,8 @@ func (port *windowsPort) SetRTS(rts bool) error {
// it doesn't send USB control message when the RTS bit is
// changed, so the following code not always works with
// USB-to-serial adapters.
//
// In addition this way the CommState Flags are not updated

/*
var res bool
Expand Down

0 comments on commit eae1344

Please sign in to comment.