@@ -26,12 +26,24 @@ type writeRequestJson struct {
26
26
Data []writeRequestJsonData
27
27
}
28
28
29
+ type writeRequestJsonRaw struct {
30
+ p * serport
31
+ P string
32
+ Data []writeRequestJsonDataRaw
33
+ }
34
+
29
35
type writeRequestJsonData struct {
30
36
D string
31
37
Id string
32
38
Buf string
33
39
}
34
40
41
+ type writeRequestJsonDataRaw struct {
42
+ D []byte
43
+ Id string
44
+ Buf string
45
+ }
46
+
35
47
type qReportJson struct {
36
48
Cmd string
37
49
QCnt int
@@ -73,7 +85,8 @@ type serialhub struct {
73
85
unregister chan * serport
74
86
75
87
// regexp for json trimming
76
- reJsonTrim * regexp.Regexp
88
+ reJsonTrim * regexp.Regexp
89
+ reJsonRawTrim * regexp.Regexp
77
90
}
78
91
79
92
type SpPortList struct {
@@ -103,12 +116,13 @@ var NetworkPorts SpPortList
103
116
104
117
var sh = serialhub {
105
118
//write: make(chan *serport, chan []byte),
106
- write : make (chan writeRequest ),
107
- writeJson : make (chan writeRequestJson ),
108
- register : make (chan * serport ),
109
- unregister : make (chan * serport ),
110
- ports : make (map [* serport ]bool ),
111
- reJsonTrim : regexp .MustCompile ("sendjson" ),
119
+ write : make (chan writeRequest ),
120
+ writeJson : make (chan writeRequestJson ),
121
+ register : make (chan * serport ),
122
+ unregister : make (chan * serport ),
123
+ ports : make (map [* serport ]bool ),
124
+ reJsonTrim : regexp .MustCompile ("sendjson" ),
125
+ reJsonRawTrim : regexp .MustCompile ("sendjsonraw" ),
112
126
}
113
127
114
128
func (sh * serialhub ) run () {
@@ -615,6 +629,64 @@ func spWriteJson(arg string) {
615
629
sh .writeJson <- m
616
630
}
617
631
632
+ func spWriteJsonRaw (arg string ) {
633
+
634
+ log .Printf ("spWriteJson. arg:%v\n " , arg )
635
+
636
+ // remove sendjson string
637
+ arg = sh .reJsonRawTrim .ReplaceAllString (arg , "" )
638
+ //log.Printf("string we're going to parse:%v\n", arg)
639
+
640
+ // this is a structured command now for sending in serial commands multiple at a time
641
+ // with an ID so we can send back the ID when the command is done
642
+ var m writeRequestJsonRaw
643
+ /*
644
+ m.P = "COM22"
645
+ var data writeRequestJsonData
646
+ data.Id = "234"
647
+ str := "yeah yeah"
648
+ data.D = str //[]byte(str) //[]byte(string("blah blah"))
649
+ m.Data = append(m.Data, data)
650
+ //m.Data = append(m.Data, data)
651
+ bm, err2 := json.Marshal(m)
652
+ if err2 == nil {
653
+ log.Printf("Test json serialize:%v\n", string(bm))
654
+ }
655
+ */
656
+
657
+ err := json .Unmarshal ([]byte (arg ), & m )
658
+
659
+ if err != nil {
660
+ log .Printf ("Problem decoding json. giving up. json:%v, err:%v\n " , arg , err )
661
+ spErr (fmt .Sprintf ("Problem decoding json. giving up. json:%v, err:%v" , arg , err ))
662
+ return
663
+ }
664
+
665
+ // see if we have this port open
666
+ portname := m .P
667
+ myport , isFound := findPortByName (portname )
668
+
669
+ if ! isFound {
670
+ // we couldn't find the port, so send err
671
+ spErr ("We could not find the serial port " + portname + " that you were trying to write to." )
672
+ return
673
+ }
674
+
675
+ // we found our port
676
+ m .p = myport
677
+
678
+ var mr writeRequestJson
679
+
680
+ mr .p = m .p
681
+ mr .P = m .P
682
+ var data writeRequestJsonData
683
+ data .D = string (m .Data [0 ].D )
684
+ mr .Data = append (mr .Data , data )
685
+
686
+ // send it to the writeJson channel
687
+ sh .writeJson <- mr
688
+ }
689
+
618
690
func spWrite (arg string ) {
619
691
// we will get a string of comXX asdf asdf asdf
620
692
log .Println ("Inside spWrite arg: " + arg )
0 commit comments