Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Write to serial port when it is being read

I can read and write to a serial port seperately with two seperate buttons (onclick) .Now I try to write to the serial port , when a value ('Please send the file') is read from same port on a single click button .Below is my code which shows errors

  1. TypeError: Failed to execute 'write' on 'UnderlyingSinkBase': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'.

  2. Uncaught (in promise) TypeError: Failed to execute 'pipeTo' on 'ReadableStream': Cannot pipe a locked stream.

navigator.serial.requestPort().then(async (port) => {

  await port.open({   baudRate: 115200  });


// Reading port //

  while (port.readable) {
    const textDecoder = new TextDecoderStream();
    const readableStreamClosed = port.readable.pipeTo(textDecoder.writable);
    const reader = textDecoder.readable.getReader();

    try {
      while (true) {
        const { value, done } = await reader.read();

        if (done) {
         
          break;
        }
        if (value) {

          document.getElementById('log').innerHTML += value + "<br/>";
          
          if(value.includes('Please send the file')){
     

            var arr=[];

            // Getting file to be sent //

var oReq = new XMLHttpRequest();
oReq.open("GET", "uart_rx_test2.bin", true);
oReq.responseType = "arraybuffer";

oReq.onload = function (oEvent) {
 var arrayBuffer = oReq.response; // Note: not oReq.responseText


 if (arrayBuffer) {
   var byteArray = new Uint8Array(arrayBuffer);

arr = byteArray;


 }
};

oReq.send(null);

   

// Writing to port //

  const writer = port.writable.getWriter();
  await writer.write(arr);

   }


        }
      }
    } catch (error) {
      console.log(error)
    }
  }

}).catch((e) => {
  console.log(e)

});

 
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!