diff --git a/serial_windows.go b/serial_windows.go index 4751c91..5853e51 100644 --- a/serial_windows.go +++ b/serial_windows.go @@ -18,7 +18,9 @@ package serial */ import ( + "errors" "sync" + "syscall" "time" "golang.org/x/sys/windows" @@ -32,7 +34,12 @@ type windowsPort struct { func nativeGetPortsList() ([]string, error) { key, err := registry.OpenKey(windows.HKEY_LOCAL_MACHINE, `HARDWARE\DEVICEMAP\SERIALCOMM\`, windows.KEY_READ) - if err != nil { + switch { + case errors.Is(err, syscall.ERROR_FILE_NOT_FOUND): + // On machines with no serial ports the registry key does not exist. + // Return this as no serial ports instead of an error. + return nil, nil + case err != nil: return nil, &PortError{code: ErrorEnumeratingPorts, causedBy: err} } defer key.Close()