Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. SerialPort

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.

EventTarget SerialPort

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 onlyExperimental

Returns a boolean value that indicates whether the port is logically connected to the device.

SerialPort.readableRead onlyExperimental

Returns aReadableStream for receiving data from the device connected to the port.

SerialPort.writableRead onlyExperimental

Returns aWritableStream for sending data to the device connected to the port.

Instance methods

SerialPort.forget()Experimental

Returns aPromise 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 aPromise 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 aPromise that resolves when they are set.

SerialPort.getSignals()Experimental

Returns aPromise that resolves with an object containing the current state of the port's control signals.

SerialPort.close()Experimental

Returns aPromise that resolves when the port closes.

Events

connectExperimental

Fired when the port connects to the device.

disconnectExperimental

Fired 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.

js
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.

js
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.

js
const encoder = new TextEncoder();const writer = port.writable.getWriter();await writer.write(encoder.encode("PING"));writer.releaseLock();

Specifications

Specification
Web Serial API
# dom-serialport

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp