0

在 web 串行 api 中,我可以在按钮单击时打开端口。连接端口后有什么方法可以打开端口吗?onconnect 事件未打开端口,要求用户手势...我需要自动打开以进行量产...

4

1 回答 1

0

https://web.dev/serial/#open-port所示,requestPort()需要用户手势,例如触摸或鼠标单击。但是,一旦有了端口,您就可以open()在后续页面加载时使用端口,而无需任何用户手势。

// 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 });
于 2021-12-02T08:06:53.650 回答