I need your help.
Is there a way to take control of a serial port, previously authorized, without using the popup window? (without port = await navigator.serial.requestPort();)
Thank you and sorry for my english, italian speaking here.
I need your help.
Is there a way to take control of a serial port, previously authorized, without using the popup window? (without port = await navigator.serial.requestPort();)
Thank you and sorry for my english, italian speaking here.
I believe JS code below should work fine for you:
// Get all serial ports the user has previously granted the website access to.
const ports = await navigator.serial.getPorts();
Run this code on first mouse or keyboard event, it will pop port permission on first run and should fetch port automatically on next runs.
// specify device details
let usbVendorId = 0x0403, // Arduino
usbProductId = 0x6001; // Nano
// get serial ports
navigator.serial
.getPorts()
.then(
ports => {
// no ports available, prompt user permission
if(!ports.length)
return navigator.serial
.requestPort({
filters: [{usbVendorId, usbProductId}]
});
// port is available
else
return Promise
.resolve(ports[0]);
}
)
.then(
port => {
port
.open({
baudRate: 9600
})
.then(
() => {
const reader = port.readable.getReader();
// ...
}
);
}
);