# How to use function node to split hexdata

**URL:** <https://discourse.nodered.org/t/how-to-use-function-node-to-split-hexdata/47457>\
**Category:** General\
**Created:** [22 June 2021 06:09 UTC](https://discourse.nodered.org/t/how-to-use-function-node-to-split-hexdata/47457 "2021-06-22T06:09:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eldho-Abraham-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eldho-abraham-coder/32/42208_2.png) [@Eldho-Abraham-coder](https://discourse.nodered.org/u/Eldho-Abraham-coder)\
**Post date:** [22 June 2021 06:09 UTC](https://discourse.nodered.org/t/how-to-use-function-node-to-split-hexdata/47457/1 "2021-06-22T06:09:33Z")

</div>

here i am sending binary coded switch values for each rooms to the serial node eg: 01,01..........02,03 .....means room1switch 1 is pressed and room2switch2,3 is pressed.

 ![Screenshot 2021-06-22 at 11.49.14 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b2ec1a09ed0aebd25fa478c04d137bdb34bab11.png)

---

<div class="post-metadata">

**Author:** ![Eldho-Abraham-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eldho-abraham-coder/32/42208_2.png) [@Eldho-Abraham-coder](https://discourse.nodered.org/u/Eldho-Abraham-coder)\
**Post date:** [22 June 2021 06:14 UTC](https://discourse.nodered.org/t/how-to-use-function-node-to-split-hexdata/47457/2 "2021-06-22T06:14:01Z")

</div>

if (msg.payload[0] == 1)  
return msg; //Output 1  
if (msg.payload[0] == 2)  
return msg; //Output 2

this is the function i am using.. but the data is not getting split

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 June 2021 06:24 UTC](https://discourse.nodered.org/t/how-to-use-function-node-to-split-hexdata/47457/3 "2021-06-22T06:24:32Z")

</div>

You return the the msg unaltered.

Before I answer the question, can I just ask what is transmitting the serial data? Are you in control of that? The reason I ask is that you could make your life much easier if you transmit JSON.

As for making sense of what you currently have, I would recommend installing buffer parser.

---

<div class="post-metadata">

**Author:** ![Eldho-Abraham-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eldho-abraham-coder/32/42208_2.png) [@Eldho-Abraham-coder](https://discourse.nodered.org/u/Eldho-Abraham-coder)\
**Post date:** [22 June 2021 06:43 UTC](https://discourse.nodered.org/t/how-to-use-function-node-to-split-hexdata/47457/4 "2021-06-22T06:43:32Z")

</div>

hi steve , thanks for answering , i am sending room switch status through rs485 with usart in half duplex mode . the is a master polling different slaves in each room. data will be like binary encoded ed: 01,03. in hex means the data is comming from room1 and switch 1 and switch 2 are pressed(00000011). there is no scope for json :).  
now i changed to this function but the second room2 is not getting data.  
if (msg.payload[0] == 0x1){  
msg.payload = msg.payload[1]; //Output 1  
return msg;  
}  
if (msg.payload[0] == 0x2){  
msg.payload = msg.payload[1]; //Output 2  
return msg;  
}

---

<div class="post-metadata">

**Author:** ![Eldho-Abraham-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eldho-abraham-coder/32/42208_2.png) [@Eldho-Abraham-coder](https://discourse.nodered.org/u/Eldho-Abraham-coder)\
**Post date:** [22 June 2021 07:00 UTC](https://discourse.nodered.org/t/how-to-use-function-node-to-split-hexdata/47457/5 "2021-06-22T07:00:03Z")

</div>

i could solve it with,  
if (msg.payload[0] == 0x1){  
var msg2 = {payload:msg.payload[1]}  
//return msg2;  
}  
if (msg.payload[0] == 0x2){  
var msg3 = {payload:msg.payload[1]}  
//return msg3;  
}  
return [msg2,msg3];  
thanks , is there any function to analyze this binary data and any particular ui to use in dashboard,

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 June 2021 07:21 UTC](https://discourse.nodered.org/t/how-to-use-function-node-to-split-hexdata/47457/6 "2021-06-22T07:21:32Z")

</div>

> [@Eldho-Abraham-coder](#):
>
> is there any function to analyze this binary data and any particular ui to use in dashboard,

As i said, you could use buffer parser to help you make sense of data...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/a/faa90dc8a2de8b93e7f2d4fb3b0b4145e5ca6c36.png)

demo flow

```auto
[{"id":"747b7ef2.be4c7","type":"buffer-parser","z":"1bd2b382.4dd4f4","name":"buffer-parser - fan out mode","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint8","name":"room","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"switch1","offset":1,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"switch2","offset":1,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"switch3","offset":1,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"switch4","offset":1,"length":1,"offsetbit":3,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":5,"x":1640,"y":1104,"wires":[["51c2906d.cde67"],["ee58b035.83c7e"],["d65306d3.bc9fe8"],["70ddd692.3c1aa8"],["2b402d5a.114542"]]},{"id":"f4d62ca2.3c856","type":"inject","z":"1bd2b382.4dd4f4","name":"fake data [1,1]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1,1]","payloadType":"bin","x":1334,"y":1088,"wires":[["747b7ef2.be4c7","eef352ae.220e"]]},{"id":"d7f09dbe.ec9d","type":"inject","z":"1bd2b382.4dd4f4","name":"fake data [2,3]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[2,3]","payloadType":"bin","x":1334,"y":1136,"wires":[["747b7ef2.be4c7","eef352ae.220e"]]},{"id":"95ce17f4.4df778","type":"inject","z":"1bd2b382.4dd4f4","name":"fake data [2,7]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[2,7]","payloadType":"bin","x":1334,"y":1184,"wires":[["747b7ef2.be4c7","eef352ae.220e"]]},{"id":"51c2906d.cde67","type":"debug","z":"1bd2b382.4dd4f4","name":"Room","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1904,"y":1056,"wires":[]},{"id":"ee58b035.83c7e","type":"debug","z":"1bd2b382.4dd4f4","name":"SW1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1906,"y":1088,"wires":[]},{"id":"d65306d3.bc9fe8","type":"debug","z":"1bd2b382.4dd4f4","name":"SW2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1906,"y":1120,"wires":[]},{"id":"70ddd692.3c1aa8","type":"debug","z":"1bd2b382.4dd4f4","name":"SW3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1906,"y":1152,"wires":[]},{"id":"2b402d5a.114542","type":"debug","z":"1bd2b382.4dd4f4","name":"SW4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1906,"y":1184,"wires":[]},{"id":"eef352ae.220e","type":"buffer-parser","z":"1bd2b382.4dd4f4","name":"buffer-parser - key/val mode","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint8","name":"room","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"switch1","offset":1,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"switch2","offset":1,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"switch3","offset":1,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"switch4","offset":1,"length":1,"offsetbit":3,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1640,"y":1232,"wires":[["91f267cd.ea0668"]]},{"id":"91f267cd.ea0668","type":"debug","z":"1bd2b382.4dd4f4","name":"Parsed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1916,"y":1232,"wires":[]}]

```

The fan out version is easy to visually understand but not exaclty future proof (as any additional switches would require additional wires.

The key/val version is slightly harder to grasp but is often the better solution as you can add to the buffer-parser any time without affecting wiring.

Both versions can be used to display values on dashboard.

- fan out mode - show `msg.payload` on dashboard item
- key/val mode - show `msg.payload.switch1` on dashboard item

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 July 2021 07:21 UTC](https://discourse.nodered.org/t/how-to-use-function-node-to-split-hexdata/47457/7 "2021-07-06T07:21:40Z")

</div>

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