forked from chilipeppr/serial-port-json-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seriallist_darwin.go
65 lines (56 loc) · 1.43 KB
/
seriallist_darwin.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
package main
import (
//"fmt"
//"github.com/tarm/goserial"
"log"
"os"
"strings"
//"encoding/binary"
//"strconv"
//"syscall"
//"fmt"
//"encoding/xml"
"io/ioutil"
)
func getMetaList() ([]OsSerialPort, os.SyscallError) {
//return getListViaWmiPnpEntity()
return getListViaTtyList()
// query the out.xml file for now, but in real life
// we would run the ioreg -a -p IOUSB command to get the output
// and then parse it
}
func getListViaTtyList() ([]OsSerialPort, os.SyscallError) {
var err os.SyscallError
log.Println("getting serial list on darwin")
// make buffer of 100 max serial ports
// return a slice
list := make([]OsSerialPort, 100)
files, _ := ioutil.ReadDir("/dev/")
ctr := 0
for _, f := range files {
if strings.HasPrefix(f.Name(), "tty.") {
// it is a legitimate serial port
list[ctr].Name = "/dev/" + f.Name()
list[ctr].FriendlyName = f.Name()
log.Println("Added serial port to list: ", list[ctr])
ctr++
}
// stop-gap in case going beyond 100 (which should never happen)
// i mean, really, who has more than 100 serial ports?
if ctr > 99 {
ctr = 99
}
//fmt.Println(f.Name())
//fmt.Println(f.)
}
/*
list := make([]OsSerialPort, 3)
list[0].Name = "tty.serial1"
list[0].FriendlyName = "tty.serial1"
list[1].Name = "tty.serial2"
list[1].FriendlyName = "tty.serial2"
list[2].Name = "tty.Bluetooth-Modem"
list[2].FriendlyName = "tty.Bluetooth-Modem"
*/
return list[0:ctr], err
}