diff --git a/src/api/flash/AVR109Flasher.js b/src/api/flash/AVR109Flasher.js index b52fb4d2d..9049c02ae 100644 --- a/src/api/flash/AVR109Flasher.js +++ b/src/api/flash/AVR109Flasher.js @@ -47,11 +47,14 @@ const flash = async (port, filecontents) => { //open & close // Wait for the serial port to open. // Wait for the serial port to open. - if (port.readable && port.writable) { - await port.close(); - } - await port.open({ baudRate: 57600 }); + try { + if (!port.readable && !port.writable) { + await port.open({ baudRate: 57600 }); + } + } catch (e) { + console.error("Error opening port", { error: e }); + } //open writing facilities const writer = await port.writable.getWriter(); //open reading stream @@ -61,7 +64,11 @@ const flash = async (port, filecontents) => { resolve(); } catch (e) { console.error("Error during flash", { error: e }); - await port.close(); + try { + await port.close(); + } catch (err) { + console.error("Failed to close the port:", err); + } reject(e); } diff --git a/src/renderer/screens/FirmwareUpdate.js b/src/renderer/screens/FirmwareUpdate.js index c966dfac3..3ed206816 100644 --- a/src/renderer/screens/FirmwareUpdate.js +++ b/src/renderer/screens/FirmwareUpdate.js @@ -386,11 +386,13 @@ const FirmwareUpdate = (props) => { title={"You need to do something TKTKTK"} onConfirm={() => { connectToSerialport().then((focus) => { - console.debug("connected to serial port"); - setPromptForBootloaderConnection(false); - console.log("Runnign in onconfirm"); - console.log(afterBootloaderConnectCallback); - afterBootloaderConnectCallback(); + if (focus) { + setPromptForBootloaderConnection(false); + console.log(afterBootloaderConnectCallback); + afterBootloaderConnectCallback(); + } else { + console.log("We need to try that connect again"); + } }); }} /> @@ -399,11 +401,15 @@ const FirmwareUpdate = (props) => { title={"You need to do something TKTKTK"} onConfirm={() => { connectToSerialport().then((focus) => { - console.debug("connected to serial port"); - setPromptForFocusConnection(false); - console.log("Runnign in onconfirm"); - console.log(afterFocusConnectCallback); - afterFocusConnectCallback(); + if (focus) { + console.debug("connected to serial port"); + setPromptForFocusConnection(false); + console.log("Runnign in onconfirm"); + console.log(afterFocusConnectCallback); + afterFocusConnectCallback(); + } else { + console.log("We need to try that connect again"); + } }); }} /> diff --git a/src/renderer/utils/connectToSerialport.js b/src/renderer/utils/connectToSerialport.js index 9f1fcf0fc..aed2d6d14 100644 --- a/src/renderer/utils/connectToSerialport.js +++ b/src/renderer/utils/connectToSerialport.js @@ -23,13 +23,16 @@ export const connectToSerialport = async () => { let serialPort; const openPort = async () => { - try { - serialPort = await navigator.serial.requestPort({ - filters: supportedDeviceVIDPIDs(), - }); - } finally { - if (!serialPort) { - //return; + while (!serialPort) { + try { + serialPort = await navigator.serial.requestPort({ + filters: supportedDeviceVIDPIDs(), + }); + } catch (e) { + if (!serialPort) { + console.error("Failed to open serial port", e); + return; + } } }