Skip to content

Commit fc30b2b

Browse files
committed
Made findPortByName a method of serialhub and moved near it
1 parent f762b44 commit fc30b2b

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

serial.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ func (sh *serialhub) run() {
106106
}
107107
}
108108

109+
func (sh *serialhub) FindPortByName(portname string) (*serport, bool) {
110+
sh.mu.Lock()
111+
defer sh.mu.Unlock()
112+
113+
for port := range sh.ports {
114+
if strings.EqualFold(port.portConf.Name, portname) {
115+
// we found our port
116+
//spHandlerClose(port)
117+
return port, true
118+
}
119+
}
120+
return nil, false
121+
}
122+
109123
func write(wr writeRequest) {
110124
switch wr.buffer {
111125
case "send":
@@ -167,7 +181,7 @@ func (sp *SerialPortList) Update() {
167181
}
168182

169183
// figure out if port is open
170-
if myport, isFound := findPortByName(item.Name); isFound {
184+
if myport, isFound := sh.FindPortByName(item.Name); isFound {
171185
// and update data with the open port parameters
172186
port.IsOpen = true
173187
port.Baud = myport.portConf.Baud
@@ -193,7 +207,7 @@ func spClose(portname string) {
193207
// that should cause an unregister channel call back
194208
// to myself
195209

196-
myport, isFound := findPortByName(portname)
210+
myport, isFound := sh.FindPortByName(portname)
197211

198212
if isFound {
199213
// we found our port
@@ -221,7 +235,7 @@ func spWrite(arg string) {
221235
//log.Println("The data is:" + args[2] + "---")
222236

223237
// see if we have this port open
224-
myport, isFound := findPortByName(portname)
238+
myport, isFound := sh.FindPortByName(portname)
225239

226240
if !isFound {
227241
// we couldn't find the port, so send err

seriallist.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package main
2020
import (
2121
"fmt"
2222
"slices"
23-
"strings"
2423

2524
log "github.com/sirupsen/logrus"
2625
"go.bug.st/serial/enumerator"
@@ -76,17 +75,3 @@ func enumerateSerialPorts() ([]*OsSerialPort, error) {
7675

7776
return arrPorts, err
7877
}
79-
80-
func findPortByName(portname string) (*serport, bool) {
81-
portnamel := strings.ToLower(portname)
82-
sh.mu.Lock()
83-
defer sh.mu.Unlock()
84-
for port := range sh.ports {
85-
if strings.ToLower(port.portConf.Name) == portnamel {
86-
// we found our port
87-
//spHandlerClose(port)
88-
return port, true
89-
}
90-
}
91-
return nil, false
92-
}

0 commit comments

Comments
 (0)