@@ -21,7 +21,6 @@ import (
2121 "io"
2222 "strconv"
2323 "time"
24- "unicode/utf8"
2524
2625 log "github.com/sirupsen/logrus"
2726 serial "go.bug.st/serial"
@@ -56,10 +55,7 @@ type serport struct {
5655 // channel containing raw base64 encoded binary data (outbound messages)
5756 sendRaw chan string
5857
59- // Do we have an extra channel/thread to watch our buffer?
60- BufferType string
61- //bufferwatcher *BufferflowDummypause
62- bufferwatcher Bufferflow
58+ bufferFlow * BufferflowTimed
6359}
6460
6561// SpPortMessage is the serial port message
@@ -74,10 +70,9 @@ type SpPortMessageRaw struct {
7470 D []byte // the data, i.e. G0 X0 Y0
7571}
7672
77- func (p * serport ) reader (buftype string ) {
73+ func (p * serport ) reader () {
7874
7975 timeCheckOpen := time .Now ()
80- var bufferedCh bytes.Buffer
8176
8277 serialBuffer := make ([]byte , 1024 )
8378 for {
@@ -95,39 +90,8 @@ func (p *serport) reader(buftype string) {
9590 // read can return legitimate bytes as well as an error
9691 // so process the n bytes red, if n > 0
9792 if n > 0 && err == nil {
98-
9993 log .Print ("Read " + strconv .Itoa (n ) + " bytes ch: " + string (bufferPart [:n ]))
100-
101- data := ""
102- switch buftype {
103- case "timedraw" , "timed" :
104- data = string (bufferPart [:n ])
105- // give the data to our bufferflow so it can do it's work
106- // to read/translate the data to see if it wants to block
107- // writes to the serialport. each bufferflow type will decide
108- // this on its own based on its logic
109- p .bufferwatcher .OnIncomingData (data )
110- case "default" : // the bufferbuftype is actually called default 🤷♂️
111- // save the left out bytes for the next iteration due to UTF-8 encoding
112- bufferPart = append (bufferedCh .Bytes (), bufferPart [:n ]... )
113- n += len (bufferedCh .Bytes ())
114- bufferedCh .Reset ()
115- for i , w := 0 , 0 ; i < n ; i += w {
116- runeValue , width := utf8 .DecodeRune (bufferPart [i :n ]) // try to decode the first i bytes in the buffer (UTF8 runes do not have a fixed length)
117- if runeValue == utf8 .RuneError {
118- bufferedCh .Write (bufferPart [i :n ])
119- break
120- }
121- if i == n {
122- bufferedCh .Reset ()
123- }
124- data += string (runeValue )
125- w = width
126- }
127- p .bufferwatcher .OnIncomingData (data )
128- default :
129- log .Panicf ("unknown buffer type %s" , buftype )
130- }
94+ p .bufferFlow .OnIncomingData (string (bufferPart [:n ]))
13195 }
13296
13397 // double check that we got characters in the buffer
@@ -272,7 +236,7 @@ func (p *serport) writerRaw() {
272236 h .broadcastSys <- []byte (msgstr )
273237}
274238
275- func spHandlerOpen (portname string , baud int , buftype string ) {
239+ func spHandlerOpen (portname string , baud int ) {
276240
277241 log .Print ("Inside spHandler" )
278242
@@ -311,23 +275,12 @@ func spHandlerOpen(portname string, baud int, buftype string) {
311275 portConf : conf ,
312276 portIo : sp ,
313277 portName : portname ,
314- BufferType : buftype }
315-
316- var bw Bufferflow
317-
318- switch buftype {
319- case "timed" :
320- bw = NewBufferflowTimed (portname , h .broadcastSys )
321- case "timedraw" :
322- bw = NewBufferflowTimedRaw (portname , h .broadcastSys )
323- case "default" :
324- bw = NewBufferflowDefault (portname , h .broadcastSys )
325- default :
326- log .Panicf ("unknown buffer type: %s" , buftype )
327278 }
328279
280+ bw := NewBufferFlowTimed (portname , h .broadcastSys )
329281 bw .Init ()
330- p .bufferwatcher = bw
282+
283+ p .bufferFlow = bw
331284
332285 sh .Register (p )
333286 defer sh .Unregister (p )
@@ -342,14 +295,14 @@ func spHandlerOpen(portname string, baud int, buftype string) {
342295 // this is thread to send to serial port but with base64 decoding
343296 go p .writerRaw ()
344297
345- p .reader (buftype )
298+ p .reader ()
346299
347300 serialPorts .List ()
348301}
349302
350303func (p * serport ) Close () {
351304 p .isClosing = true
352- p .bufferwatcher .Close ()
305+ p .bufferFlow .Close ()
353306 p .portIo .Close ()
354307 serialPorts .MarkPortAsClosed (p .portName )
355308 serialPorts .List ()
0 commit comments