Merge pull request #202 from ctarsjp/win-port-enum

Fix (non-detailed) port enumeration on Windows
This commit is contained in:
Cristian Maglie
2025-03-28 15:54:19 +01:00
committed by GitHub

View File

@@ -19,6 +19,7 @@ package serial
import (
"errors"
"strings"
"sync"
"syscall"
"time"
@@ -45,12 +46,22 @@ func nativeGetPortsList() ([]string, error) {
}
defer key.Close()
list, err := key.ReadValueNames(0)
names, err := key.ReadValueNames(0)
if err != nil {
return nil, &PortError{code: ErrorEnumeratingPorts, causedBy: err}
}
return list, nil
var values []string
for _, n := range names {
v, _, err := key.GetStringValue(n)
if err != nil || v == "" {
continue
}
values = append(values, v)
}
return values, nil
}
func (port *windowsPort) Close() error {
@@ -342,7 +353,9 @@ func createOverlappedEvent() (*windows.Overlapped, error) {
}
func nativeOpen(portName string, mode *Mode) (*windowsPort, error) {
portName = "\\\\.\\" + portName
if !strings.HasPrefix(portName, `\\.\`) {
portName = `\\.\` + portName
}
path, err := windows.UTF16PtrFromString(portName)
if err != nil {
return nil, err