Skip to content

Commit 1b8a5cd

Browse files
committed
Merge remote-tracking branch 'upstream/bugfix/console-reconnect' into bugfix-console-reconnect
2 parents 1ec6aa9 + 1bef477 commit 1b8a5cd

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

examples/typescript/src/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ <h3>Console </h3>
114114
<option value="115200">115200</option>
115115
<option value="74880">74880</option>
116116
</select>
117+
<label for="reconnectDelay" id="lblReconnectDelay">Reconnect Delay (ms):</label>
118+
<input type="number" id="reconnectDelay" name="reconnectDelay" value="1000" min="100" max="10000" step="100">
119+
<label for="maxRetries" id="lblMaxRetries">Max Retries:</label>
120+
<input type="number" id="maxRetries" name="maxRetries" value="5" min="1" max="20" step="1">
117121

118122
<br><br>
119123

examples/typescript/src/index.ts

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// and ideally there would be an alternative format for short chip names, but including revision.
44
const baudrates = document.getElementById("baudrates") as HTMLSelectElement;
55
const consoleBaudrates = document.getElementById("consoleBaudrates") as HTMLSelectElement;
6+
const reconnectDelay = document.getElementById("reconnectDelay") as HTMLInputElement;
7+
const maxRetriesInput = document.getElementById("maxRetries") as HTMLInputElement;
68
const connectButton = document.getElementById("connectButton") as HTMLButtonElement;
79
const traceButton = document.getElementById("copyTraceButton") as HTMLButtonElement;
810
const disconnectButton = document.getElementById("disconnectButton") as HTMLButtonElement;
@@ -479,31 +481,52 @@ consoleStartButton.onclick = async () => {
479481
// Set up device lost callback
480482
transport.setDeviceLostCallback(async () => {
481483
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));
484486
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));
505523
}
506524
}
525+
526+
if (retryCount >= maxRetries) {
527+
term.writeln("\n[RECONNECT] Failed to reconnect after 5 attempts. Please manually reconnect.");
528+
isReconnecting = false;
529+
}
507530
}
508531
});
509532
}

0 commit comments

Comments
 (0)