Skip to content

Commit

Permalink
Improved error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Feb 16, 2024
1 parent 1d3c009 commit 9aae282
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions serial_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package serial

import (
"fmt"
"io/ioutil"
"regexp"
"strings"
Expand Down Expand Up @@ -232,7 +233,7 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {
settings, err := port.getTermSettings()
if err != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error getting term settings: %w", err)}
}

// Set raw mode
Expand All @@ -243,14 +244,14 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {

if port.setTermSettings(settings) != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error setting term settings: %w", err)}
}

if mode.InitialStatusBits != nil {
status, err := port.getModemBitsStatus()
if err != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort, causedBy: err}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error getting modem bits status: %w", err)}
}
if mode.InitialStatusBits.DTR {
status |= unix.TIOCM_DTR
Expand All @@ -264,15 +265,15 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {
}
if err := port.setModemBitsStatus(status); err != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort, causedBy: err}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error setting modem bits status: %w", err)}
}
}

// MacOSX require that this operation is the last one otherwise an
// 'Invalid serial port' error is returned... don't know why...
if port.SetMode(mode) != nil {
if err := port.SetMode(mode); err != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error configuring port: %w", err)}
}

unix.SetNonblock(h, false)
Expand All @@ -283,7 +284,7 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {
pipe := &unixutils.Pipe{}
if err := pipe.Open(); err != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort, causedBy: err}
return nil, &PortError{code: InvalidSerialPort, causedBy: fmt.Errorf("error opening signaling pipe: %w", err)}
}
port.closeSignal = pipe

Expand Down

0 comments on commit 9aae282

Please sign in to comment.