大网数据广告活动
Web 标准与前端开发

SerialPort: connect event

· 52CE 我爱测

The **`connect`** event of the SerialPort interface is fired when the port connects to the device.

Web Serial APIwindow_and_dedicated

The connect event of the SerialPort interface is fired when the port connects to the device.

Syntax

Use the event name in EventTarget/addEventListener:

addEventListener("connect", (event) => { })

Alternatively, assign to the event handler property of the interface:

onconnect = (event) => { }

Event type

A generic Event.

Description

More specifically, the connect event fires when the port becomes logically connected to the device. This happens after a user grants permission for a site to access the port that the device is attached to, following a Serial.requestPort() call:

  • In the case of a wired serial port, this occurs when the port is physically connected to the device, for example via USB.
  • In the case of a wireless serial port (for example, Bluetooth RFCOMM), this occurs when the port makes one or more active connections to the device (for example via Bluetooth L2CAP channels).

Bubbling

This event bubbles up to the Serial instance that returned this interface. The event.target property refers to the SerialPort object that bubbles up.

For more information, see Event bubbling.

Examples

Notify when a specific port connects

The Serial.requestPort() method returns a Promise that resolves with a SerialPort chosen by the user.

// Prompt user to choose a serial port
const port = await navigator.serial.requestPort();

port.addEventListener("connect", (event) => {
  // notify that the chosen port is connected
});

Listening for any newly-connected ports

The connect event bubbles up to the Serial object where you can listen for any newly-connected ports.

navigator.serial.addEventListener("connect", (event) => {
  // notify that a new port is available
  // use `event.target` to refer to the newly-added port
});

Specifications

Browser compatibility

See also

  • SerialPort.disconnect_event event

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。