Skip to content

fix: Serial Monitor corrupted output in some rare cases #1032

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 4 commits into from
Apr 24, 2025
Merged
Changes from 1 commit
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
16 changes: 11 additions & 5 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/base64"
"io"
"strconv"
"sync"
"sync/atomic"
"time"
"unicode/utf8"
Expand Down Expand Up @@ -273,7 +274,11 @@ func (p *serport) writerRaw() {
h.broadcastSys <- []byte(msgstr)
}

var spHandlerLock sync.Mutex

func spHandlerOpen(portname string, baud int, buftype string) {
spHandlerLock.Lock()
defer spHandlerLock.Unlock()

log.Print("Inside spHandler")

Expand Down Expand Up @@ -331,7 +336,6 @@ func spHandlerOpen(portname string, baud int, buftype string) {
p.bufferwatcher = bw

sh.Register(p)
defer sh.Unregister(p)

serialPorts.MarkPortAsOpened(portname)
serialPorts.List()
Expand All @@ -342,10 +346,12 @@ func spHandlerOpen(portname string, baud int, buftype string) {
go p.writerNoBuf()
// this is thread to send to serial port but with base64 decoding
go p.writerRaw()

p.reader(buftype)

serialPorts.List()
// this is the thread that reads from the serial port
go func() {
p.reader(buftype)
serialPorts.List()
sh.Unregister(p)
}()
}

func (p *serport) Close() {
Expand Down
Loading