Skip to content

Commit

Permalink
windows: Avoid double-set of communication parameters on Open
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Feb 16, 2024
1 parent 8a31fcb commit c414e4f
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 c414e4f

Please sign in to comment.