Skip to content

Restore functionality of 'is_open' field in portlist #939

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 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,35 @@ func (sp *SerialPortList) remove(removedPort *discovery.Port) {
})
}

// MarkPortAsOpened marks a port as opened by the user
func (sp *SerialPortList) MarkPortAsOpened(portname string) {
sp.portsLock.Lock()
defer sp.portsLock.Unlock()
port := sp.getPortByName(portname)
if port != nil {
port.IsOpen = true
}
}

// MarkPortAsClosed marks a port as no more opened by the user
func (sp *SerialPortList) MarkPortAsClosed(portname string) {
sp.portsLock.Lock()
defer sp.portsLock.Unlock()
port := sp.getPortByName(portname)
if port != nil {
port.IsOpen = false
}
}

func (sp *SerialPortList) getPortByName(portname string) *SpPortItem {
for _, port := range sp.Ports {
if port.Name == portname {
return port
}
}
return nil
}

func spErr(err string) {
//log.Println("Sending err back: ", err)
//h.broadcastSys <- []byte(err)
Expand Down
4 changes: 4 additions & 0 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type serport struct {
// The serial port connection.
portConf *SerialConfig
portIo io.ReadWriteCloser
portName string

// Keep track of whether we're being actively closed
// just so we don't show scary error messages
Expand Down Expand Up @@ -305,6 +306,7 @@ func spHandlerOpen(portname string, baud int, buftype string) {
sendRaw: make(chan string),
portConf: conf,
portIo: sp,
portName: portname,
BufferType: buftype}

var bw Bufferflow
Expand All @@ -326,6 +328,7 @@ func spHandlerOpen(portname string, baud int, buftype string) {
sh.Register(p)
defer sh.Unregister(p)

serialPorts.MarkPortAsOpened(portname)
serialPorts.List()

// this is internally buffered thread to not send to serial port if blocked
Expand All @@ -349,5 +352,6 @@ func spHandlerClose(p *serport) {
func spCloseReal(p *serport) {
p.bufferwatcher.Close()
p.portIo.Close()
serialPorts.MarkPortAsClosed(p.portName)
serialPorts.List()
}
Loading