The Web Serial API allows websites to communicate with serial devices.
What is the Web Serial API?
A serial port is a bidirectional communication interface that allows sending and receiving data byte by byte.
The Web Serial API provides a way for websites to read from and write to a serial device with JavaScript. Serial devices are connected either through a serial port on the user's system or through removable USB and Bluetooth devices that emulate a serial port.
In other words, the Web Serial API bridges the web and the physical world by allowing websites to communicate with serial devices, such as microcontrollers and 3D printers.
This API is also a great companion to WebUSB as operating systems require applications to communicate with some serial ports using their higher-level serial API rather than the low-level USB API.
Suggested use cases
In the educational, hobbyist, and industrial sectors, users connect peripheral devices to their computers. These devices are often controlled by microcontrollers via a serial connection used by custom software. Some custom software to control these devices is built with web technology:
In some cases, websites communicate with the device through an agent application that users installed manually. In others, the application is delivered in a packaged application through a framework such as Electron. And in others, the user is required to perform an additional step such as copying a compiled application to the device via a USB flash drive.
In all these cases, the user experience will be improved by providing direct communication between the website and the device that it is controlling.
Current status
| Step | Status |
|---|---|
| 1. Create explainer | Complete |
| 2. Create initial draft of specification | Complete |
| 3. Gather feedback & iterate on design | Complete |
| 4. Origin trial | Complete |
| 5. Launch | Complete |
Using the Web Serial API
Feature detection
To check if the Web Serial API is supported, use:
if ("serial" in navigator) {
// The Web Serial API is supported.
}
Open a serial port
The Web Serial API is asynchronous by design. This prevents the website UI from blocking when awaiting input, which is important because serial data can be received at any time, requiring a way to listen to it.
To open a serial port, first access a SerialPort object. For this, you can
either prompt the user to select a single serial port by calling
navigator.serial.requestPort() in response to a user gesture such as touch
or mouse click, or pick one from navigator.serial.getPorts() which returns
a list of serial ports the website has been granted access to.
document.querySelector('button').addEventListener('click', async () => {
// Prompt user to select any serial port.
const port = await navigator.serial.requestPort();
});
// Get all serial ports the user has previously granted the website access to.
const ports = await navigator.serial.getPorts();
The navigator.serial.requestPort() function takes an optional object literal
that defines filters. Those are used to match any serial device connected over
USB with a mandatory USB vendor (usbVendorId) and optional USB product
identifiers (usbProductId).
// Filter on devices with the Arduino Uno USB Vendor/Product IDs.
const filters = [
{ usbVendorId: 0x2341, usbProductId: 0x0043 },
{ usbVendorId: 0x2341, usbProductId: 0x0001 }
];
// Prompt user to select an Arduino Uno device.
const port = await navigator.serial.requestPort({ filters });
const { usbProductId, usbVendorId } = port.getInfo();