-
Notifications
You must be signed in to change notification settings - Fork 0
/
seriallist.go
executable file
·71 lines (58 loc) · 1.59 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
// Supports Windows, Linux, Mac, and Raspberry Pi
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/facchinm/go-serial-native"
"regexp"
)
type OsSerialPort struct {
Name string
SerialNumber string
DeviceClass string
Manufacturer string
Product string
IdProduct string
IdVendor string
ISerial string
NetworkPort bool
}
func GetList(network bool) ([]OsSerialPort, error) {
//log.Println("Doing GetList()")
if network {
netportList, err := GetNetworkList()
return netportList, err
} else {
// will timeout in 2 seconds
arrPorts := []OsSerialPort{}
ports, err := serial.ListPorts()
if err != nil {
return arrPorts, err
}
for _, element := range ports {
vid, pid, _ := element.USBVIDPID()
vidString := fmt.Sprintf("0x%04X", vid)
pidString := fmt.Sprintf("0x%04X", pid)
if vid != 0 && pid != 0 {
arrPorts = append(arrPorts, OsSerialPort{Name: element.Name(), IdVendor: vidString, IdProduct: pidString, ISerial: element.USBSerialNumber()})
}
}
// 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.Debug("serial port did not match. port: %v\n", element)
}
}
arrPorts = newarrPorts
}
return arrPorts, err
//log.Printf("Done doing GetList(). arrPorts:%v\n", arrPorts)
}
}