File tree Expand file tree Collapse file tree 1 file changed +25
-3
lines changed
Expand file tree Collapse file tree 1 file changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -3,11 +3,13 @@ package main
33import (
44 "bytes"
55 "encoding/json"
6- log "github.com/Sirupsen/logrus"
7- "go.bug.st/serial.v1"
86 "io"
97 "strconv"
108 "time"
9+ "unicode/utf8"
10+
11+ log "github.com/Sirupsen/logrus"
12+ serial "go.bug.st/serial.v1"
1113)
1214
1315type SerialConfig struct {
@@ -95,6 +97,7 @@ func (p *serport) reader() {
9597 //var buf bytes.Buffer
9698 ch := make ([]byte , 1024 )
9799 timeCheckOpen := time .Now ()
100+ var buffered_ch bytes.Buffer
98101
99102 for {
100103
@@ -108,11 +111,30 @@ func (p *serport) reader() {
108111 break
109112 }
110113
114+ ch = append (buffered_ch .Bytes (), ch [:n ]... )
115+ n += len (buffered_ch .Bytes ())
116+ buffered_ch .Reset ()
117+
111118 // read can return legitimate bytes as well as an error
112119 // so process the bytes if n > 0
113120 if n > 0 {
114121 //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+
116138 //log.Print("The data i will convert to json is:")
117139 //log.Print(data)
118140
You can’t perform that action at this time.
0 commit comments