简体   繁体   中英

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. 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."

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. I manually increased it by adding bufferSize to the config object when opening the port

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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