forked from tve/folie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial.go
More file actions
277 lines (241 loc) · 5.97 KB
/
serial.go
File metadata and controls
277 lines (241 loc) · 5.97 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
//"bytes"
"flag"
"fmt"
"io"
"net"
"os"
"path"
"runtime"
"strconv"
"strings"
"time"
"go.bug.st/serial.v1"
)
var (
port = flag.String("p", "", "serial port (COM*, /dev/cu.*, or /dev/tty*)")
baud = flag.Int("b", 115200, "serial baud rate")
raw = flag.Bool("r", false, "use raw instead of telnet protocol")
tty serial.Port // only used for serial connections
dev io.ReadWriteCloser // used for both serial and tcp connections
tnState int // tracks telnet protocol state when reading
)
func selectPort() string {
allPorts, err := serial.GetPortsList()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ""
}
var ports []string
for _, p := range allPorts {
if !strings.HasPrefix(p, "/dev/tty.") {
ports = append(ports, p)
}
}
//sort.Strings(ports)
if len(ports) == 0 {
fmt.Fprintln(os.Stderr, "No serial ports found.")
return ""
}
fmt.Fprintln(console.Stdout(), "Select the serial port:")
for i, p := range ports {
fmt.Fprintf(console.Stdout(), "%3d: %s\n", i+1, p)
}
console.SetPrompt("? ")
console.Refresh()
reply, _ := console.Readline()
console.SetPrompt("")
fmt.Println(reply)
sel, _ := strconv.Atoi(reply)
// quit on index errors, since we have no other useful choice
defer func() {
if e := recover(); e != nil {
return
}
fmt.Println("Enter '!help' for additional help, or ctrl-d to quit.")
}()
return ports[sel-1]
}
func boardReset(enterBoot bool) {
if !*raw {
telnetReset(enterBoot)
} else if tty != nil {
tty.SetDTR(true)
tty.SetRTS(!enterBoot)
time.Sleep(time.Millisecond)
tty.SetDTR(false)
}
time.Sleep(time.Millisecond)
}
func blockUntilOpen() {
var lastErr string
for {
var err error
if _, err = os.Lstat(*port); os.IsNotExist(err) &&
strings.Count(*port, ":") == 1 && !strings.HasSuffix(*port, ":") {
// if nonexistent, it's an ip addr + port, open it as network port
dev, err = net.Dial("tcp", *port)
} else {
tty, err = serial.Open(*port, &serial.Mode{
BaudRate: *baud,
})
dev = tty
if runtime.GOOS == "linux" {
switchToByPathDev("/dev/serial/by-path/")
}
}
if err == nil {
break
}
if err.Error() != lastErr {
fmt.Println(err)
lastErr = err.Error()
}
time.Sleep(500 * time.Millisecond)
}
serialRecv = make(chan []byte)
go SerialDispatch()
// use readline's Stdout to force re-display of current input
fmt.Fprintf(console.Stdout(), "[connected to %s]\n", path.Base(*port))
if *raw && tty != nil {
tty.SetRTS(true)
tty.SetDTR(false)
} else {
// this delay avoids massive reconnects with ser2net to a usb-console
time.Sleep(time.Second)
telnetInit()
}
}
// switchToByPathDev allows re-opening a device even if its name changes
func switchToByPathDev(prefix string) {
if dir, err := os.Open(prefix); err == nil {
names, _ := dir.Readdirnames(-1)
// look for an entry matching the current serial device name
for _, name := range names {
alias := prefix + name
link, e := os.Readlink(alias)
if e == nil && path.Base(*port) == path.Base(link) {
// switch to the session-indepenent name (only switches once)
*port = alias
break
}
}
}
}
// SerialConnect opens and re-opens a serial port and feeds the receive channel.
func SerialConnect() {
for {
tnState = 0 // clear telnet state before anything comes in
blockUntilOpen()
for {
data := make([]byte, 250)
n, err := dev.Read(data)
if err != nil {
break
}
if !*raw {
n = telnetClean(data, n)
}
if n > 0 {
serialRecv <- data[:n]
}
}
fmt.Print("\n[disconnected] ")
dev.Close()
dev = nil
tty = nil
close(serialRecv)
}
}
// SerialDispatch handles all incoming and outgoing serial data.
func SerialDispatch() {
go func() {
for data := range serialSend {
// send spaces iso tabs, because mecrisp echoes tabs differently
//data = bytes.Replace(data, []byte{'\t'}, []byte{' '}, -1)
if dev == nil { // avoid write-while-closed panics
fmt.Printf("[CAN'T WRITE! %s]\n", *port)
return
} else if _, err := dev.Write(data); err != nil {
fmt.Printf("[WRITE ERROR! %s]\n", *port)
if dev != nil {
dev.Close()
}
return
}
}
}()
for {
select {
case data, ok := <-serialRecv:
if *verbose {
fmt.Printf("recv: %q %v\n", data, ok)
}
if !ok {
return
}
os.Stdout.Write(data)
case line := <-commandSend:
if strings.HasPrefix(line, "!") {
if SpecialCommand(line) {
continue
}
line = line[1:]
}
data := []byte(line + "\r")
if *verbose {
fmt.Printf("send: %q\n", data)
}
serialSend <- data
}
}
}
// SpecialCommand recognises and handles certain commands in a different way.
func SpecialCommand(line string) bool {
cmd := strings.SplitN(line, " ", 2)
if len(cmd) > 0 {
switch cmd[0] {
case "!":
fmt.Println("[enter '!h' for help]")
case "!c", "!cd":
fmt.Println(line)
wrappedCd(cmd)
case "!h", "!help":
fmt.Println(line)
showHelp()
case "!l", "!ls":
fmt.Println(line)
wrappedLs(cmd)
case "!r", "!reset":
fmt.Println(line)
wrappedReset()
case "!s", "!send":
fmt.Println(line)
wrappedSend(cmd)
case "!u", "!upload":
fmt.Println(line)
wrappedUpload(cmd)
default:
return false
}
}
return true
}
const helpMsg = `
Special commands, these can also be abbreviated as "!r", etc:
!reset reset the board, same as ctrl-c
!send <file> send text file to the serial port, expand "include" lines
!upload show the list of built-in firmware images
!upload <n> upload built-in image <n> using STM32 boot protocol
!upload <file> upload specified firmware image (bin or hex format)
!upload <url> fetch firmware image from given URL, then upload it
Utility commands:
!cd <dir> change directory (or list current one if not specified)
!ls <dir> list contents of the specified (or current) directory
!help this message
To quit, hit ctrl-d. For command history, use up-/down-arrow.
`
func showHelp() {
fmt.Print(helpMsg[1:])
}