|
1 | 1 | const baudrates = document.getElementById("baudrates") as HTMLSelectElement; |
2 | 2 | const consoleBaudrates = document.getElementById("consoleBaudrates") as HTMLSelectElement; |
| 3 | +const reconnectDelay = document.getElementById("reconnectDelay") as HTMLInputElement; |
| 4 | +const maxRetriesInput = document.getElementById("maxRetries") as HTMLInputElement; |
3 | 5 | const connectButton = document.getElementById("connectButton") as HTMLButtonElement; |
4 | 6 | const traceButton = document.getElementById("copyTraceButton") as HTMLButtonElement; |
5 | 7 | const disconnectButton = document.getElementById("disconnectButton") as HTMLButtonElement; |
@@ -250,31 +252,52 @@ consoleStartButton.onclick = async () => { |
250 | 252 | // Set up device lost callback |
251 | 253 | transport.setDeviceLostCallback(async () => { |
252 | 254 | if (!isConsoleClosed && !isReconnecting) { |
253 | | - term.writeln("\n[DEVICE LOST] Device disconnected. Click 'Reconnect' to restore connection..."); |
254 | | - await sleep(1000); |
| 255 | + term.writeln("\n[DEVICE LOST] Device disconnected. Trying to reconnect..."); |
| 256 | + await sleep(parseInt(reconnectDelay.value)); |
255 | 257 | isReconnecting = true; |
256 | | - term.writeln("\n[RECONNECT] Attempting to reconnect..."); |
257 | | - if (serialLib && serialLib.getPorts) { |
258 | | - const ports = await serialLib.getPorts(); |
259 | | - if (ports.length > 0) { |
260 | | - const newDevice = ports.find( |
261 | | - (port) => |
262 | | - port.getInfo().usbVendorId === deviceInfo.usbVendorId && |
263 | | - port.getInfo().usbProductId === deviceInfo.usbProductId, |
264 | | - ); |
265 | | - device = newDevice; |
266 | | - transport.updateDevice(device); |
267 | | - term.writeln("[RECONNECT] Found previously authorized device, connecting..."); |
268 | | - await transport.connect(parseInt(consoleBaudrates.value)); |
269 | | - term.writeln("[RECONNECT] Successfully reconnected!"); |
270 | | - consoleStopButton.style.display = "initial"; |
271 | | - resetButton.style.display = "initial"; |
272 | | - isReconnecting = false; |
273 | | - |
274 | | - startConsoleReading(); |
275 | | - return; |
| 258 | + |
| 259 | + const maxRetries = parseInt(maxRetriesInput.value); |
| 260 | + let retryCount = 0; |
| 261 | + |
| 262 | + while (retryCount < maxRetries && !isConsoleClosed) { |
| 263 | + retryCount++; |
| 264 | + term.writeln(`\n[RECONNECT] Attempt ${retryCount}/${maxRetries}...`); |
| 265 | + |
| 266 | + if (serialLib && serialLib.getPorts) { |
| 267 | + const ports = await serialLib.getPorts(); |
| 268 | + if (ports.length > 0) { |
| 269 | + const newDevice = ports.find( |
| 270 | + (port) => |
| 271 | + port.getInfo().usbVendorId === deviceInfo.usbVendorId && |
| 272 | + port.getInfo().usbProductId === deviceInfo.usbProductId, |
| 273 | + ); |
| 274 | + |
| 275 | + if (newDevice) { |
| 276 | + device = newDevice; |
| 277 | + transport.updateDevice(device); |
| 278 | + term.writeln("[RECONNECT] Found previously authorized device, connecting..."); |
| 279 | + await transport.connect(parseInt(consoleBaudrates.value)); |
| 280 | + term.writeln("[RECONNECT] Successfully reconnected!"); |
| 281 | + consoleStopButton.style.display = "initial"; |
| 282 | + resetButton.style.display = "initial"; |
| 283 | + isReconnecting = false; |
| 284 | + |
| 285 | + startConsoleReading(); |
| 286 | + return; |
| 287 | + } |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + if (retryCount < maxRetries) { |
| 292 | + term.writeln(`[RECONNECT] Device not found, retrying in ${parseInt(reconnectDelay.value)}ms...`); |
| 293 | + await sleep(parseInt(reconnectDelay.value)); |
276 | 294 | } |
277 | 295 | } |
| 296 | + |
| 297 | + if (retryCount >= maxRetries) { |
| 298 | + term.writeln("\n[RECONNECT] Failed to reconnect after 5 attempts. Please manually reconnect."); |
| 299 | + isReconnecting = false; |
| 300 | + } |
278 | 301 | } |
279 | 302 | }); |
280 | 303 | } |
|
0 commit comments