Skip to content

Commit d723283

Browse files
committed
Simplified enumerateSerialPorts function
1 parent 9e06768 commit d723283

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

seriallist.go

+19-27
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
package main
1919

2020
import (
21-
"slices"
22-
2321
log "github.com/sirupsen/logrus"
2422
"go.bug.st/serial/enumerator"
2523
)
@@ -35,36 +33,30 @@ type OsSerialPort struct {
3533
// enumerateSerialPorts will return the OS serial port
3634
func enumerateSerialPorts() ([]*OsSerialPort, error) {
3735
// will timeout in 2 seconds
38-
arrPorts := []*OsSerialPort{}
3936
ports, err := enumerator.GetDetailedPortsList()
4037
if err != nil {
41-
return arrPorts, err
38+
return nil, err
4239
}
4340

44-
for _, element := range ports {
45-
if element.IsUSB {
46-
vid, pid := "0x"+element.VID, "0x"+element.PID
47-
if vid != "0x0000" && pid != "0x0000" {
48-
arrPorts = append(arrPorts, &OsSerialPort{
49-
Name: element.Name,
50-
VID: vid,
51-
PID: pid,
52-
SerialNumber: element.SerialNumber,
53-
})
54-
}
41+
var res []*OsSerialPort
42+
for _, port := range ports {
43+
if !port.IsUSB {
44+
continue
5545
}
56-
}
57-
58-
// see if we should filter the list
59-
if portsFilter != nil {
60-
arrPorts = slices.DeleteFunc(arrPorts, func(port *OsSerialPort) bool {
61-
match := portsFilter.MatchString(port.Name)
62-
if !match {
63-
log.Debugf("ignoring port not matching filter. port: %v\n", port)
64-
}
65-
return match
46+
vid, pid := "0x"+port.VID, "0x"+port.PID
47+
if vid == "0x0000" || pid == "0x0000" {
48+
continue
49+
}
50+
if portsFilter != nil && !portsFilter.MatchString(port.Name) {
51+
log.Debugf("ignoring port not matching filter. port: %v\n", port)
52+
continue
53+
}
54+
res = append(res, &OsSerialPort{
55+
Name: port.Name,
56+
VID: vid,
57+
PID: pid,
58+
SerialNumber: port.SerialNumber,
6659
})
6760
}
68-
69-
return arrPorts, err
61+
return res, err
7062
}

0 commit comments

Comments
 (0)