Skip to content

Commit 1bef477

Browse files
committed
allow reconnect timeout custom add configurable retries
1 parent 6f691d0 commit 1bef477

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
@@ -80,6 +80,10 @@ <h3>Console </h3>
8080
<option value="115200">115200</option>
8181
<option value="74880">74880</option>
8282
</select>
83+
<label for="reconnectDelay" id="lblReconnectDelay">Reconnect Delay (ms):</label>
84+
<input type="number" id="reconnectDelay" name="reconnectDelay" value="1000" min="100" max="10000" step="100">
85+
<label for="maxRetries" id="lblMaxRetries">Max Retries:</label>
86+
<input type="number" id="maxRetries" name="maxRetries" value="5" min="1" max="20" step="1">
8387

8488
<br><br>
8589

examples/typescript/src/index.ts

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const baudrates = document.getElementById("baudrates") as HTMLSelectElement;
22
const consoleBaudrates = document.getElementById("consoleBaudrates") as HTMLSelectElement;
3+
const reconnectDelay = document.getElementById("reconnectDelay") as HTMLInputElement;
4+
const maxRetriesInput = document.getElementById("maxRetries") as HTMLInputElement;
35
const connectButton = document.getElementById("connectButton") as HTMLButtonElement;
46
const traceButton = document.getElementById("copyTraceButton") as HTMLButtonElement;
57
const disconnectButton = document.getElementById("disconnectButton") as HTMLButtonElement;
@@ -250,31 +252,52 @@ consoleStartButton.onclick = async () => {
250252
// Set up device lost callback
251253
transport.setDeviceLostCallback(async () => {
252254
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));
255257
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));
276294
}
277295
}
296+
297+
if (retryCount >= maxRetries) {
298+
term.writeln("\n[RECONNECT] Failed to reconnect after 5 attempts. Please manually reconnect.");
299+
isReconnecting = false;
300+
}
278301
}
279302
});
280303
}

0 commit comments

Comments
 (0)