summaryrefslogtreecommitdiff
path: root/examples/device/webusb_serial/website/application.js
diff options
context:
space:
mode:
authorraldone01 <[email protected]>2025-07-24 23:58:54 +0200
committerraldone01 <[email protected]>2025-07-24 23:58:54 +0200
commit30d678970e4233ab41b40be8c8be31a0f02ccfb7 (patch)
treec6b5f09f7f3569b76a01daf9ab9bc54b9dfbf0c5 /examples/device/webusb_serial/website/application.js
parent4cb4fb2e28b0de994357453e5b0259c0a54bfdf4 (diff)
Improve web usb and web serial robustness.
Diffstat (limited to 'examples/device/webusb_serial/website/application.js')
-rw-r--r--examples/device/webusb_serial/website/application.js22
1 files changed, 18 insertions, 4 deletions
diff --git a/examples/device/webusb_serial/website/application.js b/examples/device/webusb_serial/website/application.js
index 5e9dbf471..090ab5748 100644
--- a/examples/device/webusb_serial/website/application.js
+++ b/examples/device/webusb_serial/website/application.js
@@ -383,7 +383,14 @@
uiConnectSerialBtn.style.display = 'none';
uiDisconnectBtn.style.display = 'block';
uiCommandLineInput.disabled = false;
- uiCommandLineInput.focus();
+
+ if (this.currentPort instanceof SerialPort) {
+ uiDisconnectBtn.textContent = 'Disconnect from WebSerial';
+ } else if (this.currentPort instanceof WebUsbSerialPort) {
+ uiDisconnectBtn.textContent = 'Disconnect from WebUSB';
+ } else {
+ uiDisconnectBtn.textContent = 'Disconnect';
+ }
} else {
if (serial.isWebUsbSupported()) {
uiConnectWebUsbSerialBtn.style.display = 'block';
@@ -454,6 +461,8 @@
await this.currentPort.forgetDevice();
this.currentPort = null;
}
+ } finally {
+ this.updateUIConnectionState();
}
}
@@ -474,7 +483,7 @@
const savedPortInfo = JSON.parse(localStorage.getItem('webUSBSerialPort'));
if (savedPortInfo) {
for (const device of grantedDevices) {
- if (device.device.vendorId === savedPortInfo.vendorId && device.device.productId === savedPortInfo.productId) {
+ if (device._device.vendorId === savedPortInfo.vendorId && device._device.productId === savedPortInfo.productId) {
this.currentPort = device;
break;
}
@@ -501,12 +510,13 @@
// save the port to localStorage
const portInfo = {
- vendorId: this.currentPort.device.vendorId,
- productId: this.currentPort.device.productId,
+ vendorId: this.currentPort._device.vendorId,
+ productId: this.currentPort._device.productId,
}
localStorage.setItem('webUSBSerialPort', JSON.stringify(portInfo));
this.setStatus('Connected', 'info');
+ uiCommandLineInput.focus();
} catch (error) {
if (first_time_connection) {
// Forget the device if a first time connection fails
@@ -514,6 +524,8 @@
this.currentPort = null;
}
throw error;
+ } finally {
+ this.updateUIConnectionState();
}
this.updateUIConnectionState();
@@ -530,6 +542,8 @@
this.setStatus('Reconnected', 'info');
} catch (error) {
this.setStatus(`Reconnect failed: ${error.message}`, 'error');
+ } finally {
+ this.updateUIConnectionState();
}
}
this.updateUIConnectionState();