SerialPort: thuộc tính readable

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

Thuộc tính chỉ đọc readable của giao diện SerialPort trả về một ReadableStream để nhận dữ liệu từ thiết bị kết nối với cổng. Các đoạn dữ liệu đọc từ luồng này là các thực thể của Uint8Array. Thuộc tính này khác null miễn là cổng đang mở và chưa gặp lỗi nghiêm trọng.

Giá trị

Một ReadableStream.

Ví dụ

Ví dụ sau đây cho thấy cách đọc dữ liệu từ một cổng. Vòng lặp ngoài xử lý các lỗi không nghiêm trọng, tạo một trình đọc mới cho đến khi gặp lỗi nghiêm trọng và readable trở thành null.

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();
  }
}

Thông số kỹ thuật

Specification
Web Serial API
# dom-serialport-readable

Tương thích trình duyệt