@@ -98,9 +98,10 @@ func (p *serport) reader(buftype string) {
98
98
timeCheckOpen := time .Now ()
99
99
var buffered_ch bytes.Buffer
100
100
101
+ serialBuffer := make ([]byte , 1024 )
101
102
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 ]
104
105
105
106
//if we detect that port is closing, break out of this for{} loop.
106
107
if p .isClosing {
@@ -114,22 +115,22 @@ func (p *serport) reader(buftype string) {
114
115
// so process the n bytes red, if n > 0
115
116
if n > 0 && err == nil {
116
117
117
- log .Print ("Read " + strconv .Itoa (n ) + " bytes ch: " + string (ch [:n ]))
118
+ log .Print ("Read " + strconv .Itoa (n ) + " bytes ch: " + string (bufferPart [:n ]))
118
119
119
120
data := ""
120
121
switch buftype {
121
122
case "timedraw" , "timed" , "timedbinary" :
122
- data = string (ch [:n ])
123
+ data = string (bufferPart [:n ])
123
124
p .bufferwatcher .OnIncomingData (data )
124
125
case "default" : // the bufferbuftype is actually called default 🤷♂️
125
126
// 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 ]... )
127
128
n += len (buffered_ch .Bytes ())
128
129
buffered_ch .Reset ()
129
130
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)
131
132
if runeValue == utf8 .RuneError {
132
- buffered_ch .Write (ch [i :n ])
133
+ buffered_ch .Write (bufferPart [i :n ])
133
134
break
134
135
}
135
136
if i == n {
0 commit comments