【问题标题】:NodeJS not receiving data from serialportNodeJS没有从串口接收数据
【发布时间】:2018-02-11 07:17:11
【问题描述】:

我能够获取串行端口列表,并成功打开我想要的端口。但是,数据接收事件永远不会触发。端口打开后会发送少量数据。我已使用 USB 监视器验证该端口正在发送数据。

// Open event example
const SerialPort = require('serialport');
var port = new SerialPort('COM220', { autoOpen:true, baudRate: 921600 });


port.on('open', () => {
  console.log('Port Opened');

});

// Switches the port into "flowing mode"
port.on('data', function (data) {
  console.log('Data:', data);
});

// Read data that is available but keep the stream from entering "flowing mode"
port.on('readable', function () {
  console.log('Data:', port.read());
});

【问题讨论】:

  • port.on('error',.... 上的任何错误
  • port.on('error' 不会触发,所以我假设至少端口没有错误。

标签: javascript node.js serial-port


【解决方案1】:

我有一些想法:

  1. 波特率非常高,不在串行端口的标准中。我会开始低得多,以确保两个设备可以匹配。 38,4? 9600?这足以证明你的理论。
  2. 您可以记录 console.log(port) 端口本身并开始比较行为并查看结果。
  3. 可读模式与正常模式不同。这样做的结果是什么?

    port.on('数据', 函数(数据) { console.log(data.toString()); }

  4. 您可以通过在数据字符后面附加一个字符串来创建自己的缓冲区。

    var bfr = ""; port.on('数据',函数(数据){ bfr += data.toString(); 控制台.log(bfr); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-19
    • 2021-07-24
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    相关资源
    最近更新 更多