Cosmetic change: rewritten error conditions in a more compact way

This commit is contained in:
Cristian Maglie
2014-12-31 00:37:02 +01:00
parent 9e85415532
commit 2629684436
2 changed files with 8 additions and 11 deletions

View File

@@ -70,7 +70,7 @@ func OpenPort(portName string, mode *Mode) (SerialPort, error) {
} }
// Setup serial port // Setup serial port
if err := port.SetMode(mode); err != nil { if port.SetMode(mode) != nil {
port.Close() port.Close()
return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT} return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
} }
@@ -93,8 +93,7 @@ func OpenPort(portName string, mode *Mode) (SerialPort, error) {
settings.Cc[syscall.VMIN] = 1 settings.Cc[syscall.VMIN] = 1
settings.Cc[syscall.VTIME] = 0 settings.Cc[syscall.VTIME] = 0
err = port.setTermSettings(settings) if port.setTermSettings(settings) != nil {
if err != nil {
port.Close() port.Close()
return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT} return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
} }

View File

@@ -173,7 +173,7 @@ const (
func (port *windowsSerialPort) SetMode(mode *Mode) error { func (port *windowsSerialPort) SetMode(mode *Mode) error {
params := DCB{} params := DCB{}
if err := GetCommState(port.Handle, &params); err != nil { if GetCommState(port.Handle, &params) != nil {
port.Close() port.Close()
return &SerialPortError{code: ERROR_INVALID_SERIAL_PORT} return &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
} }
@@ -189,7 +189,7 @@ func (port *windowsSerialPort) SetMode(mode *Mode) error {
} }
params.StopBits = byte(mode.StopBits) params.StopBits = byte(mode.StopBits)
params.Parity = byte(mode.Parity) params.Parity = byte(mode.Parity)
if err := SetCommState(port.Handle, &params); err != nil { if SetCommState(port.Handle, &params) != nil {
port.Close() port.Close()
return &SerialPortError{code: ERROR_INVALID_SERIAL_PORT} return &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
} }
@@ -224,14 +224,13 @@ func OpenPort(portName string, mode *Mode) (SerialPort, error) {
} }
// Set port parameters // Set port parameters
if err := port.SetMode(mode); err != nil { if port.SetMode(mode) != nil {
port.Close() port.Close()
return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT} return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
} }
params := &DCB{} params := &DCB{}
err = GetCommState(port.Handle, params) if GetCommState(port.Handle, params) != nil {
if err != nil {
port.Close() port.Close()
return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT} return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
} }
@@ -248,8 +247,7 @@ func OpenPort(portName string, mode *Mode) (SerialPort, error) {
params.XoffLim = 512 params.XoffLim = 512
params.XonChar = 17 // DC1 params.XonChar = 17 // DC1
params.XoffChar = 19 // C3 params.XoffChar = 19 // C3
err = SetCommState(port.Handle, params) if SetCommState(port.Handle, params) != nil {
if err != nil {
port.Close() port.Close()
return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT} return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
} }
@@ -262,7 +260,7 @@ func OpenPort(portName string, mode *Mode) (SerialPort, error) {
WriteTotalTimeoutConstant: 0, WriteTotalTimeoutConstant: 0,
WriteTotalTimeoutMultiplier: 0, WriteTotalTimeoutMultiplier: 0,
} }
if err := SetCommTimeouts(port.Handle, timeouts); err != nil { if SetCommTimeouts(port.Handle, timeouts) != nil {
port.Close() port.Close()
return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT} return nil, &SerialPortError{code: ERROR_INVALID_SERIAL_PORT}
} }