简体   繁体   English

使用 navigator.serial 进行串行通信时的读取性能延迟

[英]Delayed read performance when using navigator.serial for serial communication

I've been trying out the web serial API in chrome ( https://web.dev/serial/ ) to do some basic communication with an Arduino board.我一直在尝试使用 chrome ( https://web.dev/serial/ ) 中的网络串行 API 来与 Arduino 板进行一些基本的通信。 I've noticed quite a substantial delay when reading data from the serial port however.但是,从串行端口读取数据时,我注意到相当大的延迟。 This same issue is present in some demos, but not all.在某些演示中存在相同的问题,但不是全部。

For instance, using the WebSerial demo linked towards the bottom has a near instantaneous read:例如,使用链接到底部的WebSerial演示几乎可以立即读取:

在此处输入图像描述

While using the Serial Terminal example results in a read delay.使用串行终端示例会导致读取延迟。 (note the write is triggered at the moment of a character being entered on the keyboard): (注意写入是在键盘上输入字符时触发的):

在此处输入图像描述

WebSerial being open source allows for me to check for differences between my own implementation, however I am seeing performance much like the second example. WebSerial 是开源的,允许我检查我自己的实现之间的差异,但是我看到的性能很像第二个例子。

As for the relevant code:至于相关代码:

this.port = await navigator.serial.requestPort({ filters });

await this.port.open({ baudRate: 115200, bufferSize: 255, dataBits: 8, flowControl: 'none', parity: 'none', stopBits: 1 });
this.open = true;
this.monitor();

private monitor = async () => {
    const dataEndFlag = new Uint8Array([4, 3]);
    while (this.open && this.port?.readable) {
        this.open = true;
        const reader = this.port.readable.getReader();
        try {
            let data: Uint8Array = new Uint8Array([]);
            while (this.open) {
                const { value, done } = await reader.read();
                if (done) {
                    this.open = false;
                    break;
                }
                if (value) {
                    data = Uint8Array.of(...data, ...value);
                }
                if (data.slice(-2).every((val, idx) => val === dataEndFlag[idx])) {
                    const decoded = this.decoder.decode(data);
                    this.messages.push(decoded);
                    data = new Uint8Array([]);
                }
            }
        } catch {
        }
    }
}

public write = async (data: string) => {
    if (this.port?.writable) {
        const writer = this.port.writable.getWriter();
        await writer.write(this.encoder.encode(data));
        writer.releaseLock();
    }
}

The equivalent WebSerial code can be found here , this is pretty much an exact replica.可以在此处找到等效的 WebSerial 代码,这几乎是一个精确的副本。 From what I can observe, it seems to hang at await reader.read();据我观察,它似乎挂在await reader.read(); for a brief period of time.在短时间内。

This is occurring both on a Windows 10 device and a macOS Monterey device.这在 Windows 10 设备和 macOS Monterey 设备上都会发生。 The specific hardware device is an Arduino Pro Micro connected to a USB port.特定的硬件设备是连接到 USB 端口的 Arduino Pro Micro。

Has anyone experienced this same scenario?有没有人经历过同样的场景?

Update: I did some additional testing with more verbose logging.更新:我用更详细的日志记录做了一些额外的测试。 It seems that the time between the write and read is exactly 1 second every time .似乎每次写入和读取之间的时间恰好是1秒。

the delay may result from SerialEvent() in your arduino script: set Serial.setTimeout(1);延迟可能是由您的 arduino 脚本中的 SerialEvent() 引起的:set Serial.setTimeout(1); This means 1 millisecond instead of default 1000 milliseconds.这意味着 1 毫秒而不是默认的 1000 毫秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM