How do I simulate a hexadecimal input to be parsed to string

I am trying to simulate data from a ble device receiving hex data. I need to parse it and display the string. How do I simulate that in node-red?

So far I have an inject node, function node and debug node, but what do I make the inject node in order to simulate 0x65 for example?

Do you mean a two char string "65" a four char string "0x65" or a byte represented in hex by 0x65 (decimal 101)?

1 Like

No, my relay board takes for example command 0x65, which i understand is hexadecimal. I need to convert it to its string form of "e".

So yes, the byte in hex represented by 0x65

This will send a single byte of value (decimal) 101

ok but then im using the function node with msg.payload.toString() but I get this error:
"Function tried to send a message of type string"

// Create a Date object from the payload

var resultado = msg.payload.toString();

// Return the message so it can be sent on
return resultado;

I think this is what you are after to convert it to an "e"

msg.payload = msg.payload.toString('utf8');
return msg;

ok thanks. For future reference (of mine) in the function I had this code:

// Create a Date object from the payload
var resultado = msg.payload.toString('utf8');

// Return the message so it can be sent on
return resultado;

which effectively took the value from msg and put it into resultado, returning resultado. This gave an error:

"Function tried to send a message of type string"

because what is passed over to the debug node, is msg.payload. Is there a way to pass resultado to the debug node and manipulate it over there? Or do I always have to replace the msg variable?

Function nodes must always return a Javascript object -- your code pulls the payload value (a buffer of 1 character) out of the incoming msg object, converts it to a string, and tries to return a bare string. This is not allowed, since the downstream nodes always expect to receive a msg object.

The best practice would be to reuse the incoming msg object, but replace the payload field:

// replace the payload buffer with the string value
msg.payload = msg.payload.toString('utf8');
// return the original msg with the new string payload
return msg;

This practice ensures that any other properties (or fields) of the incoming msg (e.g. topic, parts, msgid, req, res, etc) are not destroyed -- which can cause things like the join and http response nodes to not work.

Also, simulating buffer input in an inject node is pretty simple, since there is a buffer option. If you click the ... next to the input field, it opens a buffer editor panel which shows the hex values of each character as you type:

image

Your case is a bit different, since you know the hex value -- in this case, you can type each hex string into the field directly, as long as you use brackets to denote an array, double quotes around each hex "string", and separate each hex string with a comma, like so:

image

Whew! -- ugly, but it works...

NR Team: are there any plans to enhance the Buffer input field to accept valid syntax for JS arrays? Right now if I type "hello" in the buffer editor, then the field value is set to [104,101,108,108,111] (the ascii equivalent). But the alternate hex (or octal, or binary) forms like [0x68,0x65,0x6C,0x6C,0x6F] are treated as a plain string.

2 Likes

@shrickus - thank you. This has helped my in my Node-RED project to send some check data to serial interface from Inject node !