Limited availability
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Note: This feature is available in Dedicated Web Workers.
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.connected
Read only Experimental
Returns a boolean value that indicates whether the port is logically connected to the device.
SerialPort.readable
Read only Experimental
Returns a ReadableStream
for receiving data from the device connected to the port.
SerialPort.writable
Read only Experimental
Returns a WritableStream
for sending data to the device connected to the port.
SerialPort.forget()
Experimental
Returns a Promise
that 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()
Experimental
Returns an object containing identifying information for the device available via the port.
SerialPort.open()
Experimental
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()
Experimental
Sets control signals on the port and returns a Promise
that resolves when they are set.
SerialPort.getSignals()
Experimental
Returns a Promise
that resolves with an object containing the current state of the port's control signals.
SerialPort.close()
Experimental
Returns a Promise
that resolves when the port closes.
connect
Experimental
Fired when the port connects to the device.
disconnect
Experimental
Fired when the port disconnects 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: 9600 /* 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();
}
}
Writing data to a port
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();
Specifications Browser compatibility
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4