How to use function node to split hexdata

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.

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

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.

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;
}

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,

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

demo flow

[{"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
1 Like

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