Restore check for no detected serial ports on Windows

This commit is contained in:
Tom Payne
2024-06-25 11:02:05 +02:00
parent 1c72447e64
commit 56ac2d4e76

View File

@@ -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()