title: Node serial port write error [Error: Writing to COM port (WriteFileEx): Invalid handle] error occoured while write second time
body: <p>Iam trying to write in EDC using node serial port when i start and write first time it is success but the second try its return the error and 3rd try success same behaviour continuing</p>
  <p><strong>Error:</strong></p>
  <pre><code>[Error: Writing to COM port (WriteFileEx): Invalid handle]
  Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
  at onwriteError (internal/streams/writable.js:389:22)
  at onwrite (internal/streams/writable.js:426:7)
  </code></pre>
  <p>And i have attached the log which i got from write execption</p>
  <pre><code>[Error: Writing to COM port (WriteFileEx): Invalid handle]
  Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
  </code></pre>
  <p>Iam closing the port (port.close()) after every transaction and and i initialize the node serial port object at the begining of every transaction</p>
  <p><strong>Code:</strong></p>
  <pre><code>const port = new SerialPort(serialPortConfig, { baudRate: parseInt(baudRateConfig) }).setEncoding('hex');  
  
  const parser = new Readline();
  port.pipe(parser);
  
  port.write(data, (writeError) =&gt; {
    if (writeError) {
      console.log('[inside writeAndDrain write error::', writeError);
    }
   })
  
  port.drain((drainError) =&gt; {
    if (drainError) {
      console.log('[inside writeAndDrain drain error::', drainError);
    }
  });
  
  port.on('data', function (data) {
    console.log('[response]:', data)
    port.unpipe(parser);
    port.destroy((destroyError) =&gt; {
        console.log('[destroyError]:', destroyError)
      })
    port.close();
  })
  </code></pre>
  <p>Can anyone give me the solution for this?</p>