SerialPort: phương thức getInfo()
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.
Phương thức getInfo() của giao diện SerialPort trả về một đối tượng chứa thông tin định danh cho thiết bị có sẵn qua cổng.
Cú pháp
getInfo()
Tham số
Không có.
Giá trị trả về
Một đối tượng chứa các thuộc tính sau:
usbVendorId-
Nếu cổng là một phần của thiết bị USB, thuộc tính này là một số nguyên không dấu ngắn xác định nhà cung cấp thiết bị. Nếu không, nó là
undefined. usbProductId-
Nếu cổng là một phần của thiết bị USB, thuộc tính này là một số nguyên không dấu ngắn xác định thiết bị USB. Nếu không, nó là
undefined. bluetoothServiceClassIdExperimental-
Nếu cổng là một dịch vụ Bluetooth RFCOMM, thuộc tính này là một số nguyên dài không dấu hoặc chuỗi đại diện cho ID lớp dịch vụ Bluetooth của thiết bị. Nếu không, nó là
undefined.
Ví dụ
Đoạn mã này gọi phương thức Serial.requestPort() khi một <button> được nhấn. Chúng ta truyền một bộ lọc vào requestPort() để lọc cho các thiết bị Arduino Uno USB. Sau khi cổng được yêu cầu, chúng ta gọi getInfo() để trả về usbProductId và usbVendorId của thiết bị.
<button id="connect">Connect</button>
const connectBtn = document.getElementById("connect");
// Filter for devices with the Arduino Uno USB Vendor/Product IDs
const filters = [
{ usbVendorId: 0x2341, usbProductId: 0x0043 },
{ usbVendorId: 0x2341, usbProductId: 0x0001 }
];
connectBtn.addEventListener("click", () => {
try {
// Prompt the user to select an Arduino Uno device
const port = await navigator.serial.requestPort({ filters });
// Return the device's identifying info
const { usbProductId, usbVendorId } = port.getInfo();
} catch (e) {
// The user didn't select a device
}
});
Thông số kỹ thuật
| Specification |
|---|
| Web Serial API> # dom-serialport-getinfo> |