Skip to content

Commit

Permalink
Windows: nativeGetPortsList, com0com, nativeGetDetailedPortsList
Browse files Browse the repository at this point in the history
  • Loading branch information
abakum committed Feb 5, 2025
1 parent 427ed9f commit 8aa536f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
30 changes: 24 additions & 6 deletions enumerator/usb_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"syscall"
"unsafe"

"github.com/abakum/go-serial"
"golang.org/x/sys/windows"
)

Expand Down Expand Up @@ -271,9 +272,15 @@ func nativeGetDetailedPortsList() ([]*PortDetails, error) {
if err != nil {
return nil, &PortEnumerationError{causedBy: err}
}
ports, err := serial.GetPortsList()
if err != nil || len(ports) == 0 {
return nil, &PortEnumerationError{causedBy: err}
}

var res []*PortDetails
for _, g := range guids {
// com0com {df799e12-3c56-421b-b298-b6d3642bc878}
com0com := guid{0xdf799e12, 0x3c56, 0x421b, [8]byte{0xb2, 0x98, 0xb6, 0xd3, 0x64, 0x2b, 0xc8, 0x78}}
for _, g := range append(guids, com0com) {
devsSet, err := g.getDevicesSet()
if err != nil {
return nil, &PortEnumerationError{causedBy: err}
Expand All @@ -290,10 +297,10 @@ func nativeGetDetailedPortsList() ([]*PortDetails, error) {
if err != nil {
continue
}
if len(portName) < 3 || portName[0:3] != "COM" {
// Accept only COM ports
continue
}
// if len(portName) < 3 || portName[0:3] != "COM" {
// // Accept only COM ports
// continue
// }
details.Name = portName

if err := retrievePortDetailsFromDevInfo(device, details); err != nil {
Expand All @@ -302,7 +309,18 @@ func nativeGetDetailedPortsList() ([]*PortDetails, error) {
res = append(res, details)
}
}
return res, nil
ordRes := []*PortDetails{}
for _, port := range ports {
inner:
for _, detailedPort := range res {
if port == detailedPort.Name {
ordRes = append(ordRes, detailedPort)
break inner
}
}
}

return ordRes, nil
}

func retrievePortNameFromDevInfo(device *deviceInfo) (string, error) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fvbommel/sortorder v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglD
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fvbommel/sortorder v1.1.0 h1:fUmoe+HLsBTctBDoaBwpQo5N+nrCp8g/BjKb/6ZQmYw=
github.com/fvbommel/sortorder v1.1.0/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
5 changes: 5 additions & 0 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ package serial

import (
"errors"
"sort"
"sync"
"syscall"
"time"

"github.com/fvbommel/sortorder"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
Expand Down Expand Up @@ -55,6 +57,9 @@ func nativeGetPortsList() (list []string, err error) {
}
list = append(list, item)
}
if len(list) > 1 {
sort.Sort(sortorder.Natural(list))
}

return list, nil
}
Expand Down

0 comments on commit 8aa536f

Please sign in to comment.