/
serial-default.go
87 lines (75 loc) · 1.81 KB
/
serial-default.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
// Copyright 2017 Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
package notecard
import (
"strings"
"go.bug.st/serial/enumerator"
)
// Notecard's USB VID/PID
const (
bluesincVID = "30A4"
notecardPID = "0001"
)
// Get the default serial device
func defaultSerialDefault() (device string, speed int) {
// Enum all ports
speed = 115200
ports, err2 := enumerator.GetDetailedPortsList()
if err2 != nil {
return
}
if len(ports) == 0 {
return
}
// First, look for the notecard
for _, port := range ports {
if port.IsUSB {
if strings.EqualFold(port.VID, bluesincVID) && strings.EqualFold(port.PID, notecardPID) {
device = port.Name
return
}
}
}
// Otherwise, look for anything from Blues
for _, port := range ports {
if port.IsUSB && strings.EqualFold(port.VID, bluesincVID) {
device = port.Name
return
}
}
// Not found
return
}
// Set or display the serial port
func defaultSerialPortEnum() (allports []string, usbports []string, notecardports []string, err error) {
// Enum all ports
ports, err2 := enumerator.GetDetailedPortsList()
if err2 != nil {
err = err2
return
}
if len(ports) == 0 {
return
}
// First, look for the notecard
for _, port := range ports {
allports = append(allports, port.Name)
if port.IsUSB {
usbports = append(usbports, port.Name)
if strings.EqualFold(port.VID, bluesincVID) && strings.EqualFold(port.PID, notecardPID) {
notecardports = append(notecardports, port.Name)
}
}
}
// Otherwise, look for anything from Blues
if len(notecardports) == 0 {
for _, port := range ports {
if port.IsUSB && strings.EqualFold(port.VID, bluesincVID) {
notecardports = append(notecardports, port.Name)
}
}
}
// Done
return
}