File tree 1 file changed +25
-3
lines changed
1 file changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -3,11 +3,13 @@ package main
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
- log "github.com/Sirupsen/logrus"
7
- "go.bug.st/serial.v1"
8
6
"io"
9
7
"strconv"
10
8
"time"
9
+ "unicode/utf8"
10
+
11
+ log "github.com/Sirupsen/logrus"
12
+ serial "go.bug.st/serial.v1"
11
13
)
12
14
13
15
type SerialConfig struct {
@@ -95,6 +97,7 @@ func (p *serport) reader() {
95
97
//var buf bytes.Buffer
96
98
ch := make ([]byte , 1024 )
97
99
timeCheckOpen := time .Now ()
100
+ var buffered_ch bytes.Buffer
98
101
99
102
for {
100
103
@@ -108,11 +111,30 @@ func (p *serport) reader() {
108
111
break
109
112
}
110
113
114
+ ch = append (buffered_ch .Bytes (), ch [:n ]... )
115
+ n += len (buffered_ch .Bytes ())
116
+ buffered_ch .Reset ()
117
+
111
118
// read can return legitimate bytes as well as an error
112
119
// so process the bytes if n > 0
113
120
if n > 0 {
114
121
//log.Print("Read " + strconv.Itoa(n) + " bytes ch: " + string(ch))
115
- data := string (ch [:n ])
122
+
123
+ data := ""
124
+
125
+ for i , w := 0 , 0 ; i < n ; i += w {
126
+ runeValue , width := utf8 .DecodeRune (ch [i :n ])
127
+ if runeValue == utf8 .RuneError {
128
+ buffered_ch .Write (append (ch [i :n ]))
129
+ break
130
+ }
131
+ if i == n {
132
+ buffered_ch .Reset ()
133
+ }
134
+ data += string (runeValue )
135
+ w = width
136
+ }
137
+
116
138
//log.Print("The data i will convert to json is:")
117
139
//log.Print(data)
118
140
You can’t perform that action at this time.
0 commit comments