如何将传入的缓冲区数据从串口转换为nodejs

时间:2018-01-03 13:02:07

标签: node.js node-serialport

你好我使用的是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

arduino

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

}


}

3 个答案:

答案 0 :(得分:1)

在node.js中,您可以使用toString

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

答案 1 :(得分:0)

incomingByte.toString()

  

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

查看here

答案 2 :(得分:0)

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

const SerialPort =  require('serialport');

var port = new SerialPort("COM3").setEncoding('utf8');
相关问题