Connect to a TCP Server with special packet order

Hi everyone!

I need to connect my Node-RED Instance to a TCP Server in a special way. The packet is described as follows:

Description of the packet

  • 4 first bytes : the magic number 0xDEC0DA6E stored in little endian (lower byte first)
  • 4 next bytes : the length of the data in bytes stored in little endian (lower byte first)
  • the content : a JSON tree in UTF8

The JSON tree is no problem to create, but does anybody know how to get these additional bytes combined into the msg.payload to send out via the TCP Nodes?

Thanks for your help!
Florian

Hi @FloRu

Something like this may work.

I should note: I'm not good in this area,

// Assuming msg.payload is your JSON object

// Header
const HeaderBytes = Buffer.from("0xDEC0DA6E","hex")

// Length
const Str = JSON.stringify(msg.payload)
const SizeBytes = Buffer.allocUnsafe(4);
SizeBytes.writeInt32LE(Str.length);

// Data
const DataBytes = Buffer.from(Str, "utf-8");

msg.payload = Buffer.concat([HeaderBytes,SizeBytes,DataBytes])

return msg

EDIT
doesn't seem to work - wait for someone who knows what they are doing :smile:

I deleted the "0x" from the HeaderBytes so that the raw payload seems to be correct, but don't get any connection. [EDIT]: ...and - of course - changed the order of the Header Bytes because of the "lower byte first"...
Also the SizeBytes seems to be right - the first byte changes if I give one more character into the payload. But most probably there's some information missing for them to work.

I'll give a short feedback as soon as I managed to get the connection done!

Thanks!
Best,
Florian

1 Like

Here's the final Code:

// Header
const HeaderBytes = Buffer.from("6EDAC0DE", "hex");

// Length
const Str = JSON.stringify(msgOut);
const SizeBytes = Buffer.allocUnsafe(4);
SizeBytes.writeInt32LE(Str.length);

// Data
let DataBytes = Buffer.from(Str, "utf-8");

msg.payload = Buffer.concat([HeaderBytes, SizeBytes, DataBytes]);

Thanks for your help!

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.