The following code uses SerialPort module to listen to data from a bluetooth connection.
I am expecting to see a stream of data in Hexadecimal format printed in console. But the console just shows some weird simbols. I want to know how can I decode and display the data in console.
var serialPort = new SerialPort("/dev/tty.EV3-SerialPort", { parser: SP.parsers.raw }, false); // this is the openImmediately flag [default is true] serialPort.open(function () { console.log('open'); serialPort.on('data', function(data) { var buff = new Buffer(data, 'utf8'); //no sure about this console.log('data received: ' + buff.toString()); }); });
Buffers have a toString() method that you can use to convert the buffer to a string. By default, toString() converts the buffer to a string using UTF8 encoding. For example, if you create a buffer from a string using Buffer. from() , the toString() function gives you the original string back.
What Are Buffers? The Buffer class in Node. js is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data.
This code will show the data buffer as a hex string:
buff.toString('hex');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With