The Serial.requestPort()
method of the Serial
interface returns a Promise
that resolves with an instance of SerialPort
representing the device chosen by the user or rejects if no device was selected.
var promise = Serial.requestPort([options]);
A Promise
that resolves with an instance of SerialPort
.
The following example shows a filter being passed to requestPort()
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. If this filter was omitted the user would be able to select any available port.
button.addEventListener('click', () => {
const usbVendorId = ...;
navigator.serial.requestPort({ filters: [{ usbVendorId }]}).then((port) => {
}).catch((e) => {
});
});