Skip to content

Commit 30d6789

Browse files
committed
Improve web usb and web serial robustness.
1 parent 4cb4fb2 commit 30d6789

File tree

3 files changed

+194
-112
lines changed

3 files changed

+194
-112
lines changed

examples/device/webusb_serial/website/application.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,14 @@
383383
uiConnectSerialBtn.style.display = 'none';
384384
uiDisconnectBtn.style.display = 'block';
385385
uiCommandLineInput.disabled = false;
386-
uiCommandLineInput.focus();
386+
387+
if (this.currentPort instanceof SerialPort) {
388+
uiDisconnectBtn.textContent = 'Disconnect from WebSerial';
389+
} else if (this.currentPort instanceof WebUsbSerialPort) {
390+
uiDisconnectBtn.textContent = 'Disconnect from WebUSB';
391+
} else {
392+
uiDisconnectBtn.textContent = 'Disconnect';
393+
}
387394
} else {
388395
if (serial.isWebUsbSupported()) {
389396
uiConnectWebUsbSerialBtn.style.display = 'block';
@@ -454,6 +461,8 @@
454461
await this.currentPort.forgetDevice();
455462
this.currentPort = null;
456463
}
464+
} finally {
465+
this.updateUIConnectionState();
457466
}
458467
}
459468

@@ -474,7 +483,7 @@
474483
const savedPortInfo = JSON.parse(localStorage.getItem('webUSBSerialPort'));
475484
if (savedPortInfo) {
476485
for (const device of grantedDevices) {
477-
if (device.device.vendorId === savedPortInfo.vendorId && device.device.productId === savedPortInfo.productId) {
486+
if (device._device.vendorId === savedPortInfo.vendorId && device._device.productId === savedPortInfo.productId) {
478487
this.currentPort = device;
479488
break;
480489
}
@@ -501,19 +510,22 @@
501510

502511
// save the port to localStorage
503512
const portInfo = {
504-
vendorId: this.currentPort.device.vendorId,
505-
productId: this.currentPort.device.productId,
513+
vendorId: this.currentPort._device.vendorId,
514+
productId: this.currentPort._device.productId,
506515
}
507516
localStorage.setItem('webUSBSerialPort', JSON.stringify(portInfo));
508517

509518
this.setStatus('Connected', 'info');
519+
uiCommandLineInput.focus();
510520
} catch (error) {
511521
if (first_time_connection) {
512522
// Forget the device if a first time connection fails
513523
await this.currentPort.forgetDevice();
514524
this.currentPort = null;
515525
}
516526
throw error;
527+
} finally {
528+
this.updateUIConnectionState();
517529
}
518530

519531
this.updateUIConnectionState();
@@ -530,6 +542,8 @@
530542
this.setStatus('Reconnected', 'info');
531543
} catch (error) {
532544
this.setStatus(`Reconnect failed: ${error.message}`, 'error');
545+
} finally {
546+
this.updateUIConnectionState();
533547
}
534548
}
535549
this.updateUIConnectionState();

0 commit comments

Comments
 (0)