Skip to content

Commit d9dfa10

Browse files
committed
Forcefully close the serial port if close is issued
1 parent c8ad4a7 commit d9dfa10

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

serialport.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type serport struct {
3939
// just so we don't show scary error messages
4040
isClosing bool
4141

42+
isClosingDueToError bool
43+
4244
// counter incremented on queue, decremented on write
4345
itemsInBuffer int
4446

@@ -170,6 +172,7 @@ func (p *serport) reader() {
170172
h.broadcastSys <- []byte("Error reading on " + p.portConf.Name + " " +
171173
err.Error() + " Closing port.")
172174
h.broadcastSys <- []byte("{\"Cmd\":\"OpenFail\",\"Desc\":\"Got error reading on port. " + err.Error() + "\",\"Port\":\"" + p.portConf.Name + "\",\"Baud\":" + strconv.Itoa(p.portConf.Baud) + "}")
175+
p.isClosingDueToError = true
173176
break
174177
}
175178

@@ -179,13 +182,16 @@ func (p *serport) reader() {
179182
if err == nil {
180183
diff := time.Since(timeCheckOpen)
181184
if diff.Nanoseconds() < 1000000 {
182-
p.isClosing = true
185+
p.isClosingDueToError = true
186+
break
183187
}
184188
timeCheckOpen = time.Now()
185189
}
186190
}
187191
}
188-
spCloseReal(p)
192+
if p.isClosingDueToError {
193+
spCloseReal(p)
194+
}
189195
}
190196

191197
// this method runs as its own thread because it's instantiated
@@ -323,6 +329,7 @@ func spHandlerOpen(portname string, baud int, buftype string) {
323329
func spHandlerClose(p *serport) {
324330
p.isClosing = true
325331
h.broadcastSys <- []byte("Closing serial port " + p.portConf.Name)
332+
spCloseReal(p)
326333
}
327334

328335
func spCloseReal(p *serport) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy