Single Bit's to Integer

Hello,
unfortunately I have little experience with Node Red and json in particular.

The following problem:
I generate several bits in Nodered and then want to pass them to a PLC in an 16bit integer word via ADS.

So I need a function with 16 individual bits on the input side and a 16bit int. value as output.

thx in advance for your help!!

How do you generate them - show us.

Node-RED nodes will only ever have 1 input. This is by design. There are various methods for "tagging" the input data to differentiate "things". For example, a topic of "bit0" and payload of true can be easily differentiated from a topic of "bit1", payload true

Like wise, the payload can be an object e.g.

{
  bit0: true,
  bit1: true,
  bit2: false
}

or an array [true, true, false]

or an array of numbers [1,1,0]

and so on.


to help you further, first explain what you have (and show us).

I get single bits (true/false) via Google and want to send them combined via an integer value to a Beckhoff PLC. There I can decode the integer value into the individual bits.

image
something like that only in reverse

A basic and absolute truth in node red, it that messages NEVER arrive at the same input of a node at the same time.

The fact you have (in this screenshot) 2 sources of data and want to combine these means you should be looking at the join node to "join" the values into one message, then, once all data is in 1 message, you can begin to work out how to convert the values into a single integer.

This cookbook example explains the join node: Create a single message from separate streams of messages : Node-RED

If you want further help, copy a few REAL data examples (using the "Copy Value" button that appears under your mouse when you hover over a debug message in the side bar) and paste them in a reply as text (not screenshots)

[{"id":"7a7ab50bfdbb36b9","type":"join","z":"f0827cc6.fe0a7","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1170,"y":260,"wires":[["8fedfbdf19d8dd4a"]]},{"id":"a854d24607b174fc","type":"inject","z":"f0827cc6.fe0a7","name":"Bit_0_True","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Bit_0","payload":"true","payloadType":"bool","x":960,"y":180,"wires":[["7a7ab50bfdbb36b9"]]},{"id":"c19636ac99c0dff4","type":"inject","z":"f0827cc6.fe0a7","name":"Bit_0_False","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Bit_1","payload":"false","payloadType":"bool","x":970,"y":220,"wires":[["7a7ab50bfdbb36b9"]]},{"id":"0b8e3816c26b29c3","type":"inject","z":"f0827cc6.fe0a7","name":"Bit_1_True","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Bit_1","payload":"true","payloadType":"bool","x":960,"y":300,"wires":[["7a7ab50bfdbb36b9"]]},{"id":"3701308d0b13e071","type":"inject","z":"f0827cc6.fe0a7","name":"Bit_1_False","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Bit_1","payload":"false","payloadType":"bool","x":970,"y":340,"wires":[["7a7ab50bfdbb36b9"]]},{"id":"8fedfbdf19d8dd4a","type":"debug","z":"f0827cc6.fe0a7","name":"Int_Value","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1400,"y":260,"wires":[]}]

If Bit 0 is true > Int Value should 1
If Bit 1 is true > Int Value should 3
.....

Here is one way:

chrome_FEMSjUAG5m

[{"id":"7a7ab50bfdbb36b9","type":"join","z":"a12c69dd3cb81690","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1890,"y":460,"wires":[["8fedfbdf19d8dd4a","8dc03c2edc7f3610"]]},{"id":"a854d24607b174fc","type":"inject","z":"a12c69dd3cb81690","name":"Bit_0_True","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Bit_0","payload":"true","payloadType":"bool","x":1680,"y":380,"wires":[["7a7ab50bfdbb36b9"]]},{"id":"c19636ac99c0dff4","type":"inject","z":"a12c69dd3cb81690","name":"Bit_0_False","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Bit_0","payload":"false","payloadType":"bool","x":1690,"y":420,"wires":[["7a7ab50bfdbb36b9"]]},{"id":"0b8e3816c26b29c3","type":"inject","z":"a12c69dd3cb81690","name":"Bit_1_True","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Bit_1","payload":"true","payloadType":"bool","x":1680,"y":500,"wires":[["7a7ab50bfdbb36b9"]]},{"id":"3701308d0b13e071","type":"inject","z":"a12c69dd3cb81690","name":"Bit_1_False","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Bit_1","payload":"false","payloadType":"bool","x":1690,"y":540,"wires":[["7a7ab50bfdbb36b9"]]},{"id":"8fedfbdf19d8dd4a","type":"debug","z":"a12c69dd3cb81690","name":"Int_Value","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2080,"y":460,"wires":[]},{"id":"01d225c3b918a471","type":"debug","z":"a12c69dd3cb81690","name":"debug 239","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2110,"y":560,"wires":[]},{"id":"8dc03c2edc7f3610","type":"function","z":"a12c69dd3cb81690","name":"make integer","func":"function updateBit(currentValue, bitNo, bitValue) {\n    bitNo = parseInt(bitNo)\n    const bitValueNormalized = (bitValue === \"true\" || bitValue === true || bitValue == \"1\") ? 1 : 0\n    const clearMask = ~(1 << bitNo);\n    return (currentValue & clearMask) | (bitValueNormalized << bitNo);\n}\n\nlet result = 0 // starting value\nlet data = msg.payload\nlet keys = Object.keys(data) // get prop names in payload data\n\n// loop prop names Bit_0, Bit_1, ...\nfor (let i = 0; i < keys.length; i++) {\n    // use regex to grab the bit number from the prop name\n    let key = keys[i]\n    let bitRegex = /.*(\\d+).*/.exec(key)\n    if (bitRegex && bitRegex.length) {\n        let bitNo = bitRegex[1]\n        // update the bit in result, based on values in the input data\n        result = updateBit(result, bitNo, data[key])\n    }\n}\n\nmsg.payload = result\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1930,"y":560,"wires":[["01d225c3b918a471"]]}]

Great, I wouldn't have been able to do that on my own.
Thank you very much!

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