Skip to content

Commit

Permalink
Experimental WebSerial File
Browse files Browse the repository at this point in the history
  • Loading branch information
whowechina committed Apr 5, 2024
1 parent 786699e commit ebf7304
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions doc/web_serial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<title>Serial Ports</title>
</head>
<body>
<h1>Available Serial Ports:</h1>
<button id="listPorts">List Ports</button>
<ul id="ports"></ul>

<script>
document.getElementById('listPorts').addEventListener('click', async () => {
if ('serial' in navigator) {
try {
const ports = await navigator.serial.getPorts();
const ul = document.getElementById('ports');
ul.innerHTML = ''; // Clear the list before adding new items
ports.forEach(port => {
const li = document.createElement('li');
li.textContent = port.getInfo().usbProductId || 'Unknown';
ul.appendChild(li);
});
} catch (err) {
console.error('There was an error opening the serial port:', err);
}
} else {
console.error('Web serial doesn\'t seem to be enabled in your browser. Try enabling it by visiting:');
console.error('chrome://flags/#enable-experimental-web-platform-features');
console.error('opera://flags/#enable-experimental-web-platform-features');
console.error('edge://flags/#enable-experimental-web-platform-features');
}
});
</script>
</body>
</html>

0 comments on commit ebf7304

Please sign in to comment.