Skip to content

Commit f0e4a45

Browse files
authored
Merge pull request #202 from ctarsjp/win-port-enum
Fix (non-detailed) port enumeration on Windows
2 parents 3449d2e + bcb0408 commit f0e4a45

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

serial_windows.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package serial
1919

2020
import (
2121
"errors"
22+
"strings"
2223
"sync"
2324
"syscall"
2425
"time"
@@ -45,12 +46,22 @@ func nativeGetPortsList() ([]string, error) {
4546
}
4647
defer key.Close()
4748

48-
list, err := key.ReadValueNames(0)
49+
names, err := key.ReadValueNames(0)
4950
if err != nil {
5051
return nil, &PortError{code: ErrorEnumeratingPorts, causedBy: err}
5152
}
5253

53-
return list, nil
54+
var values []string
55+
for _, n := range names {
56+
v, _, err := key.GetStringValue(n)
57+
if err != nil || v == "" {
58+
continue
59+
}
60+
61+
values = append(values, v)
62+
}
63+
64+
return values, nil
5465
}
5566

5667
func (port *windowsPort) Close() error {
@@ -342,7 +353,9 @@ func createOverlappedEvent() (*windows.Overlapped, error) {
342353
}
343354

344355
func nativeOpen(portName string, mode *Mode) (*windowsPort, error) {
345-
portName = "\\\\.\\" + portName
356+
if !strings.HasPrefix(portName, `\\.\`) {
357+
portName = `\\.\` + portName
358+
}
346359
path, err := windows.UTF16PtrFromString(portName)
347360
if err != nil {
348361
return nil, err

0 commit comments

Comments
 (0)