Need help on reverse hex

Hello,
I'm newbie on Node-Red, I need help on reversing hex output from buffer parser.
Please let me know how to do this properly.

Thank you.

That is an unusual arrangement. No amount of byte-swapping or BE/LE operations will generate that hex string.

You will have to either...

  • pre-process the data array to reverse the 4 nibbles of each 16bit integer before sending to buffer parser
    OR
  • post-process the HEX string after buffer parser and reverse each 4 characters.

Are you 100% certain that is what you need?

  • Where does the source data come from and what does it represent?
  • How is the final data used?

Hi Steve,
Thanks for your reply. I have no idea, why would the manufacturer of product make it so complicated just to read the Serial Number. This is the data read from Modbus

Here is a version with a pre-processor function "Reverse Digits"

kk8vhvzcrM

[{"id":"48710450b1015ae2","type":"buffer-parser","z":"f9687f620c582b41","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"hex","name":"hex","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1290,"y":1980,"wires":[["be3855e7e3db7224"]]},{"id":"c01496387b913dbd","type":"inject","z":"f9687f620c582b41","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":1175,"y":1860,"wires":[["93a9fe50e7dce0f6"]],"l":false},{"id":"93a9fe50e7dce0f6","type":"function","z":"f9687f620c582b41","name":"[0xe011, 0x9111, 0x32a, 0x100]","func":"msg.payload = [0xe011, 0x9111, 0x32a, 0x100]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1350,"y":1860,"wires":[["12016d8462cdf276"]]},{"id":"be3855e7e3db7224","type":"debug","z":"f9687f620c582b41","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1450,"y":1980,"wires":[]},{"id":"12016d8462cdf276","type":"function","z":"f9687f620c582b41","name":"Reverse digits in 16bit data array","func":"let data = msg.payload;\n\nmsg.payload = data.map(e => {\n    let n1 = (e & 0xf000) >> 12\n    let n2 = (e & 0x0f00) >> 4\n    let n3 = (e & 0x00f0) << 4\n    let n4 = (e & 0x000f) << 12\n    return n1 | n2 | n3 | n4;\n})\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1360,"y":1920,"wires":[["48710450b1015ae2"]]},{"id":"57f54b59b6350372","type":"inject","z":"f9687f620c582b41","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":1175,"y":1820,"wires":[["f0b8995005a4403e"]],"l":false},{"id":"f0b8995005a4403e","type":"function","z":"f9687f620c582b41","name":"[0x4321, 0x8765, 0xcba9, 0x0fed]","func":"msg.payload = [0x4321, 0x8765, 0xcba9, 0x0fed]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1360,"y":1820,"wires":[["12016d8462cdf276"]]}]

WOW... It's works. Thank you very much for the help. Only left small problem now.
There seems inconsistency with the product datasheet. I will check this with the manufacturer.

Thanks again for your help. :wink:

If you look closely at the example shown in the datasheet you will see it is even more complex than at first appears. The most significant hex digit of the second word is not used (8 does not appear appear in the result).

Though perhaps that is a misprint, as if it were correct the algorithm would be even worse at getting the correct number I think. Is the output you showed in the first post exactly as received from modbus?

hahah.. yeah, I didn't expect the solution was that complicated just to read serial number value. I will try to read directly on the device by tomorrow morning. Currently the device is offline, since it's Grid Tie Inverter.

Here is actual output from Modbus. I've tried make modification on solution provided by Steve, to discard last digit on the string output. But cannot make it right, it's really hard for me to understand the code.

My guess is the serial number is simply 15 characters.

Use the original function code I wrote & simply remove the last character of the hex generated using a function of JSONata to trim the string to 15 chars

Select that flow (CTRL+click the timestamp inject node) then export (CTRL+E) to a reply & I'll show you

Here is the code. Many thanks for your help. :slight_smile:

[{"id":"324c152.7ff62ea","type":"inject","z":"769a8e99.43d96","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"3","x":2560,"y":140,"wires":[["379a0886.13d658"]]}]

That is only the inject.

Also, be sure to wrap flow in a code block to prevent forum damaging the code

```
[paste: flow, code: here]
```

EDIT

Hold SHIFT + CLICK (to select all connected nodes) then CTRL+E export

IEM2pYO6zf

ahh.. just realize, I can select all connected node with SHIFT + Click..

[{"id":"324c152.7ff62ea","type":"inject","z":"769a8e99.43d96","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"3","x":2560,"y":140,"wires":[["379a0886.13d658"]]},{"id":"379a0886.13d658","type":"function","z":"769a8e99.43d96","name":"Modbus Simulator","func":"var p0 = 57361;\nvar p1 = 37137;\nvar p2 = 810;\nvar p3 = 256;\n\n\nmsg.payload = { p0, p1, p2, p3 };\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":2770,"y":140,"wires":[["442fd7a8.a2f868","482de54b.9273ec"]]},{"id":"442fd7a8.a2f868","type":"debug","z":"769a8e99.43d96","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":3010,"y":120,"wires":},{"id":"482de54b.9273ec","type":"function","z":"769a8e99.43d96","name":"[0xe011, 0x9111, 0x32a, 0x100]","func":"msg.payload = [msg.payload.p0, msg.payload.p1, msg.payload.p2, msg.payload.p3]\nreturn msg;","outputs":1,"noerr":0,"x":2750,"y":220,"wires":[["d73ae348.0d79b"]]},{"id":"d73ae348.0d79b","type":"function","z":"769a8e99.43d96","name":"Reverse digits in 16bit data array","func":"let data = msg.payload;\n\nmsg.payload = data.map(e => {\n let n1 = (e & 0xf000) >> 12\n let n2 = (e & 0x0f00) >> 4\n let n3 = (e & 0x00f0) << 4\n let n4 = (e & 0x00f) << 12\n return n1 | n2 | n3 | n4;\n})\n\nreturn msg;","outputs":1,"noerr":0,"x":2760,"y":260,"wires":[["d1a5a539.599b78"]]},{"id":"d1a5a539.599b78","type":"buffer-parser","z":"769a8e99.43d96","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"hex","name":"hex","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2770,"y":300,"wires":[["b64a60b0.be0e1"]]},{"id":"b64a60b0.be0e1","type":"debug","z":"769a8e99.43d96","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":3050,"y":360,"wires":}]

You code is broken and cannot be imported as you did not wrap it in a code block.

In order to make code more readable and importable it is important to surround your code with three backticks
```
like this
```

You can edit and correct your post by clicking the pencil icon.

See this post for more details - How to share code or flow json

Please in future use a code block when posting code or flows.

So here is all it needed...

The "trim serial no" function code..

msg.payload = msg.payload.substr(0, 15).toUpperCase();  //trim to 15 chars & set upper case
return msg;

Works perfect. Thank you for your help, I appreciate it,.. for all the time you spent on this

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