@@ -103,7 +103,7 @@ func (p *serport) reader(buftype string) {
103
103
104
104
n , err := p .portIo .Read (ch )
105
105
106
- //if we detect that port is closing, break out o this for{} loop.
106
+ //if we detect that port is closing, break out of this for{} loop.
107
107
if p .isClosing {
108
108
strmsg := "Shutting down reader on " + p .portConf .Name
109
109
log .Println (strmsg )
@@ -118,16 +118,20 @@ func (p *serport) reader(buftype string) {
118
118
}
119
119
120
120
// read can return legitimate bytes as well as an error
121
- // so process the bytes if n > 0
121
+ // so process the n bytes if n > 0
122
122
if n > 0 {
123
123
log .Print ("Read " + strconv .Itoa (n ) + " bytes ch: " + string (ch ))
124
124
125
125
data := ""
126
- if buftype == "timedraw" {
126
+ switch buftype {
127
+ case "timedraw" , "timed" : // data is handled differently inside BufferFlowTimed (bufferedOutput is string) and BufferFlowTimedRaw (bufferedOutputRaw is []byte)
127
128
data = string (ch [:n ])
128
- } else {
129
+ p .bufferwatcher .OnIncomingData (data )
130
+ case "timedbinary" :
131
+ p .bufferwatcher .OnIncomingDataBinary (ch [:n ])
132
+ case "default" :
129
133
for i , w := 0 , 0 ; i < n ; i += w {
130
- runeValue , width := utf8 .DecodeRune (ch [i :n ])
134
+ runeValue , width := utf8 .DecodeRune (ch [i :n ]) // try to decode the first i bytes in the buffer (UTF8 runes do not have a fixed lenght)
131
135
if runeValue == utf8 .RuneError {
132
136
buffered_ch .Write (append (ch [i :n ]))
133
137
break
@@ -138,20 +142,14 @@ func (p *serport) reader(buftype string) {
138
142
data += string (runeValue )
139
143
w = width
140
144
}
141
- }
142
-
143
- //log.Print("The data i will convert to json is:")
144
- //log.Print(data)
145
-
146
- // give the data to our bufferflow so it can do it's work
147
- // to read/translate the data to see if it wants to block
148
- // writes to the serialport. each bufferflow type will decide
149
- // this on its own based on its logic, i.e. tinyg vs grbl vs others
150
- //p.b.bufferwatcher..OnIncomingData(data)
151
- if buftype == "timedbinary" {
152
- p .bufferwatcher .OnIncomingDataBinary (ch [:n ])
153
- } else {
145
+ // give the data to our bufferflow so it can do it's work
146
+ // to read/translate the data to see if it wants to block
147
+ // writes to the serialport. each bufferflow type will decide
148
+ // this on its own based on its logic, i.e. tinyg vs grbl vs others
149
+ //p.b.bufferwatcher..OnIncomingData(data)
154
150
p .bufferwatcher .OnIncomingData (data )
151
+ default :
152
+ log .Panicf ("unknown buffer type %s" , buftype )
155
153
}
156
154
157
155
// see if the OnIncomingData handled the broadcast back
@@ -330,14 +328,17 @@ func spHandlerOpen(portname string, baud int, buftype string) {
330
328
331
329
var bw Bufferflow
332
330
333
- if buftype == "timed" {
331
+ switch buftype {
332
+ case "timed" :
334
333
bw = & BufferflowTimed {Name : "timed" , Port : portname , Output : h .broadcastSys , Input : make (chan string )}
335
- } else if buftype == "timedraw" {
334
+ case "timedraw" :
336
335
bw = & BufferflowTimedRaw {Name : "timedraw" , Port : portname , Output : h .broadcastSys , Input : make (chan string )}
337
- } else if buftype == "timedbinary" {
336
+ case "timedbinary" :
338
337
bw = & BufferflowTimedBinary {Name : "timedbinary" , Port : portname , Output : h .broadcastSys , Input : make (chan []byte )}
339
- } else {
338
+ case "default" :
340
339
bw = & BufferflowDefault {Port : portname }
340
+ default :
341
+ log .Panicf ("unknown buffer type: %s" , buftype )
341
342
}
342
343
343
344
bw .Init ()
0 commit comments