【问题标题】:How to convert incoming buffer data from serial port to nodejs如何将传入的缓冲区数据从串口转换为nodejs
【发布时间】:2018-06-13 03:53:24
【问题描述】:

你好,我正在使用 Arduino 和节点 js 我发送和接收数据,但从 arduino 传入的数据是这样的:

 <Buffer 00 00 00 e0 e0 e0 00 e0 e0 e0>

<Buffer e0 e0 e0 e0 00 e0 e0 00 e0 00 e0 e0 e0>

如何将其解码为 UTF8

阿杜诺

int incomingByte = 0;

void setup() {

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

}

void loop() {

if (Serial.available() > 0) {

incomingByte = Serial.read(); // read the incoming byte:

Serial.print(incomingByte); 

}


}

【问题讨论】:

    标签: node.js node-serialport


    【解决方案1】:

    在 node.js 中你可以使用toString:

    console.log(incomeBuffer.toString('utf8'))
    

    【讨论】:

    • console.log(incomeBuffer.toString('utf8')) 不工作
    【解决方案2】:

    incomingByte.toString()

    encoding 要解码的字符编码。默认值:'utf8'

    here

    【讨论】:

      【解决方案3】:

      您可以使用 Node SerialPort 类中的readable.setEncoding 方法:

      const SerialPort =  require('serialport');
      
      var port = new SerialPort("COM3").setEncoding('utf8');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-27
        • 2019-01-01
        • 1970-01-01
        • 2023-03-02
        • 1970-01-01
        • 2021-10-29
        相关资源
        最近更新 更多