|
3 | 3 | // and ideally there would be an alternative format for short chip names, but including revision. |
4 | 4 | const baudrates = document.getElementById("baudrates") as HTMLSelectElement; |
5 | 5 | const consoleBaudrates = document.getElementById("consoleBaudrates") as HTMLSelectElement; |
| 6 | +const reconnectDelay = document.getElementById("reconnectDelay") as HTMLInputElement; |
| 7 | +const maxRetriesInput = document.getElementById("maxRetries") as HTMLInputElement; |
6 | 8 | const connectButton = document.getElementById("connectButton") as HTMLButtonElement; |
7 | 9 | const traceButton = document.getElementById("copyTraceButton") as HTMLButtonElement; |
8 | 10 | const disconnectButton = document.getElementById("disconnectButton") as HTMLButtonElement; |
@@ -479,31 +481,52 @@ consoleStartButton.onclick = async () => { |
479 | 481 | // Set up device lost callback |
480 | 482 | transport.setDeviceLostCallback(async () => { |
481 | 483 | if (!isConsoleClosed && !isReconnecting) { |
482 | | - term.writeln("\n[DEVICE LOST] Device disconnected. Click 'Reconnect' to restore connection..."); |
483 | | - await sleep(1000); |
| 484 | + term.writeln("\n[DEVICE LOST] Device disconnected. Trying to reconnect..."); |
| 485 | + await sleep(parseInt(reconnectDelay.value)); |
484 | 486 | isReconnecting = true; |
485 | | - term.writeln("\n[RECONNECT] Attempting to reconnect..."); |
486 | | - if (serialLib && serialLib.getPorts) { |
487 | | - const ports = await serialLib.getPorts(); |
488 | | - if (ports.length > 0) { |
489 | | - const newDevice = ports.find( |
490 | | - (port) => |
491 | | - port.getInfo().usbVendorId === deviceInfo.usbVendorId && |
492 | | - port.getInfo().usbProductId === deviceInfo.usbProductId, |
493 | | - ); |
494 | | - device = newDevice; |
495 | | - transport.updateDevice(device); |
496 | | - term.writeln("[RECONNECT] Found previously authorized device, connecting..."); |
497 | | - await transport.connect(parseInt(consoleBaudrates.value)); |
498 | | - term.writeln("[RECONNECT] Successfully reconnected!"); |
499 | | - consoleStopButton.style.display = "initial"; |
500 | | - resetButton.style.display = "initial"; |
501 | | - isReconnecting = false; |
502 | | - |
503 | | - startConsoleReading(); |
504 | | - return; |
| 487 | + |
| 488 | + const maxRetries = parseInt(maxRetriesInput.value); |
| 489 | + let retryCount = 0; |
| 490 | + |
| 491 | + while (retryCount < maxRetries && !isConsoleClosed) { |
| 492 | + retryCount++; |
| 493 | + term.writeln(`\n[RECONNECT] Attempt ${retryCount}/${maxRetries}...`); |
| 494 | + |
| 495 | + if (serialLib && serialLib.getPorts) { |
| 496 | + const ports = await serialLib.getPorts(); |
| 497 | + if (ports.length > 0) { |
| 498 | + const newDevice = ports.find( |
| 499 | + (port) => |
| 500 | + port.getInfo().usbVendorId === deviceInfo.usbVendorId && |
| 501 | + port.getInfo().usbProductId === deviceInfo.usbProductId, |
| 502 | + ); |
| 503 | + |
| 504 | + if (newDevice) { |
| 505 | + device = newDevice; |
| 506 | + transport.updateDevice(device); |
| 507 | + term.writeln("[RECONNECT] Found previously authorized device, connecting..."); |
| 508 | + await transport.connect(parseInt(consoleBaudrates.value)); |
| 509 | + term.writeln("[RECONNECT] Successfully reconnected!"); |
| 510 | + consoleStopButton.style.display = "initial"; |
| 511 | + resetButton.style.display = "initial"; |
| 512 | + isReconnecting = false; |
| 513 | + |
| 514 | + startConsoleReading(); |
| 515 | + return; |
| 516 | + } |
| 517 | + } |
| 518 | + } |
| 519 | + |
| 520 | + if (retryCount < maxRetries) { |
| 521 | + term.writeln(`[RECONNECT] Device not found, retrying in ${parseInt(reconnectDelay.value)}ms...`); |
| 522 | + await sleep(parseInt(reconnectDelay.value)); |
505 | 523 | } |
506 | 524 | } |
| 525 | + |
| 526 | + if (retryCount >= maxRetries) { |
| 527 | + term.writeln("\n[RECONNECT] Failed to reconnect after 5 attempts. Please manually reconnect."); |
| 528 | + isReconnecting = false; |
| 529 | + } |
507 | 530 | } |
508 | 531 | }); |
509 | 532 | } |
|
0 commit comments