Very Large Numbers Modulo 10k

Here is how modulo 10k is described.

64-bit unsigned value returned in four consecutive 16-bit registers, where R4 is the last register and R1 is the first register. Each registers range is 0 to +9,999:
R410,000^3 + R310,000^2 + R2*10,000 + R1

I have created a Modbus function read of four registers to create the math. The resulting numbers appear to be too large for Node-Red to use. What is the work around? Need to do these multiplications and then add the numbers together. Any help?

Use the BigInt data type.

Also, node-red-contrib-buffer-parser can do this for you.

Many thanks.
Trying to do the math with calculator function, first run the multiplication and then add them together.

Node Red will not allow me to add pallet node-red-contrib-buffer-parser.

Other posts indicate that BigInt will not work in newer revs of Node Red.

How is it stopping you? That is a well supported node that is widely used.

When I try to add these with the pallet adding tool the suggested items show up but I get an error message stating that the library will not load. I believe the issue has more to do with the fact that the modbus tcp block load (four registers) that I am using does not split the four values for me to use and do the math. Since it is one value I cannot use four reads. It needs to be a block so the math lines up.

Go into your .node-red folder and run
npm remove node-red-contrib-buffer-parser
npm install node-red-contrib-buffer-parser
and post the output here. Copy/paste not screenshot please. Use the </> button when pasting it here.

Which posts are those?

Will not allow me to send to you now

The calculation in a function node using BigInt can be achieived like this...

const HR0 = BigInt(msg.payload[0])
const HR1 = BigInt(msg.payload[1])
const HR2 = BigInt(msg.payload[2])
const HR3 = BigInt(msg.payload[3])

const result =  (HR3 * 1000000000000n) +
                (HR2 * 100000000n) +
                (HR1 * 10000n) +
                (HR0)
msg.payload = result
return msg;

Example...

Demo flow...

[{"id":"078ec5d1dc7504d8","type":"inject","z":"ec9cf53f5ff54f3b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":640,"y":80,"wires":[["5e61be43ece7fd86"]]},{"id":"5e61be43ece7fd86","type":"change","z":"ec9cf53f5ff54f3b","name":"Fake modbus FC3 READ 4 Registers","rules":[{"t":"set","p":"payload","pt":"msg","to":"[7131,9300,45,0]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":870,"y":80,"wires":[["d0e10296cab5f111"]]},{"id":"d0e10296cab5f111","type":"function","z":"ec9cf53f5ff54f3b","name":"function 7","func":"const HR0 = BigInt(msg.payload[0])\nconst HR1 = BigInt(msg.payload[1])\nconst HR2 = BigInt(msg.payload[2])\nconst HR3 = BigInt(msg.payload[3])\n\nconst result =  (HR3 * 1000000000000n) +\n                (HR2 * 100000000n) +\n                (HR1 * 10000n) +\n                (HR0)\nmsg.payload = result\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":790,"y":140,"wires":[["f2057fc77ed47e06"]]},{"id":"f2057fc77ed47e06","type":"debug","z":"ec9cf53f5ff54f3b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":990,"y":140,"wires":[]}]

BigInt does not show up in Palette Install Window anymore. When it did Node-Red Palette installer produce a "Failed to Install" message. That is a big drawback. I an experienced industrial HMI, PLC, SCADA programmer but not java person. I am running v3.0.1 on a Win 10 machine. Know that people are trying to help but BigInt is not showing up.

Regret that I do not understand. See BigInt in solution posted here.

What don't you understand?

Steve-Mcl. Since I am new to Node-Red I did not understand that I had to grab a blank function block and copy the code that you wrote as script into that block. As a Node-Red novice, it took me a few runs at your posts to understand the meaning. It is working now. Thanks for the patience with this subject. Was able to get Watts, KWatts and MegWatts with your help. Hans

1 Like

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