Skip to content

Commit

Permalink
nativeGetPortsList for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
abakum committed Feb 3, 2025
1 parent dacab2d commit 8d0b118
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions portlist/portlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"log"

"github.com/abakum/go-serial"
"github.com/abakum/go-serial/enumerator"
)

Expand All @@ -40,4 +41,5 @@ func main() {
fmt.Printf(" USB serial : %s\n", port.SerialNumber)
}
}
fmt.Println(serial.GetPortsList())
}
11 changes: 9 additions & 2 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type windowsPort struct {
handle windows.Handle
}

func nativeGetPortsList() ([]string, error) {
func nativeGetPortsList() (list []string, err error) {
key, err := registry.OpenKey(windows.HKEY_LOCAL_MACHINE, `HARDWARE\DEVICEMAP\SERIALCOMM\`, windows.KEY_READ)
switch {
case errors.Is(err, syscall.ERROR_FILE_NOT_FOUND):
Expand All @@ -44,10 +44,17 @@ 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}
}
for _, name := range names {
item, _, err := key.GetStringValue(name)
if err != nil {
continue
}
list = append(list, item)
}

return list, nil
}
Expand Down

0 comments on commit 8d0b118

Please sign in to comment.