Fix connection status UI not updating properly
- Ensure isConnected is always updated on main thread - Ensure lastError is always updated on main thread - Disconnect before reconnecting to avoid stale file descriptors - Start update timer on main thread
This commit is contained in:
@@ -680,18 +680,30 @@ class SerialManager: ObservableObject {
|
|||||||
/// Connect to selected serial port
|
/// Connect to selected serial port
|
||||||
func connect() {
|
func connect() {
|
||||||
guard !selectedPortPath.isEmpty else {
|
guard !selectedPortPath.isEmpty else {
|
||||||
lastError = "No port selected"
|
DispatchQueue.main.async {
|
||||||
|
self.lastError = "No port selected"
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure we disconnect first if already connected
|
||||||
|
if fileDescriptor != -1 {
|
||||||
|
disconnect()
|
||||||
|
}
|
||||||
|
|
||||||
// Open serial port
|
// Open serial port
|
||||||
fileDescriptor = open(selectedPortPath, O_RDWR | O_NOCTTY | O_NONBLOCK)
|
let fd = open(selectedPortPath, O_RDWR | O_NOCTTY | O_NONBLOCK)
|
||||||
|
|
||||||
guard fileDescriptor != -1 else {
|
guard fd != -1 else {
|
||||||
lastError = "Failed to open port: \(String(cString: strerror(errno)))"
|
DispatchQueue.main.async {
|
||||||
|
self.lastError = "Failed to open port: \(String(cString: strerror(errno)))"
|
||||||
|
self.isConnected = false
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fileDescriptor = fd
|
||||||
|
|
||||||
// Configure serial port
|
// Configure serial port
|
||||||
var options = termios()
|
var options = termios()
|
||||||
tcgetattr(fileDescriptor, &options)
|
tcgetattr(fileDescriptor, &options)
|
||||||
@@ -722,13 +734,18 @@ class SerialManager: ObservableObject {
|
|||||||
// Clear any pending data
|
// Clear any pending data
|
||||||
tcflush(fileDescriptor, TCIOFLUSH)
|
tcflush(fileDescriptor, TCIOFLUSH)
|
||||||
|
|
||||||
isConnected = true
|
// Update UI on main thread
|
||||||
lastError = nil
|
DispatchQueue.main.async {
|
||||||
|
self.isConnected = true
|
||||||
|
self.lastError = nil
|
||||||
|
}
|
||||||
|
|
||||||
print("Connected to \(selectedPortPath) at \(baudRate) baud")
|
print("Connected to \(selectedPortPath) at \(baudRate) baud")
|
||||||
|
|
||||||
// Start update timer
|
// Start update timer (must be on main thread)
|
||||||
startUpdateTimer()
|
DispatchQueue.main.async {
|
||||||
|
self.startUpdateTimer()
|
||||||
|
}
|
||||||
|
|
||||||
// For VU-Server: start response reader and query device info
|
// For VU-Server: start response reader and query device info
|
||||||
if selectedProtocol == .vuServer {
|
if selectedProtocol == .vuServer {
|
||||||
@@ -792,14 +809,20 @@ class SerialManager: ObservableObject {
|
|||||||
|
|
||||||
/// Disconnect from serial port
|
/// Disconnect from serial port
|
||||||
func disconnect() {
|
func disconnect() {
|
||||||
stopUpdateTimer()
|
// Stop timer on main thread
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.stopUpdateTimer()
|
||||||
|
}
|
||||||
|
|
||||||
if fileDescriptor != -1 {
|
if fileDescriptor != -1 {
|
||||||
close(fileDescriptor)
|
close(fileDescriptor)
|
||||||
fileDescriptor = -1
|
fileDescriptor = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
isConnected = false
|
// Update UI on main thread
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.isConnected = false
|
||||||
|
}
|
||||||
print("Disconnected from serial port")
|
print("Disconnected from serial port")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user