Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The SerialPort
interface of the Web Serial API
provides access to a serial port on the host device.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The SerialPort
interface of the Web Serial API
provides access to a serial port on the host device.
Instances of this interface may be obtained by calling methods of the Serial
interface, therefore it has no constructor of its own.
SerialPort.readable
Read only
Returns a ReadableStream
for receiving data from the device connected to the port.
SerialPort.writable
Read only
Returns a WritableStream
for sending data to the device connected to the port.
SerialPort.getInfo()
Returns a Promise
that resolves with an object containing properties of the port.
SerialPort.open()
Returns a Promise
that 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()
Sets control signals on the port and returns a Promise
that resolves when they are set.
SerialPort.getSignals()
Returns a Promise
that resolves with an object containing the current state of the port's control signals.
SerialPort.close()
Returns a Promise
that resolves when the port closes.
connect
An event fired when the port has connected to the device.
disconnect
An event fired when the port has disconnected from the device.
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: /* pick your baud rate */ });
Once the Promise
returned by open()
resolves the readable
and writable
attributes can be accessed to get the ReadableStream
and WritableStream
instances for receiving data from and sending data to the connected device.
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 and readable
becomes null
.
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(); } }
The following example shows how to write a string to a port. A TextEncoder
converts the string to a Uint8Array
before transmission.
const encoder = new TextEncoder(); const writer = port.writable.getWriter(); await writer.write(encoder.encode("PING")); writer.releaseLock();
Specification |
---|
Web Serial API # dom-serialport |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
SerialPort |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
close |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
connect_event |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
disconnect_event |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
getInfo |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
getSignals |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
open |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
readable |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
setSignals |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
writable |
89 |
89 |
No |
No |
75 |
No |
No |
No |
No |
No |
No |
No |
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/SerialPort