简体   繁体   English

Web 串行 API “未捕获(承诺)DOMException:检测到缓冲区溢出。” 从串行设备读取串行数据时

[英]Web serial API "Uncaught (in promise) DOMException: A buffer overrun has been detected." when reading serial data from serial device

I am creating a website to send a command to a serial device (a microcontroller that I've programmed), and then read the response, which is also sent over serial.我正在创建一个网站来向串行设备(我编程的微控制器)发送命令,然后读取响应,该响应也是通过串行发送的。 I am using the web serial API to do this.我正在使用 web 序列号 API 来执行此操作。 When it goes to read the data sent over serial, it throws an error saying当它去读取通过串行发送的数据时,它会抛出一个错误说

"Uncaught (in promise) DOMException: A buffer overrun has been detected." “未捕获(承诺)DOMException:已检测到缓冲区溢出。”

What could be causing this?这可能是什么原因造成的?

    context = {}

    const connect = async () => {

         let tempPort = await navigator.serial.requestPort()
         await tempPort.open({baudRate: 115200})
         setPort(tempPort)
    
         context.textEncoder = new TextEncoderStream();
         context.writableStreamClosed = context.textEncoder.readable.pipeTo(tempPort.writable);
         context.writer = context.textEncoder.writable.getWriter();
         context.textDecoder = new TextDecoderStream()
         context.readableStreamClosed = tempPort.readable.pipeTo(context.textDecoder.writable)
         context.reader = context.textDecoder.readable.getReader()
    }

    const getFlightData = async () => {
         await context.writer.write("!flightData\r\n");
         var textStream, done
         while(!done) {
           let {value, done} = await context.reader.read()
           console.log(value)
           textStream += value
           if(value.includes("!end")) break
         }
         textStream = textStream.replace("!end", "")
         textStream = textStream.replace('\r', "")
         textStream = textStream.replace('\n', "")
         textStream = textStream.split("!flightData")[1] 

         console.log("Data:")

         textStream.split("\r\n").forEach(row => {
           console.log(row)
         })
       }

I found the problem.我发现了问题。 The default buffer size is only 256bit.默认缓冲区大小仅为 256 位。 I manually increased it by adding bufferSize to the config object when opening the port我通过在打开端口时将bufferSize添加到配置 object 来手动增加它

await tempPort.open({baudRate: 115200, bufferSize: 1000000})

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

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