Skip to content

Commit 041f054

Browse files
committed
slightly refactor serial port listing
1 parent d9dfa10 commit 041f054

File tree

3 files changed

+44
-33
lines changed

3 files changed

+44
-33
lines changed

serial.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,15 @@ func discoverLoop() {
144144
func spListDual(network bool) {
145145

146146
// call our os specific implementation of getting the serial list
147-
list, _ := GetList(network)
147+
list, err := GetList(network)
148+
149+
log.Println(list)
150+
log.Println(err)
151+
152+
if err != nil {
153+
// avoid reporting dummy data if an error occurred
154+
return
155+
}
148156

149157
// do a quick loop to see if any of our open ports
150158
// did not end up in the list port list. this can

seriallist.go

+31-32
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,46 @@ func GetList(network bool) ([]OsSerialPort, error) {
2727
if network {
2828
netportList, err := GetNetworkList()
2929
return netportList, err
30-
} else {
30+
}
3131

32-
// will timeout in 2 seconds
33-
arrPorts := []OsSerialPort{}
34-
ports, err := enumerator.GetDetailedPortsList()
35-
if err != nil {
36-
return arrPorts, err
37-
}
32+
// will timeout in 2 seconds
33+
arrPorts := []OsSerialPort{}
34+
ports, err := enumerator.GetDetailedPortsList()
35+
if err != nil {
36+
return arrPorts, err
37+
}
3838

39-
for _, element := range ports {
40-
if element.IsUSB {
41-
vid := element.VID
42-
pid := element.PID
43-
vidString := fmt.Sprintf("0x%s", vid)
44-
pidString := fmt.Sprintf("0x%s", pid)
45-
if vid != "0000" && pid != "0000" {
46-
arrPorts = append(arrPorts, OsSerialPort{Name: element.Name, IdVendor: vidString, IdProduct: pidString, ISerial: element.SerialNumber})
47-
}
39+
for _, element := range ports {
40+
if element.IsUSB {
41+
vid := element.VID
42+
pid := element.PID
43+
vidString := fmt.Sprintf("0x%s", vid)
44+
pidString := fmt.Sprintf("0x%s", pid)
45+
if vid != "0000" && pid != "0000" {
46+
arrPorts = append(arrPorts, OsSerialPort{Name: element.Name, IdVendor: vidString, IdProduct: pidString, ISerial: element.SerialNumber})
4847
}
4948
}
49+
}
5050

51-
// see if we should filter the list
52-
if len(*regExpFilter) > 0 {
53-
// yes, user asked for a filter
54-
reFilter := regexp.MustCompile("(?i)" + *regExpFilter)
55-
56-
newarrPorts := []OsSerialPort{}
57-
for _, element := range arrPorts {
58-
// if matches regex, include
59-
if reFilter.MatchString(element.Name) {
60-
newarrPorts = append(newarrPorts, element)
61-
} else {
62-
log.Debug("serial port did not match. port: %v\n", element)
63-
}
51+
// see if we should filter the list
52+
if len(*regExpFilter) > 0 {
53+
// yes, user asked for a filter
54+
reFilter := regexp.MustCompile("(?i)" + *regExpFilter)
6455

56+
newarrPorts := []OsSerialPort{}
57+
for _, element := range arrPorts {
58+
// if matches regex, include
59+
if reFilter.MatchString(element.Name) {
60+
newarrPorts = append(newarrPorts, element)
61+
} else {
62+
log.Debug("serial port did not match. port: %v\n", element)
6563
}
66-
arrPorts = newarrPorts
67-
}
6864

69-
return arrPorts, err
65+
}
66+
arrPorts = newarrPorts
7067
}
68+
69+
return arrPorts, err
7170
}
7271

7372
func findPortByName(portname string) (*serport, bool) {

serialport.go

+4
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ func spHandlerOpen(portname string, baud int, buftype string) {
321321
// this is thread to send to serial port regardless of block
322322
go p.writerNoBuf()
323323
p.reader()
324+
325+
spListDual(false)
326+
spList(false)
327+
324328
//go p.reader()
325329
//p.done = make(chan bool)
326330
//<-p.done

0 commit comments

Comments
 (0)