-
Notifications
You must be signed in to change notification settings - Fork 174
/
serialport.go
executable file
·585 lines (494 loc) · 17.7 KB
/
serialport.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package main
import (
"bytes"
"encoding/json"
"sync"
"github.com/johnlauer/goserial"
//"github.com/facchinm/go-serial"
"io"
"log"
"strconv"
"strings"
"time"
)
type SerialConfig struct {
Name string
Baud int
// Size int // 0 get translated to 8
// Parity SomeNewTypeToGetCorrectDefaultOf_None
// StopBits SomeNewTypeToGetCorrectDefaultOf_1
// RTSFlowControl bool
// DTRFlowControl bool
// XONFlowControl bool
// CRLFTranslate bool
// TimeoutStuff int
RtsOn bool
DtrOn bool
}
type serport struct {
// The serial port connection.
// Needed for original serial library
portConf *serial.Config
// Needed for Arduino serial library
//portConf *SerialConfig
portIo io.ReadWriteCloser
done chan bool // signals the end of this request
// Keep track of whether we're being actively closed
// just so we don't show scary error messages
isClosing bool
// counter incremented on queue, decremented on write
itemsInBuffer int
// buffered channel containing up to 25600 outbound messages.
sendBuffered chan Cmd
// unbuffered channel of outbound messages that bypass internal serial port buffer
sendNoBuf chan Cmd
// Do we have an extra channel/thread to watch our buffer?
BufferType string
//bufferwatcher *BufferflowDummypause
bufferwatcher Bufferflow
// Keep track of whether this is the primary serial port, i.e. cnc controller
// or if its secondary, i.e. a backup port or arduino or something tertiary
IsPrimary bool
IsSecondary bool
// Feedrate override value
feedRateOverride float32
isFeedRateOverrideOn bool
}
type Cmd struct {
data string
id string
skippedBuffer bool
willHandleCompleteResponse bool
pause int
}
type CmdComplete struct {
Cmd string
Id string
P string
BufSize int `json:"-"`
D string `json:"-"`
}
type qwReport struct {
Cmd string
QCnt int
Id string
D string `json:"-"`
Buf string `json:"-"`
P string
}
type qwReportWithData struct {
Cmd string
QCnt int
Id string
D string //`json:"-"`
Buf string `json:"-"`
P string
}
type SpPortMessage struct {
P string // the port, i.e. com22
D string // the data, i.e. G0 X0 Y0
}
func (p *serport) reader() {
//var buf bytes.Buffer
ch := make([]byte, 1024)
timeCheckOpen := time.Now()
for {
n, err := p.portIo.Read(ch)
//if we detect that port is closing, break out o this for{} loop.
if p.isClosing {
strmsg := "Shutting down reader on " + p.portConf.Name
log.Println(strmsg)
h.broadcastSys <- []byte(strmsg)
break
}
// read can return legitimate bytes as well as an error
// so process the bytes if n > 0
if n > 0 {
//log.Print("Read " + strconv.Itoa(n) + " bytes ch: " + string(ch))
data := string(ch[:n])
//log.Print("The data i will convert to json is:")
//log.Print(data)
// give the data to our bufferflow so it can do it's work
// to read/translate the data to see if it wants to block
// writes to the serialport. each bufferflow type will decide
// this on its own based on its logic, i.e. tinyg vs grbl vs others
//p.b.bufferwatcher..OnIncomingData(data)
p.bufferwatcher.OnIncomingData(data)
// see if the OnIncomingData handled the broadcast back
// to the user. this option was added in case the OnIncomingData wanted
// to do something fancier or implementation specific, i.e. TinyG Buffer
// actually sends back data on a perline basis rather than our method
// where we just send the moment we get it. the reason for this is that
// the browser was sometimes getting back packets out of order which
// of course would screw things up when parsing
if p.bufferwatcher.IsBufferGloballySendingBackIncomingData() == false {
//m := SpPortMessage{"Alice", "Hello"}
m := SpPortMessage{p.portConf.Name, data}
//log.Print("The m obj struct is:")
//log.Print(m)
//b, err := json.MarshalIndent(m, "", "\t")
b, err := json.Marshal(m)
if err != nil {
log.Println(err)
h.broadcastSys <- []byte("Error creating json on " + p.portConf.Name + " " +
err.Error() + " The data we were trying to convert is: " + string(ch[:n]))
break
}
//log.Print("Printing out json byte data...")
//log.Print(string(b))
h.broadcastSys <- b
//h.broadcastSys <- []byte("{ \"p\" : \"" + p.portConf.Name + "\", \"d\": \"" + string(ch[:n]) + "\" }\n")
}
}
// double check that we got characters in the buffer
// before deciding if an EOF is legitimately a reason
// to close the port because we're seeing that on some
// os's like Linux/Ubuntu you get an EOF when you open
// the port. Perhaps the EOF was buffered from a previous
// close and the OS doesn't clear out that buffer on a new
// connect. This means we'll only catch EOF's when there are
// other characters with it, but that seems to work ok
if n <= 0 {
if err == io.EOF || err == io.ErrUnexpectedEOF {
// hit end of file
log.Println("Hit end of file on serial port")
h.broadcastSys <- []byte("{\"Cmd\":\"OpenFail\",\"Desc\":\"Got EOF (End of File) on port which usually means another app other than Serial Port JSON Server is locking your port. " + err.Error() + "\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + "}")
}
if err != nil {
log.Println(err)
h.broadcastSys <- []byte("Error reading on " + p.portConf.Name + " " +
err.Error() + " Closing port.")
h.broadcastSys <- []byte("{\"Cmd\":\"OpenFail\",\"Desc\":\"Got error reading on port. " + err.Error() + "\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + "}")
break
}
// Keep track of time difference between two consecutive read with n == 0 and err == nil
// we get here if the port has been disconnected while open (cpu usage will jump to 100%)
// let's close the port only if the events are extremely fast (<1ms)
if err == nil {
diff := time.Since(timeCheckOpen)
if diff.Nanoseconds() < 1000000 {
p.isClosing = true
}
timeCheckOpen = time.Now()
}
}
}
p.portIo.Close()
}
// this method runs as its own thread because it's instantiated
// as a "go" method. so if it blocks inside, it is ok
func (p *serport) writerBuffered() {
// this method can panic if user closes serial port and something is
// in BlockUntilReady() and then a send occurs on p.sendNoBuf
defer func() {
if e := recover(); e != nil {
// e is the interface{} typed-value we passed to panic()
log.Println("Got panic: ", e) // Prints "Whoops: boom!"
}
}()
// this for loop blocks on p.sendBuffered until that channel
// sees something come in
for data := range p.sendBuffered {
log.Printf("Got p.sendBuffered. data:%v, id:%v, pause:%v\n", strings.Replace(string(data.data), "\n", "\\n", -1), string(data.id), data.pause)
// we want to block here if we are being asked
// to pause.
goodToGo, willHandleCompleteResponse, newGcode := p.bufferwatcher.BlockUntilReady(string(data.data), data.id)
// BlockUntilReady can modify our Gcode now so it can possibly add tracking data
// so if we got newGcode then we must swap it for our original gcode
if len(newGcode) > 0 {
data.data = newGcode
}
if goodToGo == false {
log.Println("We got back from BlockUntilReady() but apparently we must cancel this cmd")
// since we won't get a buffer decrement in p.sendNoBuf, we must do it here
p.itemsInBuffer--
} else {
// send to the non-buffered serial port writer
//log.Printf("About to send to p.sendNoBuf channel. cmd:%v", data)
data.willHandleCompleteResponse = willHandleCompleteResponse
p.sendNoBuf <- data
}
}
msgstr := "writerBuffered just got closed. make sure you make a new one. port:" + p.portConf.Name
log.Println(msgstr)
h.broadcastSys <- []byte(msgstr)
}
// this method runs as its own thread because it's instantiated
// as a "go" method. so if it blocks inside, it is ok
func (p *serport) writerNoBuf() {
// this for loop blocks on p.send until that channel
// sees something come in
for data := range p.sendNoBuf {
log.Printf("Got p.sendNoBuf. id:%v, pause:%v, data:%v\n", string(data.id), data.pause, strings.Replace(string(data.data), "\n", "\\n", -1))
// if we get here, we were able to write successfully
// to the serial port because it blocks until it can write
// decrement counter
p.itemsInBuffer--
log.Printf("Items In SPJS Queue List:%v\n", p.itemsInBuffer)
//h.broadcastSys <- []byte("{\"Cmd\":\"Write\",\"QCnt\":" + strconv.Itoa(p.itemsInBuffer) + ",\"Byte\":" + strconv.Itoa(n2) + ",\"Port\":\"" + p.portConf.Name + "\"}")
// Figure out buffered or not buffered
buf := "Buf"
if data.skippedBuffer {
buf = "NoBuf"
}
// WARNING - Feedrate Override doesn't really belong in here because this is supposed
// to be a generic implementation of sending/receiving to serial ports
// However, there's not really a better place to put this because you need to know
// last minute what the feedrate override is and let the user adjust it at any time
// If you want a generic serial port implementation, remove this last minute call from this code
didWeOverride := false
newData := ""
if p.isFeedRateOverrideOn {
didWeOverride, newData = doFeedRateOverride(data.data, p.feedRateOverride)
}
if didWeOverride {
// We need to reset the gcode and make the qwReport be what we want
// Since we changed the gcode, we need to report it back to the user
// For reducing load on websocket, stop transmitting write data
data.data = newData
qwr := qwReportWithData{
Cmd: "Write",
QCnt: p.itemsInBuffer,
Id: string(data.id),
D: string(data.data),
Buf: buf,
P: p.portConf.Name,
}
qwrJson, _ := json.Marshal(qwr)
h.broadcastSys <- qwrJson
} else {
// For reducing load on websocket, stop transmitting write data
qwr := qwReport{
Cmd: "Write",
QCnt: p.itemsInBuffer,
Id: string(data.id),
D: string(data.data),
Buf: buf,
P: p.portConf.Name,
}
qwrJson, _ := json.Marshal(qwr)
h.broadcastSys <- qwrJson
}
// FINALLY, OF ALL THE CODE IN THIS PROJECT
// WE TRULY/FINALLY GET TO WRITE TO THE SERIAL PORT!
_, err := p.portIo.Write([]byte(data.data)) // n2, err :=
// New Pause capability after we write. Added 9/23/15
// This was needed because many Atmel microcontrollers just plain drop serial data
// if it's being sent over while an EEPROM write is in play, so SPJS now
// let's the user specify a pause after a serial command to solve for this error
if data.pause > 0 {
log.Printf("We are sleeping after the port write for milliseconds:%v\n", data.pause)
time.Sleep(time.Duration(data.pause) * time.Millisecond)
}
// see if we need to send back the completeResponse
if data.willHandleCompleteResponse == false {
// we need to send back complete response
// Send fake cmd:"Complete" back
//strCmd := data.data
m := CmdComplete{"CompleteFake", data.id, p.portConf.Name, -1, data.data}
msgJson, err := json.Marshal(m)
if err == nil {
h.broadcastSys <- msgJson
}
}
//log.Print("Just wrote ", n2, " bytes to serial: ", string(data.data))
//log.Print(n2)
//log.Print(" bytes to serial: ")
//log.Print(data)
if err != nil {
errstr := "Error writing to " + p.portConf.Name + " " + err.Error() + " Closing port."
log.Print(errstr)
h.broadcastSys <- []byte(errstr)
break
}
}
msgstr := "Shutting down writer on " + p.portConf.Name
log.Println(msgstr)
h.broadcastSys <- []byte(msgstr)
p.portIo.Close()
}
var spmutex = &sync.Mutex{}
var spIsOpening = false
func spHandlerOpen(portname string, baud int, buftype string, isSecondary bool) {
log.Print("Inside spHandler")
spmutex.Lock()
if spIsOpening {
log.Println("We are currently in the middle of opening a port. Returning...")
return
}
spIsOpening = true
var out bytes.Buffer
out.WriteString("Opening serial port ")
out.WriteString(portname)
out.WriteString(" at ")
out.WriteString(strconv.Itoa(baud))
out.WriteString(" baud")
log.Print(out.String())
//h.broadcast <- []byte("Opened a serial port ")
//h.broadcastSys <- out.Bytes()
isPrimary := true
if isSecondary {
isPrimary = false
}
//options := serial.RawOptions
//options.BitRate = 1200
//options.FlowControl = serial.FLOWCONTROL_RTSCTS
//p, err := options.Open(portname)
// Needed for original serial library
conf := &serial.Config{}
conf.Baud = baud
conf.Name = portname
conf.RtsOn = true
conf.DtrOn = false
// Needed for Arduino serial library
/*
conf := &SerialConfig{Name: portname, Baud: baud, RtsOn: true}
conf.DtrOn = false
*/
/*
// Needed for Arduino serial library
mode := &serial.Mode{
BaudRate: baud,
Vmin: 0,
Vtimeout: 10,
}
*/
//mode.DataBits = 7
//mode.Parity = 0
//mode.StopBits = 1
// Needed for original serial library
sp, err := serial.OpenPort(conf)
// Needed for Arduino serial library
//sp, err := serial.OpenPort(portname, mode)
log.Print("Just tried to open port")
if err != nil {
//log.Fatal(err)
log.Print("Error opening port " + err.Error())
//h.broadcastSys <- []byte("Error opening port. " + err.Error())
h.broadcastSys <- []byte("{\"Cmd\":\"OpenFail\",\"Desc\":\"Error opening port. " + err.Error() + "\",\"Port\":\"" + conf.Name + "\",\"Baud\":" + strconv.Itoa(conf.Baud) + "}")
return
}
log.Print("Opened port successfully")
//p := &serport{send: make(chan []byte, 256), portConf: conf, portIo: sp}
// we can go up to 500,000 lines of gcode in the buffer
p := &serport{sendBuffered: make(chan Cmd, 500000), sendNoBuf: make(chan Cmd), portConf: conf, portIo: sp, BufferType: buftype, IsPrimary: isPrimary, IsSecondary: isSecondary, isFeedRateOverrideOn: false}
// if user asked for a buffer watcher, i.e. tinyg/grbl then attach here
if buftype == "tinyg_old" {
bw := &BufferflowTinyg{Name: "tinyg", parent_serport: p}
bw.Init()
bw.Port = portname
p.bufferwatcher = bw
} else if buftype == "tinyg" {
bw := &BufferflowTinygV2{Name: "tinyg_v2", parent_serport: p}
bw.Init()
bw.Port = portname
p.bufferwatcher = bw
} else if buftype == "tinygg2" {
bw := &BufferflowTinygG2{Name: "tinygg2", parent_serport: p}
bw.Init()
bw.Port = portname
p.bufferwatcher = bw
} else if buftype == "tinyg_linemode" {
bw := &BufferflowTinygPktMode{Name: "tinyg_linemode", parent_serport: p}
bw.Init()
bw.Port = portname
p.bufferwatcher = bw
} else if buftype == "tinyg_tidmode" {
bw := &BufferflowTinygTidMode{Name: "tinyg_tidmode", parent_serport: p}
bw.Init()
bw.Port = portname
p.bufferwatcher = bw
} else if buftype == "dummypause" {
// this is a dummy pause type bufferflow object
// to test artificially a delay on the serial port write
// it just pauses 3 seconds on each serial port write
bw := &BufferflowDummypause{}
bw.Init()
bw.Port = portname
p.bufferwatcher = bw
} else if buftype == "timed" {
// this is a timed bufferflow taken from what the Arduino
// guys did to reduce the amount of json packets coming
// back from the server. by adding a timer we can collect data
// first and then send back. we only add 16ms so it's not too bad
bw := &BufferflowTimed{Name: "timed", Port: portname, Output: h.broadcastSys, Input: make(chan string)}
bw.Init()
bw.Port = portname
p.bufferwatcher = bw
} else if buftype == "nodemcu" {
// nodemcu buffer only sends data back per line (which might be a bad call)
// and it only sends 1 line at a time to the device and releases the next line
// when it sees a > come back
bw := &BufferflowNodeMcu{Name: "nodemcu", Port: portname}
bw.Init()
p.bufferwatcher = bw
} else if buftype == "grbl" {
// grbl bufferflow
// store port as parent_serport for use in intializing a status query loop for '?'
bw := &BufferflowGrbl{Name: "grbl", parent_serport: p}
bw.Init()
bw.Port = portname
p.bufferwatcher = bw
} else if buftype == "marlin" {
// marlin bufferflow
// store port as parent_serport for use in intializing a status query loop for '?'
bw := &BufferflowMarlin{Name: "marlin", parent_serport: p}
bw.Init()
bw.Port = portname
p.bufferwatcher = bw
} else {
bw := &BufferflowDefault{}
bw.Init()
bw.Port = portname
p.bufferwatcher = bw
}
sh.register <- p
defer func() { sh.unregister <- p }()
// this is internally buffered thread to not send to serial port if blocked
go p.writerBuffered()
// this is thread to send to serial port regardless of block
go p.writerNoBuf()
p.reader()
//go p.reader()
//p.done = make(chan bool)
//<-p.done
spIsOpening = false
spmutex.Unlock()
}
func spHandlerCloseExperimental(p *serport) {
h.broadcastSys <- []byte("Pre-closing serial port " + p.portConf.Name)
p.isClosing = true
//close the port
p.bufferwatcher.Close()
p.portIo.Close()
h.broadcastSys <- []byte("Bufferwatcher closed")
p.portIo.Close()
//elicit response from hardware to close out p.reader()
//_, _ = p.portIo.Write([]byte("?"))
//p.portIo.Read(nil)
//close(p.portIo)
h.broadcastSys <- []byte("portIo closed")
close(p.sendBuffered)
h.broadcastSys <- []byte("p.sendBuffered closed")
close(p.sendNoBuf)
h.broadcastSys <- []byte("p.sendNoBuf closed")
//p.done <- true
// unregister myself
// we already have a deferred unregister in place from when
// we opened. the only thing holding up that thread is the p.reader()
// so if we close the reader we should get an exit
h.broadcastSys <- []byte("Closing serial port " + p.portConf.Name)
}
func spHandlerClose(p *serport) {
p.isClosing = true
//close the port
//elicit response from hardware to close out p.reader()
_, _ = p.portIo.Write([]byte("?"))
p.bufferwatcher.Close()
p.portIo.Close()
// unregister myself
// we already have a deferred unregister in place from when
// we opened. the only thing holding up that thread is the p.reader()
// so if we close the reader we should get an exit
h.broadcastSys <- []byte("Closing serial port " + p.portConf.Name)
}