-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathseriallist.go
executable file
·93 lines (79 loc) · 2.04 KB
/
seriallist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Supports Windows, Linux, Mac, and Raspberry Pi
package main
import (
"fmt"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
"go.bug.st/serial/enumerator"
)
type OsSerialPort struct {
Name string
DeviceClass string
Manufacturer string
Product string
IdProduct string
IdVendor string
ISerial string
NetworkPort bool
}
func GetList(network bool) ([]OsSerialPort, error) {
if network {
netportList, err := GetNetworkList()
return netportList, err
}
// will timeout in 2 seconds
arrPorts := []OsSerialPort{}
ports, err := enumerator.GetDetailedPortsList()
if err != nil {
return arrPorts, err
}
for _, element := range ports {
if element.IsUSB {
vid := element.VID
pid := element.PID
vidString := fmt.Sprintf("0x%s", vid)
pidString := fmt.Sprintf("0x%s", pid)
if vid != "0000" && pid != "0000" {
arrPorts = append(arrPorts, OsSerialPort{Name: element.Name, IdVendor: vidString, IdProduct: pidString, ISerial: element.SerialNumber})
}
}
}
// see if we should filter the list
if len(*regExpFilter) > 0 {
// yes, user asked for a filter
reFilter := regexp.MustCompile("(?i)" + *regExpFilter)
newarrPorts := []OsSerialPort{}
for _, element := range arrPorts {
// if matches regex, include
if reFilter.MatchString(element.Name) {
newarrPorts = append(newarrPorts, element)
} else {
log.Debugf("serial port did not match. port: %v\n", element)
}
}
arrPorts = newarrPorts
}
return arrPorts, err
}
func findPortByName(portname string) (*serport, bool) {
portnamel := strings.ToLower(portname)
for port := range sh.ports {
if strings.ToLower(port.portConf.Name) == portnamel {
// we found our port
//spHandlerClose(port)
return port, true
}
}
return nil, false
}
func findPortByNameRerun(portname string, network bool) (OsSerialPort, bool) {
portnamel := strings.ToLower(portname)
list, _ := GetList(network)
for _, item := range list {
if strings.ToLower(item.Name) == portnamel {
return item, true
}
}
return OsSerialPort{}, false
}