node.js serialport-TypeError:“偏移量”不是整数

时间:2019-06-17 08:12:21

标签: node.js node-serialport

我正在尝试打开一个串行端口并读取传入的数据:

const SerialPort = require('serialport');

var port = new SerialPort(
  "/dev/ttyUSB0",
  { baudRate: 115200 }
);

port.on('data', data => {
  console.log(data);
});

port.on('error', err => {
  console.log('Error', err);
});

port.on('close', err => {
  console.log('Closed', err);
});

只要有新数据到达,上面的示例就会以TypeError: "offset" is not an integer失败:

Closed { TypeError: "offset" is not an integer
    at LinuxBinding.read (/home/kristupas/projects/node-factory-worker/node_modules/@serialport/binding-abstract/binding-abstract.js:89:13)
    at LinuxBinding.read (/home/kristupas/projects/node-factory-worker/node_modules/@serialport/bindings/lib/linux.js:63:17)
    at SerialPort._read (/home/kristupas/projects/node-factory-worker/node_modules/@serialport/stream/stream.js:374:16)
    at SerialPort.Readable.read (_stream_readable.js:462:10)
    at maybeReadMore_ (_stream_readable.js:610:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:17) disconnected: true }

我已经尝试过@serialport/terminalsource here)并按预期工作,因此显然我的代码出了点问题,但是据我所知,我正确地遵循了文档。 / p>

1 个答案:

答案 0 :(得分:1)

您正在使用哪个版本的serialport?确保您没有使用8.0.0分支。