Skip to content

Commit 9c1115e

Browse files
committed
Add reconnect m
1 parent f36f97c commit 9c1115e

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

examples/device/webusb_serial/website/application.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@
369369
}
370370

371371
setStatus(msg, level = 'info') {
372-
console.log(msg);
372+
console.error(msg);
373373
uiStatusSpan.textContent = msg;
374374
uiStatusSpan.className = 'status status-' + level;
375375
}
@@ -596,24 +596,33 @@
596596
}
597597
}
598598

599-
tryAutoReconnect() {
600-
this.updateUIConnectionState();
601-
if (!uiAutoReconnectCheckbox.checked) return;
602-
if (this.reconnectTimeoutId !== null) return; // already trying
603-
this.setStatus('Attempting to auto-reconnect...', 'info');
604-
this.reconnectTimeoutId = setTimeout(async () => {
599+
async autoReconnectTimeout() {
605600
this.reconnectTimeoutId = null;
606601
if (!uiAutoReconnectCheckbox.checked) {
607602
this.setStatus('Auto-reconnect stopped.', 'info');
608603
return;
609604
}
610-
if (this.currentPort) {
605+
if (this.currentPort && !this.currentPort.isConnected) {
611606
try {
612607
await this.currentPort.connect();
608+
this.setStatus('Reconnected successfully', 'info');
609+
} catch (error) {
610+
this.setStatus(`Reconnect failed: ${error.message}`, 'error');
611+
// Try again after a delay
612+
this.tryAutoReconnect();
613613
} finally {
614614
this.updateUIConnectionState();
615615
}
616616
}
617+
}
618+
619+
tryAutoReconnect() {
620+
this.updateUIConnectionState();
621+
if (!uiAutoReconnectCheckbox.checked) return;
622+
if (this.reconnectTimeoutId !== null) return; // already trying
623+
this.setStatus('Attempting to auto-reconnect...', 'info');
624+
this.reconnectTimeoutId = setTimeout(async () => {
625+
await this.autoReconnectTimeout();
617626
}, 1000);
618627
}
619628

@@ -762,14 +771,15 @@
762771
// save <iso_date_time>,<received_line>
763772
let csvContent = 'data:text/csv;charset=utf-8,';
764773
for (const entry of this.receivedData) {
765-
let line = new Date(entry.time).toISOString() + ',"' + entry.text.replace(/[\r\n]+$/, '') + '"';
774+
let sanitizedText = entry.text.replace(/"/g, '""').replace(/[\r\n]+$/, '');
775+
let line = new Date(entry.time).toISOString() + ',"' + sanitizedText + '"';
766776
csvContent += line + '\n';
767777
}
768778

769779
const encodedUri = encodeURI(csvContent);
770780
const link = document.createElement('a');
771781
link.setAttribute('href', encodedUri);
772-
const filename = new Date().toISOString() + '_tinyusb_received_serial_data.csv';
782+
const filename = new Date().toISOString().replace(/:/g, '-') + '_tinyusb_received_serial_data.csv';
773783
link.setAttribute('download', filename);
774784
document.body.appendChild(link);
775785
link.click();

examples/device/webusb_serial/website/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ <h1 class="app-title">TinyUSB - WebUSB Serial</h1>
3535
</label>
3636
<label for="auto_reconnect_checkbox" class="controls">
3737
<input type="checkbox" id="auto_reconnect_checkbox" />
38-
Auto Reconnect
38+
Auto Reconnect WebUSB
3939
</label>
4040
<button id="forget_device_btn" class="controls btn danger">Forget Device</button>
4141
<button id="forget_all_devices_btn" class="controls btn danger">Forget All Devices</button>

examples/device/webusb_serial/website/serial.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class SerialPort {
116116
if (!this._port.writable) {
117117
throw new Error('Port is not writable');
118118
}
119-
this._writer = port.writeable.getWriter();
119+
this._writer = this._port.writable.getWriter();
120120
if (!this._writer) {
121121
throw new Error('Failed to get writer from port');
122122
}
@@ -141,6 +141,13 @@ class WebUsbSerialPort {
141141
this._readLoopPromise = null;
142142
this._initialized = false;
143143
this._keepReading = true;
144+
145+
this._vendorId = device.vendorId;
146+
this._productId = device.productId;
147+
}
148+
149+
_isSameWebUsbSerialPort(webUsbSerialPort) {
150+
return this._vendorId === webUsbSerialPort._vendorId && this._productId === webUsbSerialPort._productId;
144151
}
145152

146153
/// Connect and start reading loop
@@ -152,14 +159,9 @@ class WebUsbSerialPort {
152159
console.error('Error disconnecting previous device:', error);
153160
}
154161

155-
if (this._readLoopPromise) {
156-
try {
157-
await this._readLoopPromise;
158-
} catch (error) {
159-
console.error('Error in read loop:', error);
160-
}
161-
}
162-
this._readLoopPromise = null;
162+
const webUsbSerialPorts = await serial.getWebUsbSerialPorts();
163+
const webUsbSerialPort = webUsbSerialPorts.find(serialPort => this._isSameWebUsbSerialPort(serialPort));
164+
this._device = webUsbSerialPort ? webUsbSerialPort._device : this._device;
163165
}
164166
this._initialized = true;
165167

examples/device/webusb_serial/website/style.css

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ body.dark-mode {
221221
color: #d4d4d4;
222222
}
223223

224+
body.dark-mode input[type="checkbox"] {
225+
border-color: #888;
226+
accent-color: #2e2e2e;
227+
opacity: 0.8;
228+
}
229+
224230
body.dark-mode .btn-theme {
225231
background-color: #b0b0b0;
226232
color: #000;
@@ -251,6 +257,7 @@ body.dark-mode .input:focus {
251257

252258
body.dark-mode .scrollbox {
253259
background-color: #252526;
260+
scrollbar-color: #555 #2e2e2e;
254261
border: 1px solid #444;
255262
}
256263

@@ -287,7 +294,3 @@ body.dark-mode option {
287294
background-color: #3c3c3c;
288295
color: #f0f0f0;
289296
}
290-
291-
body.dark-mode .scrollbox {
292-
scrollbar-color: #555 #2e2e2e;
293-
}

0 commit comments

Comments
 (0)