SerialPort
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.
Note: This feature is available inDedicated Web Workers.
TheSerialPort interface of theWeb Serial API provides access to a serial port on the host device.
In this article
Constructor
Instances of this interface may be obtained by calling methods of theSerial interface, therefore it has no constructor of its own.
Instance properties
SerialPort.connectedRead onlyExperimentalReturns a boolean value that indicates whether the port is logically connected to the device.
SerialPort.readableRead onlyExperimentalReturns a
ReadableStreamfor receiving data from the device connected to the port.SerialPort.writableRead onlyExperimentalReturns a
WritableStreamfor sending data to the device connected to the port.
Instance methods
SerialPort.forget()ExperimentalReturns a
Promisethat resolves when access to the serial port is revoked. Calling this "forgets" the device, resetting any previously-set permissions so the calling site can no longer communicate with the port.SerialPort.getInfo()ExperimentalReturns an object containing identifying information for the device available via the port.
SerialPort.open()ExperimentalReturns a
Promisethat resolves when the port is opened. By default the port is opened with 8 data bits, 1 stop bit and no parity checking.SerialPort.setSignals()ExperimentalSets control signals on the port and returns a
Promisethat resolves when they are set.SerialPort.getSignals()ExperimentalReturns a
Promisethat resolves with an object containing the current state of the port's control signals.SerialPort.close()ExperimentalReturns a
Promisethat resolves when the port closes.
Events
connectExperimentalFired when the port connects to the device.
disconnectExperimentalFired when the port disconnects from the device.
Examples
>Opening a port
Before communicating on a serial port it must be opened. Opening the port allows the site to specify the necessary parameters that control how data is transmitted and received. Developers should check the documentation for the device they are connecting to for the appropriate parameters.
await port.open({ baudRate: 9600 /* pick your baud rate */ });Once thePromise returned byopen() resolves thereadable andwritable attributes can be accessed to get theReadableStream andWritableStream instances for receiving data from and sending data to the connected device.
Reading data from a port
The following example shows how to read data from a port. The outer loop handles non-fatal errors, creating a new reader until a fatal error is encountered andreadable becomesnull.
while (port.readable) { const reader = port.readable.getReader(); try { while (true) { const { value, done } = await reader.read(); if (done) { // |reader| has been canceled. break; } // Do something with |value|… } } catch (error) { // Handle |error|… } finally { reader.releaseLock(); }}Writing data to a port
The following example shows how to write a string to a port. ATextEncoder converts the string to aUint8Array before transmission.
const encoder = new TextEncoder();const writer = port.writable.getWriter();await writer.write(encoder.encode("PING"));writer.releaseLock();Specifications
| Specification |
|---|
| Web Serial API> # dom-serialport> |