Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Serial

Serial

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.

TheSerial interface of theWeb Serial API provides attributes and methods for finding and connecting to serial ports from a web page.

EventTarget Serial

Instance methods

Serial.requestPort()Experimental

Returns aPromise that resolves with an instance ofSerialPort representing the device chosen by the user. This method must be called viatransient activation.

Serial.getPorts()Experimental

Returns aPromise that resolves with an array ofSerialPort objects representing serial ports connected tothe host which the origin has permission to access.

Events

The following events are available toSerial via event bubbling fromSerialPort:

SerialPortconnect event

An event fired when a port has been connected to the device.

SerialPortdisconnect event

An event fired when a port has been disconnected from the device.

Examples

The following example shows how a site can check for available ports and allow the user to grant it permission to access additional ports.

On load event listeners are added for theconnect anddisconnect events so that the site can react when a device is connected or disconnected from the system. ThegetPorts() method is then called to see which ports are connected that the site already has access to.

If the site doesn't have access to any connected ports it has to wait until it has user activation to proceed. In this example we use aclick event handler on a button for this task. A filter is passed torequestPort() with a USB vendor ID in order to limit the set of devices shown to the user to only USB devices built by a particular manufacturer.

js
navigator.serial.addEventListener("connect", (e) => {  // Connect to `e.target` or add it to a list of available ports.});navigator.serial.addEventListener("disconnect", (e) => {  // Remove `e.target` from the list of available ports.});navigator.serial.getPorts().then((ports) => {  // Initialize the list of available ports with `ports` on page load.});button.addEventListener("click", () => {  const usbVendorId = 0xabcd;  navigator.serial    .requestPort({ filters: [{ usbVendorId }] })    .then((port) => {      // Connect to `port` or add it to the list of available ports.    })    .catch((e) => {      // The user didn't select a port.    });});

Specifications

Specification
Web Serial API
# serial-interface

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp