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

SerialPort: open() method

· 52CE 我爱测

The **`open()`** method of the SerialPort interface 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. The `baudRate` parameter is required.

Web Serial APIwindow_and_dedicated

The open() method of the SerialPort interface 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. The baudRate parameter is required.

Syntax

open(options)

Parameters

  • options
    • : An object with any of the following values:
      • baudRate
        • : A positive, non-zero value indicating the baud rate at which serial communication should be established.
      • bufferSize
        • : An unsigned long integer indicating the size of the read and write buffers that are to be established. If not passed, defaults to 255.
      • dataBits
        • : An integer value of 7 or 8 indicating the number of data bits per frame. If not passed, defaults to 8.
      • flowControl
        • : The flow control type, either "none" or "hardware". The default value is "none".
      • parity
        • : The parity mode, either "none", "even", or "odd". The default value is "none".
      • stopBits
        • : An integer value of 1 or 2 indicating the number of stop bits at the end of the frame. If not passed, defaults to 1.

Return value

A Promise.

Exceptions

The returned Promise rejects with one of the following exceptions:

  • InvalidStateError DOMException
    • : If open() is called when the port is already open.
  • NetworkError DOMException
    • : If the attempt to open the port failed.

Examples

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 */ });

Specifications

Browser compatibility

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

资料来源与适用范围

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

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

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