Skip to content

Commit

Permalink
Merge pull request #174 from bugst/windows-multi-set-rtscts
Browse files Browse the repository at this point in the history
windows: Avoid double-set of communication parameters on Open
  • Loading branch information
cmaglie authored Feb 16, 2024
2 parents 8b563a8 + c414e4f commit 9a2045a
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ func (port *windowsPort) SetMode(mode *Mode) error {
port.Close()
return &PortError{code: InvalidSerialPort}
}
port.setModeParams(mode, &params)
if setCommState(port.handle, &params) != nil {
port.Close()
return &PortError{code: InvalidSerialPort}
}
return nil
}

func (port *windowsPort) setModeParams(mode *Mode, params *dcb) {
if mode.BaudRate == 0 {
params.BaudRate = 9600 // Default to 9600
} else {
Expand All @@ -266,11 +275,6 @@ func (port *windowsPort) SetMode(mode *Mode) error {
}
params.StopBits = stopBitsMap[mode.StopBits]
params.Parity = parityMap[mode.Parity]
if setCommState(port.handle, &params) != nil {
port.Close()
return &PortError{code: InvalidSerialPort}
}
return nil
}

func (port *windowsPort) SetDTR(dtr bool) error {
Expand Down Expand Up @@ -432,16 +436,12 @@ func nativeOpen(portName string, mode *Mode) (*windowsPort, error) {
}

// Set port parameters
if port.SetMode(mode) != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort}
}

params := &dcb{}
if getCommState(port.handle, params) != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort}
}
port.setModeParams(mode, params)
params.Flags &= dcbDTRControlDisableMask
params.Flags &= dcbRTSControlDisbaleMask
if mode.InitialStatusBits == nil {
Expand Down

0 comments on commit 9a2045a

Please sign in to comment.