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
Show file tree
Hide file tree
Changes from 2 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
19 changes: 6 additions & 13 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

type serialhub struct {
// Opened serial ports.
ports map[*serport]bool
ports map[string]*serport

mu sync.Mutex
}
Expand Down Expand Up @@ -60,15 +60,15 @@ type SpPortItem struct {
var serialPorts SerialPortList

var sh = serialhub{
ports: make(map[*serport]bool),
ports: make(map[string]*serport),
}

// Register serial ports from the connections.
func (sh *serialhub) Register(port *serport) {
sh.mu.Lock()
//log.Print("Registering a port: ", p.portConf.Name)
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + ",\"BufferType\":\"" + port.BufferType + "\"}")
sh.ports[port] = true
sh.ports[port.portName] = port
sh.mu.Unlock()
}

Expand All @@ -77,7 +77,7 @@ func (sh *serialhub) Unregister(port *serport) {
sh.mu.Lock()
//log.Print("Unregistering a port: ", p.portConf.Name)
h.broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "}")
delete(sh.ports, port)
delete(sh.ports, port.portName)
close(port.sendBuffered)
close(port.sendNoBuf)
sh.mu.Unlock()
Expand All @@ -86,15 +86,8 @@ func (sh *serialhub) Unregister(port *serport) {
func (sh *serialhub) FindPortByName(portname string) (*serport, bool) {
sh.mu.Lock()
defer sh.mu.Unlock()

for port := range sh.ports {
if strings.EqualFold(port.portConf.Name, portname) {
// we found our port
//spHandlerClose(port)
return port, true
}
}
return nil, false
port, ok := sh.ports[portname]
return port, ok
}

// List broadcasts a Json representation of the ports found
Expand Down
28 changes: 18 additions & 10 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 All @@ -295,11 +300,13 @@ func spHandlerOpen(portname string, baud int, buftype string) {
sp, err := serial.Open(portname, mode)
log.Print("Just tried to open port")
if err != nil {
//log.Fatal(err)
log.Print("Error opening port " + err.Error())
//h.broadcastSys <- []byte("Error opening port. " + err.Error())
h.broadcastSys <- []byte("{\"Cmd\":\"OpenFail\",\"Desc\":\"Error opening port. " + err.Error() + "\",\"Port\":\"" + conf.Name + "\",\"Baud\":" + strconv.Itoa(conf.Baud) + "}")

if existingPort, ok := sh.FindPortByName(portname); ok {
log.Print("Port already opened")
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Port already opened.\",\"Port\":\"" + existingPort.portConf.Name + "\",\"Baud\":" + strconv.Itoa(existingPort.portConf.Baud) + ",\"BufferType\":\"" + existingPort.BufferType + "\"}")
} else {
log.Print("Error opening port " + err.Error())
h.broadcastSys <- []byte("{\"Cmd\":\"OpenFail\",\"Desc\":\"Error opening port. " + err.Error() + "\",\"Port\":\"" + conf.Name + "\",\"Baud\":" + strconv.Itoa(conf.Baud) + "}")
}
return
}
log.Print("Opened port successfully")
Expand Down Expand Up @@ -331,7 +338,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 +348,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