Skip to content

Reinitialize serial buffer on every read to avoid concurrent rewrite #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Reinitialize serial buffer on every read to avoid concurrent rewrite
buffered_ch was being rewritten with data being read on a later Read()

Attention: this implementation "leaks" (ch will be garbage collected every time)
Please profile the memory usage before merging
  • Loading branch information
facchinm committed Nov 7, 2019
commit 153748632a037cd6750f88e3a36f573e8ce21162
8 changes: 3 additions & 5 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ type SpPortMessageRaw struct {
func (p *serport) reader() {

//var buf bytes.Buffer
ch := make([]byte, 1024)
timeCheckOpen := time.Now()
var buffered_ch bytes.Buffer

for {

ch := make([]byte, 1024)

n, err := p.portIo.Read(ch)

//if we detect that port is closing, break out o this for{} loop.
Expand All @@ -127,12 +128,9 @@ func (p *serport) reader() {
for i, w := 0, 0; i < n; i += w {
runeValue, width := utf8.DecodeRune(ch[i:n])
if runeValue == utf8.RuneError {
buffered_ch.Write(append(ch[i:n]))
buffered_ch.Write(ch[i:n])
break
}
if i == n {
buffered_ch.Reset()
}
data += string(runeValue)
w = width
}
Expand Down