Help with creating a buffer of hex bytes from a string of hex words

We will be passing a string from MQTT that is formatted like this "0201 0141 4000 0841 1E90 0000 0000 8203 45"

Right now I just have that in an inject (as a string) that goes into a function. The values are already in hex. I need to get the top to this format ["0x02","0x01","0x01","0x41","0x40","0x00","0x08","0x41","0x1E","0x90","0x00","0x00","0x00","0x00","0x82","0x03","0x45"] in a buffer. When I inject that buffer into the write to usb my usb to rs485 works to control my board but I am stuck on how to get from the above to this format with function code

This should be a piece of cake to you @Steve-Mcl, right? :wink:

2 Likes

Here is the code that the buffer inject works with USB on


[{"id":"9a21f667.8fc0c8","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"3b8d164f.baeb12","type":"serial out","z":"9a21f667.8fc0c8","name":"","serial":"6316eb55.80e3a4","x":650,"y":220,"wires":[]},{"id":"fd418c64.ca7508","type":"inject","z":"9a21f667.8fc0c8","name":"Set to EOL","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x08\",\"0x41\",\"0x1E\",\"0x90\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x82\",\"0x03\",\"0x45\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":380,"y":220,"wires":[["3b8d164f.baeb12"]]},{"id":"475ce64b.f69e5","type":"inject","z":"9a21f667.8fc0c8","name":"Set Normal","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x08\",\"0x41\",\"0x1E\",\"0x17\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x83\",\"0x03\",\"0xC3\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":390,"y":280,"wires":[["3b8d164f.baeb12"]]},{"id":"4a6c0228.8aa92c","type":"inject","z":"9a21f667.8fc0c8","name":"MainBoard-Relay1Off","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4A\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":360,"y":380,"wires":[["3b8d164f.baeb12"]]},{"id":"9e59f466.668f48","type":"inject","z":"9a21f667.8fc0c8","name":"MainBoard-Relay2On","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x00\",\"0x00\",\"0x01\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4B\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":360,"y":420,"wires":[["3b8d164f.baeb12"]]},{"id":"4e239164.415098","type":"debug","z":"9a21f667.8fc0c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":650,"y":340,"wires":[]},{"id":"438754f0.729e24","type":"inject","z":"9a21f667.8fc0c8","name":"MainBoard-Relay1On","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x01\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4B\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":360,"y":340,"wires":[["3b8d164f.baeb12","4e239164.415098"]]},{"id":"6316eb55.80e3a4","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

Hi @jeffreys

in order to make code more readable and importable it is important to post it between two sets of three backticks - ``` - see this post for more details - How to share code or flow json

As it stands both your posts are unusable. Please edit them.

Thanks for the heads up. That better? The code I posted is my working USB write code with the correct formatting in the buffers.

1 Like

So you realise you can inject a buffer from the inject node right?

But if you want to convert a string like "0201 0141 4000 0841 1E90 0000 0000 8203 45" to a buffer then you can do this...

msg.oldPayload = msg.payload; //save it for later debugging
var noSpaces = msg.payload.replace(/ /g, '');// remove spaces
msg.payload = Buffer.from(noSpaces, "hex"); //convert to buffer
return msg;

demo flow...

[{"id":"7b50a866.e6d048","type":"inject","z":"3ffdd389.c6e0bc","name":"fake mqtt","topic":"","payload":"0201 0141 4000 0841 1E90 0000 0000 8203 45","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":460,"y":540,"wires":[["bbea88eb.a001a8"]]},{"id":"bbea88eb.a001a8","type":"function","z":"3ffdd389.c6e0bc","name":"funky string to buffer","func":"msg.oldPayload = msg.payload; //save it for later debugging\nvar noSpaces = msg.payload.replace(/ /g, '');// remove spaces\nmsg.payload = Buffer.from(noSpaces, \"hex\"); //convert to buffer\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":540,"wires":[["ddf36481.e0a718"]]},{"id":"ddf36481.e0a718","type":"debug","z":"3ffdd389.c6e0bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":830,"y":540,"wires":[]}]
3 Likes

THANK YOU! Much appreciated

I knew you'd crack this a lot faster. Meanwhile I was only able to cobble together this inferior version:

let str = msg.payload.split(' ').join('');
msg.payload = hexStringToBuffer(str);

return msg;

function hexStringToBuffer(str) {
  let a = [];
  for (let i = 0, len = str.length; i < len; i += 2) {
    a.push(parseInt(str.substr(i,2), 16));
  }
  
  return Buffer.from(a);
}

Great to learn there's even a simpler version. But what boggled me the most is why str.replaceAll(' ', ''); gave me an unknown function error. I'm running Node 14 and I'm pretty sure I've used the syntax multiple times earlier on. With that said, my JS seems to be getting rusty after a 6 month of break writing it at work...

2 Likes

I originally wrote it as a one liner but decided to be more verbose for clarity over showing off haha

In fairness I've done a fair bit with buffers and conversations of late.

Ps, I also seen replaceAll on mdn but it didn't work for me either (didn't look any further into it and flipped to the tried n tested regex replacement)

3 Likes

I believe I know why it doesn't work. It's still at an ECMA script proposal stage. At work we've been using TypeScript/Babel to transpile the code, with esnext enabled to support upcoming features. I didn't look into this but this is my hunch.

1 Like

My next step is to read the response when I write the packet which we got working above. Im used to basically drawing a line from the write to the input node of the USB read. There is not an input node on the USB read to control the flow. How do you read the response from the packet we sent after you send it?

Here is my code:

[{"id":"9a21f667.8fc0c8","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"3b8d164f.baeb12","type":"serial out","z":"9a21f667.8fc0c8","name":"","serial":"6316eb55.80e3a4","x":690,"y":360,"wires":[]},{"id":"fd418c64.ca7508","type":"inject","z":"9a21f667.8fc0c8","name":"Set to EOL","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x08\",\"0x41\",\"0x1E\",\"0x90\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x82\",\"0x03\",\"0x45\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":440,"y":360,"wires":[["3b8d164f.baeb12"]]},{"id":"475ce64b.f69e5","type":"inject","z":"9a21f667.8fc0c8","name":"Set Normal","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x08\",\"0x41\",\"0x1E\",\"0x17\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x83\",\"0x03\",\"0xC3\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":430,"y":400,"wires":[["3b8d164f.baeb12"]]},{"id":"4a6c0228.8aa92c","type":"inject","z":"9a21f667.8fc0c8","name":"MainBoard-Relay1Off","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4A\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":500,"wires":[["3b8d164f.baeb12"]]},{"id":"9e59f466.668f48","type":"inject","z":"9a21f667.8fc0c8","name":"MainBoard-Relay2On","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x00\",\"0x00\",\"0x01\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4B\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":540,"wires":[["3b8d164f.baeb12"]]},{"id":"438754f0.729e24","type":"inject","z":"9a21f667.8fc0c8","name":"MainBoard-Relay1On","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x01\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4B\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":460,"wires":[["3b8d164f.baeb12"]]},{"id":"8850d8be.bc3f88","type":"inject","z":"9a21f667.8fc0c8","name":"fake mqtt Set to end of line mode","topic":"","payload":"0201 0141 4000 0841 1E90 0000 0000 8203 45","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":580,"wires":[["b54fa00f.5ecc9"]]},{"id":"b54fa00f.5ecc9","type":"function","z":"9a21f667.8fc0c8","name":"Hex string to Hex buffer","func":"msg.oldPayload = msg.payload; //save it for later debugging\nvar noSpaces = msg.payload.replace(/ /g, '');// remove spaces\nmsg.payload = Buffer.from(noSpaces, \"hex\"); //convert to buffer\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":600,"wires":[["3b8d164f.baeb12"]]},{"id":"e47641e4.d18d38","type":"inject","z":"9a21f667.8fc0c8","name":"fake mqtt MainBoard-Relay1On","topic":"","payload":"0201 0141 4000 2220 1200 5A00 0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0003 4B","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":620,"wires":[["b54fa00f.5ecc9"]]},{"id":"484dec1a.a6fd5c","type":"serial in","z":"9a21f667.8fc0c8","name":"","serial":"6316eb55.80e3a4","x":870,"y":360,"wires":[["57476dc8.786784"]]},{"id":"57476dc8.786784","type":"debug","z":"9a21f667.8fc0c8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","targetType":"msg","x":1050,"y":360,"wires":[]},{"id":"6316eb55.80e3a4","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

Guess I should start a new post with this question since I checked your solution to this post

I've never used the USB node but so I can only help if you do the following...

  1. Set the debug to show complete msg
  2. Copy the data response you get and paste it (as code) into a reply
    image
  3. Tell me what each part should be (e.g. first 4 digits are temperature and should be 31.5 etc)

I set that as you stated for the complete message and I dont get anything in the debug or console. Its like it doesnt fire off. Just saw your other message. I can delete that post if we get it here to help keep the forum clean

Delete other thread since we are here now.

So your issue is more a case of getting no response?

That suggests the device doesn't receive or doesn't like the data you are sending it perhaps?

Edit...
It that's the case, then perhaps this should be a new thread but titled appropriately (apologies for the runaround - I thought you were getting data but didn't know how to format it)

1 Like

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