TCP outputs a string instead of byte values

Hi, i wrote a function, it outputs an object message with an array in payload and bytes. I need those bytes to been sent into tcp packet. Actually it converts the buffer to a string and also add commas! how can I overcome it?
Here is the last parto fo the code:
var buffer = new Uint8Array (12);
buffer= [0x55, 0x33, 0x61, 0x39, 0x02, zona, comando1, comando2, comando3, checksum, 0xaa, 0xaa];
msg={payload:buffer};
return msg;

Here is the debug log in Node red, it sent correct to TCP:
24/1/2019, 12:06:41node: cfc34e3f.3c648msg : Object

object

payload: array[12]

[0 … 9]

0: 85

1: 51

2: 97

3: 57

4: 2

5: 0

6: 2

7: 16

8: 146

9: 164

[10 … 11]

10: 170

11: 170

_msgid: "2c01d1a2.dc9b5e"

here is what I sniff receiving the TCP packet:
<- {192.168.1.251/37272} 38 35 2C 35 31 2C 39 37 2C 35 37 2C 32 2C 30 2C 32 2C 31 36 2C 31 34 36 2C 31 36 34 2C 31 37 30 2C 31 37 30
which translated to ascii is:
85,51,97,57,2,0,2,16,146,164,170,170

it's the buffer, converted in decimal, then string, and added commas!
I need (hex) {192.168.1.251/37272} 55 33 61 39 02 etc.etc
thank you!

try this for your function instead

var buffer = Buffer.from( [0x55, 0x33, 0x61, 0x39, 0x02, zona, comando1, comando2, comando3, checksum, 0xaa, 0xaa] );
return {payload:buffer};

assuming zona and other variables are set of course

1 Like

thank you works perfect,
i used the same to produce a simple string followed by "carrier return". Maybe for this there can be a simpler way?

i need to send PWON, i need this payload:
msg : Object

object

payload: buffer[6]raw

0: 0x50

1: 0x57

2: 0x4f

3: 0x4e

4: 0xa

5: 0xd

now i used this function, thanks to dceejay:
var comando = "PWON"
var buffer = new Uint8Array (comando.length+2);
for (var i=0; i<comando.length; i++) {
buffer[i]=comando.charCodeAt(i);buffer[i+1]=0x0a;buffer[i+2]=0x0d;}
buffer2= Buffer.from(buffer);
return {payload:buffer2};

 var comando = "PWON";
 return {payload: Buffer.from( comando + "\r\n" ) };
1 Like