5

hello I'am using Arduino and node js I sent and recive data but the data incoming from arduino like this :

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

How can i decode this to 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); 

}


}
SAMSOL
  • 90
  • 2
  • 8

4 Answers4

3

You can use readable.setEncoding method from Node SerialPort class:

const SerialPort =  require('serialport');

var port = new SerialPort("COM3").setEncoding('utf8');
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • thanks @heltonbiker- I tried to modify this value to utilize `utf8`, `ascii`, and `hex` with no luck. I imagine there might be some work I need to do on the C++ library side to get things in the right format, or at least to understand how it is sending data. It does say it supports SPI. – mheavers Jul 10 '22 at 22:51
2

In node.js you can use toString:

console.log(incomeBuffer.toString('utf8'))
stdob--
  • 28,222
  • 5
  • 58
  • 73
1

incomingByte.toString()

encoding The character encoding to decode to. Default: 'utf8'

look at here

hanfengcan
  • 11
  • 1
0

I used the Buffer class to do various conversions to get printable hex data.

const { SerialPort } = require('serialport')
const { Buffer } = require('buffer')

let buffer = []
port.on('data', function (data) {
    buffer.push(Buffer.from(data, 'ascii'))
}})

// Button handler or something else
let buffer_ascii = ''
buffer.forEach(chunk => {
    buffer_ascii += chunk.toString('hex') 
})
console.log(buffer_ascii)
Yvan Pearson
  • 319
  • 4
  • 6