-
Notifications
You must be signed in to change notification settings - Fork 174
/
serial.go
executable file
·758 lines (651 loc) · 21.2 KB
/
serial.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// Supports Windows, Linux, Mac, BeagleBone Black, and Raspberry Pi
package main
import (
//"bufio"
"encoding/json"
"fmt"
//"path/filepath"
//"github.com/kballard/go-shellquote"
//"github.com/johnlauer/goserial"
//"github.com/mikepb/go-serial"
//"github.com/facchinm/go-serial"
//"github.com/kardianos/osext"
"log"
//"os"
"regexp"
"runtime/debug"
"strconv"
"strings"
//"time"
)
type writeRequest struct {
p *serport
d string
buffer bool
id string
pause int
}
type writeRequestJson struct {
p *serport
P string
Data []writeRequestJsonData
}
type writeRequestJsonData struct {
D string
Id string
Buf string
Pause int
}
type qReportJson struct {
Cmd string
QCnt int
P string
Data []qReportJsonData
}
type qReportJsonData struct {
D string
Id string
Buf string `json:"-"`
Parts int `json:"-"`
Pause int
}
type qReport struct {
Cmd string
QCnt int
Type []string `json:"-"`
Ids []string
D []string //`json:"-"`
Port string
}
type serialhub struct {
// Opened serial ports.
ports map[*serport]bool
//open chan *io.ReadWriteCloser
//write chan *serport, chan []byte
write chan writeRequest
//read chan []byte
writeJson chan writeRequestJson
// Register requests from the connections.
register chan *serport
// Unregister requests from connections.
unregister chan *serport
// regexp for json trimming
reJsonTrim *regexp.Regexp
}
type SpPortList struct {
SerialPorts []SpPortItem
}
type SpPortItem struct {
Name string
Friendly string
SerialNumber string
DeviceClass string
IsOpen bool
IsPrimary bool
RelatedNames []string
Baud int
BufferAlgorithm string
AvailableBufferAlgorithms []string
Ver float32
UsbVid string
UsbPid string
FeedRateOverride float32
}
var sh = serialhub{
//write: make(chan *serport, chan []byte),
write: make(chan writeRequest),
writeJson: make(chan writeRequestJson),
register: make(chan *serport),
unregister: make(chan *serport),
ports: make(map[*serport]bool),
reJsonTrim: regexp.MustCompile("sendjson"),
}
func (sh *serialhub) run() {
log.Print("Inside run of serialhub")
//cmdIdCtr := 0
//s := ser.open()
//ser.s := s
//ser.write(s, []byte("hello serial data"))
for {
select {
case p := <-sh.register:
log.Print("Registering a port: ", p.portConf.Name)
isPrimary := "false"
if p.IsPrimary {
isPrimary = "true"
}
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + p.portConf.Name + "\",\"IsPrimary\":" + isPrimary + ",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + ",\"BufferType\":\"" + p.BufferType + "\"}")
//log.Print(p.portConf.Name)
sh.ports[p] = true
case p := <-sh.unregister:
log.Print("Unregistering a port: ", p.portConf.Name)
h.broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + "}")
delete(sh.ports, p)
close(p.sendBuffered)
close(p.sendNoBuf)
case wrj := <-sh.writeJson:
// if the user sent in the commands as json
writeJson(wrj)
case wr := <-sh.write:
// if user sent in the commands as one text mode line
write(wr, "")
}
}
}
func writeJson(wrj writeRequestJson) {
// we'll parse this json request and then do a write() as if
// the cmd was sent in as text mode
// create array to hold our qReportJsonData
qReportDataArr := []qReportJsonData{}
for _, cmdJson := range wrj.Data {
var wr writeRequest
wr.d = cmdJson.D //[]byte(cmdJson.D)
//wr.id = cmdJson.Id
wr.p = wrj.p
if cmdJson.Buf == "Buf" {
wr.buffer = true
} else if cmdJson.Buf == "NoBuf" {
wr.buffer = false
} else {
wr.buffer = true
}
//write(wr, cmdJson.Id, true)
// handle our new pause value as of 9/23/15
wr.pause = cmdJson.Pause
// we are sending 1 cmd in, but we may get back multiple cmds
// because the BreakApartCommands() can add/modify stuff, so keep
// that in mind
cmds, idArr, bufTypeArr := createCommands(wr, cmdJson.Id)
for index, _ := range cmds {
// create q report data
cmdId := idArr[index]
// append stuff to the id if this cmd was one line and just got broken up
// the first line will have a normal id like "123"
// the 2nd, 3rd,e tc line will have id like "123-part-2-3", "123-part-3-3"
if index > 0 {
cmdId = fmt.Sprintf("%v-part-%v-%v", cmdId, (index + 1), len(cmds))
}
qrd := qReportJsonData{D: cmds[index], Id: cmdId, Parts: len(cmds)}
// if user forced the buffer type, just use it
if cmdJson.Buf == "Buf" || cmdJson.Buf == "NoBuf" {
qrd.Buf = cmdJson.Buf
} else {
// else use the buffer type figured out in createCommands()
qrd.Buf = bufTypeArr[index]
}
// handle pause value
// when a user sends in multiple lines in one command and we break it apart,
// just assume that the pause will be the same on subsequent lines
qrd.Pause = cmdJson.Pause
qReportDataArr = append(qReportDataArr, qrd)
}
}
// do our own report
qr := qReportJson{
Cmd: "Queued",
Data: qReportDataArr,
QCnt: wrj.p.itemsInBuffer,
P: wrj.p.portConf.Name,
}
json, _ := json.Marshal(qr)
h.broadcastSys <- json
// now send off the commands to the appropriate channel
for _, qrd := range qReportDataArr {
if qrd.Buf == "Buf" {
//log.Println("Json sending to wr.p.sendBuffered")
wrj.p.sendBuffered <- Cmd{qrd.D, qrd.Id, false, false, qrd.Pause}
} else {
//log.Println("Json sending to wr.p.sendNoBuf")
// Need to see if we should rewrite the serial command though
newCmd := wrj.p.bufferwatcher.RewriteSerialData(qrd.D, qrd.Id)
if len(newCmd) > 0 {
qrd.D = newCmd
log.Printf("The serial data got rewritten on a NoBuf cmd. new cmd:%v", qrd.D)
}
wrj.p.sendNoBuf <- Cmd{qrd.D, qrd.Id, true, false, qrd.Pause}
}
}
// garbage collect
if *gcType == "max" {
debug.FreeOSMemory()
}
log.Println("Done with writeJson method")
}
func write(wr writeRequest, id string) {
cmds, idArr, bufTypeArr := createCommands(wr, id)
qr := qReport{
Cmd: "Queued",
//Type: bufTypeArr,
Ids: idArr,
D: cmds,
QCnt: wr.p.itemsInBuffer,
Port: wr.p.portConf.Name,
}
json, _ := json.Marshal(qr)
h.broadcastSys <- json
// now send off the commands to the appropriate channel
for index, cmdToSendToChannel := range cmds {
//cmdIdCtr++
//cmdId := "fakeid-" + strconv.Itoa(cmdIdCtr)
cmdId := idArr[index]
if bufTypeArr[index] == "Buf" {
//log.Println("Send was normal send, so sending to wr.p.sendBuffered")
wr.p.sendBuffered <- Cmd{cmdToSendToChannel, cmdId, false, false, 0}
} else {
//log.Println("Send was sendnobuf, so sending to wr.p.sendNoBuf")
// Need to see if we should rewrite the serial command though
newCmd := wr.p.bufferwatcher.RewriteSerialData(cmdToSendToChannel, cmdId)
if len(newCmd) > 0 {
cmdToSendToChannel = newCmd
log.Printf("The serial data got rewritten on a NoBuf. new cmd:%v", cmdToSendToChannel)
}
wr.p.sendNoBuf <- Cmd{cmdToSendToChannel, cmdId, true, false, 0}
}
}
// garbage collect
if *gcType == "max" {
debug.FreeOSMemory()
}
}
func createCommands(wr writeRequest, id string) ([]string, []string, []string) {
//log.Print("Got a write to a port")
//log.Print("Port: ", string(wr.p.portConf.Name))
//log.Print(wr.p)
//log.Print("Data is: ")
//log.Println(wr.d)
//log.Print("Data:" + string(wr.d))
//log.Print("-----")
//log.Printf("Got write to id:%v, port:%v, buffer:%v, data:%v", id, wr.p.portConf.Name, wr.buffer, string(wr.d))
dataCmd := string(wr.d)
// break the data into individual commands for queuing
// this is important because:
// 1) we could be sent multiple serial commands at once and the
// serial device may want them sent in smaller chunks to give
// better feedback. For example, if we're sent G0 X0\nG0 Y10\n we
// could happily send that to a CNC controller like a TinyG. However,
// on something like TinyG that would chew up 2 buffer planners. So,
// to better match what will happen, we break those into 2 commands
// so we get a better granularity of getting back qr responses or
// other feedback.
// 2) we need to break apart specific commands potentially that do
// not need newlines. For example, on TinyG we need !~% to be different
// commands because we need to pivot off of what they mean. ! means pause
// the sending. So, we need that command as its own command in order of
// how they were sent to us.
cmds := wr.p.bufferwatcher.BreakApartCommands(dataCmd)
dataArr := []string{}
bufTypeArr := []string{}
idArr := []string{}
for _, cmd := range cmds {
// push this cmd onto dataArr for reporting
dataArr = append(dataArr, cmd)
idArr = append(idArr, id)
// do extra check to see if certain command should wipe out
// the entire internal serial port buffer we're holding in wr.p.sendBuffered
wipeBuf := wr.p.bufferwatcher.SeeIfSpecificCommandsShouldWipeBuffer(cmd)
if wipeBuf {
log.Printf("We got a command that is asking us to wipe the sendBuffered buf. cmd:%v\n", cmd)
// just wipe out the current channel and create new
// hopefully garbage collection works here
// close the channel
//close(wr.p.sendBuffered)
// consume all stuff queued
func() {
ctr := 0
/*
for data := range wr.p.sendBuffered {
log.Printf("Consuming sendBuffered queue. d:%v\n", string(data))
ctr++
}*/
keepLooping := true
for keepLooping {
select {
case d, ok := <-wr.p.sendBuffered:
log.Printf("Consuming sendBuffered queue. ok:%v, d:%v, id:%v\n", ok, string(d.data), string(d.id))
ctr++
// since we just consumed a buffer item, we need to decrement bufcount
// we are doing this artificially because we artifically threw
// away what was in the bufer
wr.p.itemsInBuffer--
if ok == false {
keepLooping = false
}
default:
keepLooping = false
log.Println("Hit default in select clause")
}
}
log.Printf("Done consuming sendBuffered cmds. ctr:%v\n", ctr)
}()
// we still will likely have a sendBuffered that is in the BlockUntilReady()
// that we have to deal with so it doesn't send to the serial port
// when we release it
// send semaphore release if there is one on the BlockUntilReady()
// this method will release the BlockUntilReady() but with an unblock
// of type 2 which means cancel the send
wr.p.bufferwatcher.ReleaseLock()
// let user know we wiped queue
log.Printf("itemsInBuffer:%v\n", wr.p.itemsInBuffer)
h.broadcastSys <- []byte("{\"Cmd\":\"WipedQueue\",\"QCnt\":" + strconv.Itoa(wr.p.itemsInBuffer) + ",\"Port\":\"" + wr.p.portConf.Name + "\"}")
}
// do extra check to see if any specific commands should pause
// the buffer. this means we'll trigger a BlockUntilReady() block
pauseBuf := wr.p.bufferwatcher.SeeIfSpecificCommandsShouldPauseBuffer(cmd)
if pauseBuf {
log.Printf("We need to manually pause our internal buffer.\n")
wr.p.bufferwatcher.SetManualPaused(true)
wr.p.bufferwatcher.Pause()
}
// do extra check to see if any specific commands should unpause
// the buffer. this means we'll release the BlockUntilReady() block
unpauseBuf := wr.p.bufferwatcher.SeeIfSpecificCommandsShouldUnpauseBuffer(cmd)
if unpauseBuf {
log.Printf("We need to unpause our internal buffer.\n")
wr.p.bufferwatcher.SetManualPaused(false)
wr.p.bufferwatcher.Unpause()
}
// do extra check to see if certain commands for this buffer type
// should skip the internal serial port buffering
// for example ! on tinyg and grbl should skip
typeBuf := "" // set in if stmt below for reporting afterwards
if wr.buffer {
bufferSkip := wr.p.bufferwatcher.SeeIfSpecificCommandsShouldSkipBuffer(cmd)
if bufferSkip {
log.Printf("Forcing this cmd to skip buffer. cmd:%v\n", cmd)
//wr.buffer = false
typeBuf = "NoBuf"
} else {
typeBuf = "Buf"
}
} else {
typeBuf = "NoBuf"
}
/*
if wr.buffer {
//log.Println("Send was normal send, so sending to wr.p.sendBuffered")
//wr.p.sendBuffered <- []byte(cmd)
typeBuf = "Buf"
} else {
//log.Println("Send was sendnobuf, so sending to wr.p.sendNoBuf")
//wr.p.sendNoBuf <- []byte(cmd)
typeBuf = "NoBuf"
}
*/
// increment queue counter for reporting
wr.p.itemsInBuffer++
//log.Printf("itemsInBuffer:%v\n", wr.p.itemsInBuffer)
// push the type of this command to bufTypeArr
bufTypeArr = append(bufTypeArr, typeBuf)
} // for loop on broken apart commands
return cmds, idArr, bufTypeArr
}
func writeToChannels(cmds []string, idArr []string, bufTypeArr []string) {
}
func spList() {
// call our os specific implementation of getting the serial list
list, _ := GetList()
// do a quick loop to see if any of our open ports
// did not end up in the list port list. this can
// happen on windows in a fallback scenario where an
// open port can't be identified because it is locked,
// so just solve that by manually inserting
for port := range sh.ports {
isFound := false
for _, item := range list {
if strings.ToLower(port.portConf.Name) == strings.ToLower(item.Name) {
isFound = true
}
}
if !isFound {
// artificially push to front of port list
log.Println(fmt.Sprintf("Did not find an open port in the serial port list. We are going to artificially push it onto the list. port:%v", port.portConf.Name))
var ossp OsSerialPort
ossp.Name = port.portConf.Name
ossp.FriendlyName = port.portConf.Name
list = append([]OsSerialPort{ossp}, list...)
}
}
// we have a full clean list of ports now. iterate thru them
// to append the open/close state, baud rates, etc to make
// a super clean nice list to send back to browser
n := len(list)
spl := SpPortList{make([]SpPortItem, n, n)}
// now try to get the meta data for the ports. keep in mind this may fail
// to give us anything
metaports, err := GetMetaList()
log.Printf("Got metadata on ports:%v", metaports)
ctr := 0
for _, item := range list {
/*
Name string
Friendly string
IsOpen bool
IsPrimary bool
RelatedNames []string
Baud int
RtsOn bool
DtrOn bool
BufferAlgorithm string
AvailableBufferAlgorithms []string
Ver float32
*/
spl.SerialPorts[ctr] = SpPortItem{
Name: item.Name,
Friendly: item.FriendlyName,
SerialNumber: item.SerialNumber,
DeviceClass: item.DeviceClass,
IsOpen: false,
IsPrimary: false,
RelatedNames: item.RelatedNames,
Baud: 0,
BufferAlgorithm: "",
AvailableBufferAlgorithms: availableBufferAlgorithms,
Ver: versionFloat,
UsbPid: item.IdProduct,
UsbVid: item.IdVendor,
}
// if we have meta data for this port, use it
if len(metaports) > 0 {
setMetaData(&spl.SerialPorts[ctr], metaports)
}
// figure out if port is open
//spl.SerialPorts[ctr].IsOpen = false
myport, isFound := findPortByName(item.Name)
if isFound {
// we found our port
spl.SerialPorts[ctr].IsOpen = true
spl.SerialPorts[ctr].Baud = myport.portConf.Baud
spl.SerialPorts[ctr].BufferAlgorithm = myport.BufferType
spl.SerialPorts[ctr].IsPrimary = myport.IsPrimary
spl.SerialPorts[ctr].FeedRateOverride = myport.feedRateOverride
}
//ls += "{ \"name\" : \"" + item.Name + "\", \"friendly\" : \"" + item.FriendlyName + "\" },\n"
ctr++
}
// we are getting a crash here, so thinking it's like a null pointer. do some further
// debug and set default values
log.Printf("About to marshal the serial port list. spl:%v", spl)
ls, err := json.MarshalIndent(spl, "", "\t")
if err != nil {
log.Println(err)
h.broadcastSys <- []byte("Error creating json on port list " +
err.Error())
} else {
//log.Print("Printing out json byte data...")
//log.Print(ls)
h.broadcastSys <- ls
}
}
func setMetaData(pi *SpPortItem, metadata []OsSerialPort) {
// loop thru metadata and see if this port (pi) is in the list
for _, mi := range metadata {
if pi.Name == mi.Name {
// we have a winner
pi.Friendly = mi.FriendlyName
pi.DeviceClass = mi.DeviceClass
pi.SerialNumber = mi.SerialNumber
pi.RelatedNames = mi.RelatedNames
pi.UsbPid = mi.IdProduct
pi.UsbVid = mi.IdVendor
break
}
}
}
func setMetaDataForOsSerialPort(pi *OsSerialPort, metadata []OsSerialPort) {
// loop thru metadata and see if this port (pi) is in the list
for _, mi := range metadata {
if pi.Name == mi.Name {
// we have a winner
pi.FriendlyName = mi.FriendlyName
pi.IdProduct = mi.IdProduct
pi.IdVendor = mi.IdVendor
pi.Manufacturer = mi.Manufacturer
pi.DeviceClass = mi.DeviceClass
pi.SerialNumber = mi.SerialNumber
pi.RelatedNames = mi.RelatedNames
break
}
}
}
/*
func spListOld() {
ls := "{\"serialports\" : [\n"
list, _ := getList()
for _, item := range list {
ls += "{ \"name\" : \"" + item.Name + "\", \"friendly\" : \"" + item.FriendlyName + "\" },\n"
}
ls = strings.TrimSuffix(ls, "},\n")
ls += "}\n"
ls += "]}\n"
h.broadcastSys <- []byte(ls)
}*/
func spErr(err string) {
log.Println("Sending err back: ", err)
//h.broadcastSys <- []byte(err)
h.broadcastSys <- []byte("{\"Error\" : \"" + err + "\"}")
}
func spClose(portname string) {
// look up the registered port by name
// then call the close method inside serialport
// that should cause an unregister channel call back
// to myself
myport, isFound := findPortByName(portname)
if isFound {
// we found our port
spHandlerClose(myport)
} else {
// we couldn't find the port, so send err
spErr("We could not find the serial port " + portname + " that you were trying to close.")
}
}
func spWriteJson(arg string) {
//log.Printf("spWriteJson. arg:%v\n", arg)
// remove sendjson string
arg = sh.reJsonTrim.ReplaceAllString(arg, "")
//log.Printf("string we're going to parse:%v\n", arg)
// this is a structured command now for sending in serial commands multiple at a time
// with an ID so we can send back the ID when the command is done
var m writeRequestJson
/*
m.P = "COM22"
var data writeRequestJsonData
data.Id = "234"
str := "yeah yeah"
data.D = str //[]byte(str) //[]byte(string("blah blah"))
m.Data = append(m.Data, data)
//m.Data = append(m.Data, data)
bm, err2 := json.Marshal(m)
if err2 == nil {
log.Printf("Test json serialize:%v\n", string(bm))
}
*/
err := json.Unmarshal([]byte(arg), &m)
if err != nil {
log.Printf("Problem decoding json. giving up. json:%v, err:%v\n", arg, err)
spErr(fmt.Sprintf("Problem decoding json. giving up. json:%v, err:%v", arg, err))
return
}
// see if we have this port open
portname := m.P
myport, isFound := findPortByName(portname)
if !isFound {
// we couldn't find the port, so send err
spErr("We could not find the serial port " + portname + " that you were trying to write to.")
return
}
// we found our port
m.p = myport
// send it to the writeJson channel
sh.writeJson <- m
}
func spWrite(arg string) {
// we will get a string of comXX asdf asdf asdf
log.Println("Inside spWrite arg: " + arg)
arg = strings.TrimPrefix(arg, " ")
//log.Println("arg after trim: " + arg)
args := strings.SplitN(arg, " ", 3)
if len(args) != 3 {
errstr := "Could not parse send command: " + arg
log.Println(errstr)
spErr(errstr)
return
}
portname := strings.Trim(args[1], " ")
//log.Println("The port to write to is:" + portname + "---")
//log.Println("The data is:" + args[2] + "---")
// see if we have this port open
myport, isFound := findPortByName(portname)
if !isFound {
// we couldn't find the port, so send err
spErr("We could not find the serial port " + portname + " that you were trying to write to.")
return
}
// we found our port
// create our write request
var wr writeRequest
wr.p = myport
// see if args[0] is send or sendnobuf
if args[0] != "sendnobuf" {
// we were just given a "send" so buffer it
wr.buffer = true
} else {
log.Println("sendnobuf specified so wr.buffer is false")
wr.buffer = false
}
// include newline or not in the write? that is the question.
// for now lets skip the newline
//wr.d = []byte(args[2] + "\n")
wr.d = args[2] //[]byte(args[2])
// send it to the write channel
sh.write <- wr
}
func findPortByName(portname string) (*serport, bool) {
portnamel := strings.ToLower(portname)
for port := range sh.ports {
if strings.ToLower(port.portConf.Name) == portnamel {
// we found our port
//spHandlerClose(port)
return port, true
}
}
return nil, false
}
func spBufferAlgorithms() {
//arr := []string{"default", "tinyg", "dummypause"}
arr := availableBufferAlgorithms
json := "{\"BufferAlgorithm\" : ["
for _, elem := range arr {
json += "\"" + elem + "\", "
}
json = regexp.MustCompile(", $").ReplaceAllString(json, "]}")
h.broadcastSys <- []byte(json)
}
func spBaudRates() {
arr := []string{"2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400"}
json := "{\"BaudRate\" : ["
for _, elem := range arr {
json += "" + elem + ", "
}
json = regexp.MustCompile(", $").ReplaceAllString(json, "]}")
h.broadcastSys <- []byte(json)
}