如何从 Web 串行 api writer 读取字节响应

发布于 2025-01-18 09:24:05 字数 1174 浏览 5 评论 0原文

我正在尝试使用 Web Serial API。我想向设备写入字节并从中读取字节响应。

我可以将字节以及字符串写入设备 - 这里一切都按预期工作。 我可以从设备读取字节和字符串 - 这里我遇到问题。 示例表明您可以创建 while(true) 循环,并且流将返回 { value, done } 标志。但我没有找到 done 标志的实际含义。当我连接到设备并开始这个循环时,它永远不会结束。

    while (this.port.readable) {
      try {
        while (true) {
          const { value, done } = await this.reader.read();
          if (value) {
            getChunk(value);
          }
          if (done) {
            break;
          }
        }
      } catch (error) {
        console.error("Serial port reading error: " + error);
      } finally {
        this.reader.releaseLock();
      }
    }

我确实找到了一些有关 pipeTo 函数的建议,但它仅在字符串响应的示例中运行,此外它在我的情况下不起作用:

const textEncoder = new TextEncoderStream();
const writableStreamClosed = textEncoder.readable.pipeTo(this.port.writable);
const writer = textEncoder.writable.getWriter();

await writer.write("help");
Uncaught (in promise) TypeError: Failed to execute 'pipeTo' on 'ReadableStream': Cannot pipe to a locked stream

所以总结一下。如何通过串行 API 将字节写入设备并接收其返回的响应。

提前致谢。

I'm trying to work with Web Serial API. I want to write bytes to device and read bytes response from it.

I can write bytes as well as string to device - here everything working as expected.
I can read bytes and string from device - here I have problems.
Examples are showing that you can create a while(true) loop and the stream will return { value, done } flags. But I didn't find what actually done flag means. When I'm connecting to device and start this loop, it never ends.

    while (this.port.readable) {
      try {
        while (true) {
          const { value, done } = await this.reader.read();
          if (value) {
            getChunk(value);
          }
          if (done) {
            break;
          }
        }
      } catch (error) {
        console.error("Serial port reading error: " + error);
      } finally {
        this.reader.releaseLock();
      }
    }

I did find some suggestions with pipeTo function but it only operates in examples on string responses, in addition it don't work in my case:

const textEncoder = new TextEncoderStream();
const writableStreamClosed = textEncoder.readable.pipeTo(this.port.writable);
const writer = textEncoder.writable.getWriter();

await writer.write("help");
Uncaught (in promise) TypeError: Failed to execute 'pipeTo' on 'ReadableStream': Cannot pipe to a locked stream

So to sum up. How can I write bytes to device via serial API and receive back response from it.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沦落红尘 2025-01-25 09:24:05

好的,我确实编写了自己的函数来读取给定的字节数。也许这会对某人有所帮助。

public async readBytes(numberOfBytes: number): Promise<Uint8Array> {
    let bytes: Uint8Array = new Uint8Array(numberOfBytes);
    let readBytes: number = 0;
    let finished: boolean = false;

    try {
      while (true) {
        const { value, done } = await this.reader.read();
        if (value) {
          let chunk: Uint8Array = value;

          if (readBytes === 0 && chunk[0] === SerialService.FAIL) {
            return bytes;
          }

          for (let i = 0; i < chunk.length; i++) {
            bytes[i] = chunk[i];
            readBytes++;

            if (readBytes >= numberOfBytes) {
              finished = true;
            }
          }
        }
        if (done || finished) {
          break;
        }
      }
    } catch (error) {
      console.error("Serial port reading error: " + error);
    }

    return bytes;
  }

Ok, I did write my own function that reads given number of bytes. Maybe this will help someone.

public async readBytes(numberOfBytes: number): Promise<Uint8Array> {
    let bytes: Uint8Array = new Uint8Array(numberOfBytes);
    let readBytes: number = 0;
    let finished: boolean = false;

    try {
      while (true) {
        const { value, done } = await this.reader.read();
        if (value) {
          let chunk: Uint8Array = value;

          if (readBytes === 0 && chunk[0] === SerialService.FAIL) {
            return bytes;
          }

          for (let i = 0; i < chunk.length; i++) {
            bytes[i] = chunk[i];
            readBytes++;

            if (readBytes >= numberOfBytes) {
              finished = true;
            }
          }
        }
        if (done || finished) {
          break;
        }
      }
    } catch (error) {
      console.error("Serial port reading error: " + error);
    }

    return bytes;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文