7

I have server receiving data from a client [GPS Device]. I have problem presenting the data (i.e the results obtained from the client) in a readable format. Below are the things I have tried.

Doing:

console.log(data)

I get

<Buffer d0 d7 3d 00 c4 56 7e 81>

Also tried

 console.log(data.toString())

But I get unwanted results:See below:

��A�V~�

Here is my full code:

var net = require('net');
var fs = require('fs');

var server = net.createServer(function (socket) {
  console.log('Server started: Waiting for client connection ...');
  console.log('Client connected:port,address: '+socket.remotePort,      socket.remoteAddress);
  socket.on('data', function (data) {
        var date = new Date();
        var today = date.getDate()+'_'+date.getMonth();
        fs.appendFile(today+'_log.txt', data, function (err) {
          if (err) throw err;
            console.log(data.toString())

    });
 });
});

server.listen(my_port, my_ip);

Thanks for your input.

7
  • Any idea about the data getting received? Whether it is base64 encoded or ascii? Commented Feb 18, 2014 at 11:04
  • No clear details on encoding used. One of my biggest problem. The protocal doc doesn't show Commented Feb 18, 2014 at 11:07
  • Not much help without knowing that. Trial and error might work. Here's a doc that can help. docs.nodejitsu.com/articles/advanced/buffers/how-to-use-buffers Commented Feb 18, 2014 at 11:09
  • I have found somewhere in the vendor website talking about 'ISO-8859-1' encoding standard. Let me find out about it. Commented Feb 18, 2014 at 11:19
  • ascii encoding gave me this result - PW=\u0000DV~\u0001 This is the only one that made some sense to me. Commented Feb 18, 2014 at 11:29

3 Answers 3

10

According to the documentation, you must specify an encoding to get a String instead of a Buffer:

Event: 'data'#
Buffer object
Emitted when data is received. The argument data will be a Buffer or String. Encoding of data is set by socket.setEncoding().

You could configure the socket to get the data in UTF-8, for example, with:

socket.setEncoding('utf8');
Sign up to request clarification or add additional context in comments.

Comments

1

Do it like this

socket.on('data', function (data) {
        var buff = Buffer.from(data);

But remember, lots of GPS devices use little-endian, so later you're going to have to decode the data as well.

Comments

0

Assuming the data in buffer is 7 bit ASCII,

console.log(data.toString('ascii'))

would resolve the problem.

1 Comment

The data in the buffer is clearly not 7 bit ASCII

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.