Thursday, 18 May 2017

NodeJS Buffer - reading data results in garbage characters

I am trying to read some data from some devices which are connected to my socket server written in NodeJS. Data is received as binary stream and I am following device specification to read each chunk properly. Specification is similar to this:

byte | size | type          | description
-------------------------------------------
   0 |    2 | unsigned int  | whatever...
   2 |    4 | unsigned int  | irrelevant...
   6 |  var | character str | etc...

When I receive data like this, I am reading each chunk with specific Buffer method, for example:

// example code
const tid = data.readUInt16BE(0);
const dataLength = data.readUInt32BE(2);
const rest = data.toString('utf8', 6);

The problem I have is reading this variable-length data. I am using data.toString('utf8', 6) (without end defined because I need just the rest of the data) but my rest variable contains some weird characters which I suspect are carriage returns but are showing differently in console every time. For example:

// in each response here, everything before $ character is garbage
// 'clean' response should look like:
// OK
// $DATA=1234567890
// $DATA=0987654321
// etc...

// example code, 1st try
const rest = data.toString('utf8', 6);
console.log(rest);
// results in:
// OK
// ��$DATA=1234567890
// ��$DATA=0987654321
// etc...

// example code, 2nd try
const rest = data.toString('utf8', 6);
console.log(rest);
// results in:
// OK
// i�$DATA=1234567890
// i�$DATA=0987654321
// etc...

// example code, 3rd try
const rest = data.toString('utf8', 6);
console.log(rest);
// results in:
// OK
// UB$DATA=1234567890
// UB$DATA=0987654321
// etc...

// example code, 4th try
const rest = data.toString('utf8', 6);
console.log(rest);
// results in:
// OK
// � $DATA=1234567890
// � $DATA=0987654321
// etc...

// well, you get the point...

The thing is that there are always 2 characters in the beginning of each line which I just cannot figure out what they are and why do they appear.

As I said, I think that this happens because of carriage return, so I tried to replace it in different ways:

const rest = data.toString('utf8', 6).replace('\r', '');
// or (makes no sense, but let's try anyways)
const rest = data.toString('utf8', 6).replace(/[\r]/g, '');
// or (hoped that this one will really work)
const rest = data.toString('utf8', 6).replace(/[\n\r]/g, '\n');
// etc...

but, of course, nothing works from my attempts, garbage characters always show up.

My questions are: how to figure out which characters are these and what can I try to do to remove them?



via errata

No comments:

Post a Comment