节点串行端口写入错误[错误:写入com端口(WriteFilex):无效句柄]第二次写入时出错

qyswt5oh  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(492)

当我第一次启动并写入时,我尝试使用节点串行端口在edc中写入,这是成功的,但第二次尝试返回错误,第三次尝试成功相同的行为继续
错误:

[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)

我已经附上了从write Execution获得的日志

[Error: Writing to COM port (WriteFileEx): Invalid handle]
Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed

我在每个事务之后关闭端口(port.close()),并在每个事务开始时初始化节点串行端口对象
代码:

const port = new SerialPort(serialPortConfig, { baudRate: parseInt(baudRateConfig) }).setEncoding('hex');  

const parser = new Readline();
port.pipe(parser);

port.write(data, (writeError) => {
  if (writeError) {
    console.log('[inside writeAndDrain write error::', writeError);
  }
 })

port.drain((drainError) => {
  if (drainError) {
    console.log('[inside writeAndDrain drain error::', drainError);
  }
});

port.on('data', function (data) {
  console.log('[response]:', data)
  port.unpipe(parser);
  port.destroy((destroyError) => {
      console.log('[destroyError]:', destroyError)
    })
  port.close();
})

有谁能给我解决这个问题的办法吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题