diff --git a/src/js/gui.js b/src/js/gui.js index e8f2105a50..a8897b9b5a 100644 --- a/src/js/gui.js +++ b/src/js/gui.js @@ -15,7 +15,6 @@ const GUI_MODES = { class GuiControl { constructor() { - this.auto_connect = false; this.connecting_to = false; this.connected_to = false; this.connect_lock = false; diff --git a/src/js/msp/MSPHelper.js b/src/js/msp/MSPHelper.js index dcdd4018d6..8e9a17a863 100644 --- a/src/js/msp/MSPHelper.js +++ b/src/js/msp/MSPHelper.js @@ -2801,7 +2801,7 @@ MspHelper.prototype.writeConfiguration = function(reboot, callback) { console.log('Configuration saved to EEPROM'); if (reboot) { GUI.tab_switch_cleanup(function() { - MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection); + return reinitializeConnection(callback); }); } if (callback) { diff --git a/src/js/port_handler.js b/src/js/port_handler.js index e50cdcd4a6..2a380e5205 100644 --- a/src/js/port_handler.js +++ b/src/js/port_handler.js @@ -56,11 +56,6 @@ PortHandler.setShowManualMode = function (showManualMode) { PortHandler.reinitialize = function () { this.initialPorts = false; - - if (this.usbCheckLoop) { - clearTimeout(this.usbCheckLoop); - } - this.showAllSerialDevices = getConfig('showAllSerialDevices').showAllSerialDevices; this.check(); // start listening, check after TIMEOUT_CHECK ms @@ -92,6 +87,7 @@ PortHandler.check_serial_devices = function () { self.selectActivePort(); self.initialPorts = {...self.currentPorts}; GUI.updateManualPortVisibility(); + self.detectPort(); } else { self.removePort(); self.detectPort(); diff --git a/src/js/serial_backend.js b/src/js/serial_backend.js index 613dca28a0..e436e20239 100644 --- a/src/js/serial_backend.js +++ b/src/js/serial_backend.js @@ -694,9 +694,11 @@ function onClosed(result) { MSP.clearListeners(); - serial.removeEventListener('receive', read_serial_adapter); - serial.removeEventListener('connect', connectHandler); - serial.removeEventListener('disconnect', disconnectHandler); + if (PortHandler.portPicker.selectedPort !== 'virtual') { + serial.removeEventListener('receive', read_serial_adapter); + serial.removeEventListener('connect', connectHandler); + serial.removeEventListener('disconnect', disconnectHandler); + } CONFIGURATOR.connectionValid = false; CONFIGURATOR.cliValid = false; @@ -782,53 +784,24 @@ function startLiveDataRefreshTimer() { } export function reinitializeConnection(callback) { - const isVirtual = CONFIGURATOR.virtualMode && GUI.connected_to == 'virtual' && CONFIGURATOR.connectionValid && serial.connectionId === 'virtual'; - - gui_log(i18n.getMessage('deviceRebooting')); - - // Close connection gracefully if it still exists. - const previousTimeStamp = connectionTimestamp; - - if (serial.connectionId) { - if (GUI.connected_to || GUI.connecting_to) { - $('a.connect').trigger('click'); - } else { - serial.disconnect(); - } - } // In virtual mode reconnect when autoconnect is enabled - if (isVirtual) { - return setTimeout(() => { - if (PortHandler.portPicker.autoConnect) { - $('a.connect').trigger('click'); - } - if (typeof callback === 'function') { - callback(); - } + if (PortHandler.portPicker.selectedPort === 'virtual' && PortHandler.portPicker.autoConnect) { + return setTimeout(function() { + $('a.connect').trigger('click'); }, 500); } - // Wait for serial or tcp connection to be available - let attempts = 0; - const reconnect = setInterval(waitforSerial, 100); + MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false); - function waitforSerial() { - if ((connectionTimestamp !== previousTimeStamp && CONFIGURATOR.connectionValid) || GUI.active_tab === 'firmware_flasher') { - console.log(`Serial connection available after ${attempts / 10} seconds`); - clearInterval(reconnect); - gui_log(i18n.getMessage('deviceReady')); + gui_log(i18n.getMessage('deviceRebooting')); - if (typeof callback === 'function') { - callback(); - } - } else { - attempts++; - if (attempts > 100) { - clearInterval(reconnect); - console.log(`failed to get serial connection, gave up after 10 seconds`); - gui_log(i18n.getMessage('serialPortOpenFail')); - } - } + // wait for the device to reboot + setTimeout(function() { + gui_log(i18n.getMessage('deviceReady')); + }, 2000); + + if (callback) { + callback(); } } diff --git a/src/js/webSerial.js b/src/js/webSerial.js index 080f836b7a..a3e90f903d 100644 --- a/src/js/webSerial.js +++ b/src/js/webSerial.js @@ -187,6 +187,7 @@ class WebSerial extends EventTarget { const doCleanup = async () => { if (this.reader) { + // this.reader.cancel(); this.reader.releaseLock(); this.reader = null; }