1

In web serial api, I am able to open ports on button click. Is there any way to open port once the port is connected ? onconnect event does not open port, asking for user gesture...I need automatic open for mass production...

Sibin
  • 11
  • 3

1 Answers1

1

As indicated in https://web.dev/serial/#open-port, requestPort() requires a user gesture such as touch or mouse click. However once you have a port, you can open() one on subsequent page load without any user gesture.

// Get all serial ports the user has previously granted the website access to.
const ports = await navigator.serial.getPorts();

// Assuming the first one is the one you want to open...
const port = ports[0];

// Open serial port without user gesture.
await port.open({ baudRate: 9600 });
François Beaufort
  • 4,843
  • 3
  • 29
  • 38