@@ -98,9 +98,10 @@ func (p *serport) reader(buftype string) {
9898 timeCheckOpen := time .Now ()
9999 var buffered_ch bytes.Buffer
100100
101+ serialBuffer := make ([]byte , 1024 )
101102 for {
102- ch := make ([] byte , 1024 ) //a new array of bytes is initilized everytime because we pass it (as a pointer) in a channel, it can be improved
103- n , err := p . portIo . Read ( ch )
103+ n , err := p . portIo . Read ( serialBuffer )
104+ bufferPart := serialBuffer [: n ]
104105
105106 //if we detect that port is closing, break out of this for{} loop.
106107 if p .isClosing {
@@ -114,22 +115,22 @@ func (p *serport) reader(buftype string) {
114115 // so process the n bytes red, if n > 0
115116 if n > 0 && err == nil {
116117
117- log .Print ("Read " + strconv .Itoa (n ) + " bytes ch: " + string (ch [:n ]))
118+ log .Print ("Read " + strconv .Itoa (n ) + " bytes ch: " + string (bufferPart [:n ]))
118119
119120 data := ""
120121 switch buftype {
121122 case "timedraw" , "timed" , "timedbinary" :
122- data = string (ch [:n ])
123+ data = string (bufferPart [:n ])
123124 p .bufferwatcher .OnIncomingData (data )
124125 case "default" : // the bufferbuftype is actually called default 🤷♂️
125126 // save the left out bytes for the next iteration due to UTF-8 encoding
126- ch = append (buffered_ch .Bytes (), ch [:n ]... ) // TODO ch is not handled correctly: doing this way its length is messed up. Use ch2
127+ bufferPart = append (buffered_ch .Bytes (), bufferPart [:n ]... )
127128 n += len (buffered_ch .Bytes ())
128129 buffered_ch .Reset ()
129130 for i , w := 0 , 0 ; i < n ; i += w {
130- runeValue , width := utf8 .DecodeRune (ch [i :n ]) // try to decode the first i bytes in the buffer (UTF8 runes do not have a fixed length)
131+ 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)
131132 if runeValue == utf8 .RuneError {
132- buffered_ch .Write (ch [i :n ])
133+ buffered_ch .Write (bufferPart [i :n ])
133134 break
134135 }
135136 if i == n {
0 commit comments