Skip to content

Commit 5bd8910

Browse files
committed
Synchronize multiple open commands coming together.
1 parent b717430 commit 5bd8910

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

serialport.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/base64"
2121
"io"
2222
"strconv"
23+
"sync"
2324
"sync/atomic"
2425
"time"
2526
"unicode/utf8"
@@ -273,7 +274,11 @@ func (p *serport) writerRaw() {
273274
h.broadcastSys <- []byte(msgstr)
274275
}
275276

277+
var spHandlerLock sync.Mutex
278+
276279
func spHandlerOpen(portname string, baud int, buftype string) {
280+
spHandlerLock.Lock()
281+
defer spHandlerLock.Unlock()
277282

278283
log.Print("Inside spHandler")
279284

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

333338
sh.Register(p)
334-
defer sh.Unregister(p)
335339

336340
serialPorts.MarkPortAsOpened(portname)
337341
serialPorts.List()
@@ -342,10 +346,12 @@ func spHandlerOpen(portname string, baud int, buftype string) {
342346
go p.writerNoBuf()
343347
// this is thread to send to serial port but with base64 decoding
344348
go p.writerRaw()
345-
346-
p.reader(buftype)
347-
348-
serialPorts.List()
349+
// this is the thread that reads from the serial port
350+
go func() {
351+
p.reader(buftype)
352+
serialPorts.List()
353+
sh.Unregister(p)
354+
}()
349355
}
350356

351357
func (p *serport) Close() {

0 commit comments

Comments
 (0)