forked from tarm/serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enum_linux.go
56 lines (43 loc) · 952 Bytes
/
enum_linux.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
// +build linux
package serial
import (
"io/ioutil"
"regexp"
"strings"
)
// EnumerateSerialPorts lists all serial ports.
// Based on https://github.com/bugst/go-serial
func EnumerateSerialPorts() ([]string, []string, error) {
files, err := ioutil.ReadDir(devFolder)
if err != nil {
return nil, nil, err
}
ports := make([]string, 0, len(files))
for _, f := range files {
// Skip folders
if f.IsDir() {
continue
}
// Keep only devices with the correct name
match, err := regexp.MatchString(regexFilter, f.Name())
if err != nil {
return nil, nil, err
}
if !match {
continue
}
portName := devFolder + "/" + f.Name()
if strings.HasPrefix(f.Name(), "ttyS") {
c := &Config{Name: portName, Baud: 115200}
s, err := OpenPort(c)
if err != nil {
continue
} else {
s.Close()
}
}
// Save serial port in the resulting list
ports = append(ports, portName)
}
return ports, nil, nil
}