forked from johnlauer/serial-port-json-server
-
Notifications
You must be signed in to change notification settings - Fork 101
/
seriallist.go
executable file
·112 lines (92 loc) · 2.54 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Supports Windows, Linux, Mac, and Raspberry Pi
package main
import (
"encoding/xml"
"strings"
//"fmt"
"github.com/facchinm/go-serial"
//"io/ioutil"
"log"
//"os"
"regexp"
)
type OsSerialPort struct {
Name string
FriendlyName string
RelatedNames []string // for some devices there are 2 or more ports, i.e. TinyG v9 has 2 serial ports
SerialNumber string
DeviceClass string
Manufacturer string
Product string
IdProduct string
IdVendor string
}
func GetList() ([]OsSerialPort, error) {
//log.Println("Doing GetList()")
ports, err := serial.GetPortsList()
arrPorts := []OsSerialPort{}
for _, element := range ports {
friendly := strings.Replace(element, "/dev/", "", -1)
arrPorts = append(arrPorts, OsSerialPort{Name: element, FriendlyName: friendly})
}
// 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 if reFilter.MatchString(element.FriendlyName) {
newarrPorts = append(newarrPorts, element)
} else {
log.Printf("serial port did not match. port: %v\n", element)
}
}
arrPorts = newarrPorts
}
//log.Printf("Done doing GetList(). arrPorts:%v\n", arrPorts)
return arrPorts, err
}
func GetMetaList() ([]OsSerialPort, error) {
metaportlist, err := getMetaList()
if err.Err != nil {
return nil, err.Err
}
return metaportlist, err.Err
}
func GetFriendlyName(portname string) string {
log.Println("GetFriendlyName from base class")
return ""
}
type Dict struct {
Keys []string `xml:"key"`
Arrays []Dict `xml:"array"`
Strings []string `xml:"string"`
Dicts []Dict `xml:"dict"`
}
type Result struct {
XMLName xml.Name `xml:"plist"`
//Strings []string `xml:"dict>string"`
Dict `xml:"dict"`
//Phone string
//Groups []string `xml:"Group>Value"`
}
/*
func GetDarwinMeta() {
xmlFile, err := os.Open("out.xml")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer xmlFile.Close()
XMLdata, _ := ioutil.ReadAll(xmlFile)
var v Result
//v := Result{}
xml.Unmarshal(XMLdata, &v)
log.Printf("Result:%v", len(v.Dicts[0].Arrays)) //, v.Dict.Dicts[0].Keys) // Dicts[0] .Keys[0])
log.Printf("Result:%v", v.Dicts[0].Arrays[1].Dicts[0].Keys[0]) // Dicts[0] .Keys[0])
log.Printf("Result:%v", v.Dicts[0].Keys[0]) // Dicts[0] .Keys[0])
}
*/