Int32 to binary - ie, unpack bits from PLC/PAC

A customer had an Int32 from a PAC Controller that was bit-packed with pump status.
Each bit of the variable was a pump on/off status that he wanted to show on an LED in groov.
(I have added bit select for the binary gadgets to the groov feature request list).

Granted, you are more likely to see this issue with a PLC than a PAC, but he wanted to get the data into groov without having to do a download via PAC Control (and do the unpack in the strategy - at the expense of extra tag names as well).

As it happens this customer was running groov on an AR1 and so had Node-RED running in the background.
Simple matter then for him to bring the Int32 into Node-RED via a PAC Node, unpack the bits and then connect them to a groov Data Store and then connect the LED gadgets to the Data Store Tags.

Figured the code was worthy enough to post here.
(Many many thanks to @mstjohn for the Function Node code - my first attempts were, ah… buggy…)

Here is what it looks like;

I hope its pretty clear. I put the test nodes in there so you can import the flow and test it out.
The debug nodes will show the binary result for the Int32.
As per the comment nodes, just swap out the input and output test nodes for your Nodes and you should be up and running.
We figured 8 bit would cover most people, but feel free to expand the code as required.

[{"id":"1382a7c7.0a762","type":"inject","z":"bfafb09d.34a88","name":"","topic":"","payload":"7","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":160,"wires":[["df41c583.24ff58"]]},{"id":"df41c583.24ff58","type":"function","z":"bfafb09d.34a88","name":"int32 to binary","func":"// initialize bit array to 0s\nvar bits0thru31 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];\n\nfor (i = 0; i < 32; i++){\n bits0thru31[i] = (msg.payload >> i) & 0x1;\n}\n\nvar bit0 = {payload: bits0thru31[0]};\nvar bit1 = {payload: bits0thru31[1]};\nvar bit2 = {payload: bits0thru31[2]};\nvar bit3 = {payload: bits0thru31[3]};\nvar bit4 = {payload: bits0thru31[4]};\nvar bit5 = {payload: bits0thru31[5]};\nvar bit6 = {payload: bits0thru31[6]};\nvar bit7 = {payload: bits0thru31[7]};\n\nreturn[ bit0, bit1, bit2, bit3, bit4, bit5, bit6, bit7 ];\n","outputs":"8","noerr":0,"x":580,"y":200,"wires":[["aa201119.796068"],["8756f456.0d6bf"],["6d516811.372178"],["948f3ad4.183b88"],["8f435811.a4be68"],["cdb17f80.fac8e8"],["75ff10d8.fba63"],["e5d55421.de459"]]},{"id":"aa201119.796068","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":60,"wires":[]},{"id":"8756f456.0d6bf","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"false","x":810,"y":100,"wires":[]},{"id":"6d516811.372178","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"false","x":810,"y":140,"wires":[]},{"id":"948f3ad4.183b88","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":180,"wires":[]},{"id":"b0359ef5.fc504","type":"inject","z":"bfafb09d.34a88","name":"","topic":"","payload":"13","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":200,"wires":[["df41c583.24ff58"]]},{"id":"8ee3c07b.e2f6e8","type":"inject","z":"bfafb09d.34a88","name":"","topic":"","payload":"16","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":240,"wires":[["df41c583.24ff58"]]},{"id":"634a4cb4.87fb64","type":"inject","z":"bfafb09d.34a88","name":"64","topic":"","payload":"64","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":280,"wires":[["df41c583.24ff58"]]},{"id":"8f435811.a4be68","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":220,"wires":[]},{"id":"d3de9e7d.6e1768","type":"inject","z":"bfafb09d.34a88","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":120,"wires":[["df41c583.24ff58"]]},{"id":"cdb17f80.fac8e8","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":260,"wires":[]},{"id":"75ff10d8.fba63","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":300,"wires":[]},{"id":"e5d55421.de459","type":"debug","z":"bfafb09d.34a88","name":"","active":true,"console":"false","complete":"payload","x":810,"y":340,"wires":[]},{"id":"38c7fe04.c374d2","type":"inject","z":"bfafb09d.34a88","name":"128","topic":"","payload":"128","payloadType":"num","repeat":"","crontab":"","once":false,"x":350,"y":320,"wires":[["df41c583.24ff58"]]},{"id":"f5940c68.5bf178","type":"comment","z":"bfafb09d.34a88","name":"Swap test Nodes for PLC/PAC output Node","info":"","x":400,"y":80,"wires":[]},{"id":"3ea62b9f.10e8a4","type":"comment","z":"bfafb09d.34a88","name":"Swap debug Nodes for groov Data Store nodes for LED gadget","info":"","x":810,"y":400,"wires":[]}]

HI there

New to node red, doing an import and it says syntax error at position 2…Quick look I dont see a syntax error…Simply cut and importing it into node red…

Could anyone point me in the right direction please …

thankyou…Peter

Yeah, sorry about the import… I put that code up before we found out that the forums formatting was not kind to the JSON.
You must make sure you post the code as ‘Preformatted Text’ which I have now done.
I think you will find it imports without error now (I tested it and it works).

Also, welcome to the forums Peter. Glad you joined us.

1 Like

Hi Beno

Thanks for fixing the exported flow paste :slight_smile: Imported fine :slight_smile: I have a flow talking to my PLC and its all starting to make sense :slight_smile:

Thanks for the quick reply…Pete

1 Like