From 56ac2d4e76afa2d2e789f33da60395ae184eccb1 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 25 Jun 2024 11:02:05 +0200 Subject: [PATCH] Restore check for no detected serial ports on Windows --- serial_windows.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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()