Skip to content

Commit c2bee84

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

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

serialport.go

+8-4
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"
@@ -76,6 +77,7 @@ type SpPortMessageRaw struct {
7677
}
7778

7879
func (p *serport) reader(buftype string) {
80+
defer serialPorts.List()
7981

8082
timeCheckOpen := time.Now()
8183
var bufferedCh bytes.Buffer
@@ -273,7 +275,11 @@ func (p *serport) writerRaw() {
273275
h.broadcastSys <- []byte(msgstr)
274276
}
275277

278+
var spHandlerLock sync.Mutex
279+
276280
func spHandlerOpen(portname string, baud int, buftype string) {
281+
spHandlerLock.Lock()
282+
defer spHandlerLock.Unlock()
277283

278284
log.Print("Inside spHandler")
279285

@@ -342,10 +348,8 @@ func spHandlerOpen(portname string, baud int, buftype string) {
342348
go p.writerNoBuf()
343349
// this is thread to send to serial port but with base64 decoding
344350
go p.writerRaw()
345-
346-
p.reader(buftype)
347-
348-
serialPorts.List()
351+
// this is the thread that reads from the serial port
352+
go p.reader(buftype)
349353
}
350354

351355
func (p *serport) Close() {

0 commit comments

Comments
 (0)