# Modbus 16bit INT to 32bit signed float for 50+ signals

I see there are plenty of discussions on this topic here in the forums, but since none of these directly answered my question and most of them are already closed, I thought of sharing how I did this case. At least I can look this up myself, next time I bang my head with Modbus...

The setup is like this:

1. we have a Modbus Slave device that has 50+ tags in it
2. all tags are to be treated as 32bit FLOAT values, and some of these are signed
3. we want to process all these tags in one go with minimum amount of fuss and coding, so we get a neat list of floats

The solution
The solution to this problem is inspired by the Youtube video by @achmadinlabs2720 (Modbus RTU TCP/IP on Node-Red (16bit, 32bit and floating data) - YouTube) - Kudos to Achmad!

1. First step is to read in the data from Modbus. In my case I always use this node and since I have more than 50 32bit tags, it means reading in 100+ registers. In my case the Modbus Slave device is unit ID = 1 and has a zero indexing type of regs, so we start from 0. FC3 is the Modbus function "Read holding registers".
``````msg.payload = {
'fc': 3,
'unitid': 1,
'quantity': 120
};
return msg;
``````
1. We feed the above into the mighty Modbus-Flex-Getter node (where we have separately configured the Modbus server)

2. We grab the data from the Modbus-node, which in our case is 120 16bit integers

3. Now the first challenge is how to get these 120 register values to 32bit and doing this in a fairly simple way and doing ALL tags in one function node. We do it like this, by shifting the first register of each pair by 16bits with the << operatore.

``````// put all data in our raw array
// convert each pair to 32bit
// you may need to swap the order of the values, ie (raw << 16) + (raw)
// instead of (raw << 16) + (raw), if the word order is swapped
let tag1 = (raw << 16) + (raw);
let tag2 = (raw << 16) + (raw);
let tag3 = (raw << 16) + (raw);
let tag4 = (raw << 16) + (raw);
let tag5 = (raw << 16) + (raw);
// you can add here as many register pairs as you like, in our case we have 56
let tag55 = (raw << 16) + (raw);
let tag56 = (raw << 16) + (raw);

// and lastly spit these out as a new array
// note the bracket [ ie we don't use a curly bracket { here, since we need an array
tag1,
tag2,
tag3,
tag4,
tag5,
tag55,
tag56
]
return msg;
``````

The output will look like this

As you see, we now have a new array of numbers.

1. Then the next challenge is how to convert this to Float values. And here we use the float node. The beauty with this node is that it ALSO accepts an array of values, ie we can take all our 56 tags and convert them in one go to floats.

1. And all is well, until we notice that one or two of the tags is signed... Ie the original decimal value in the Modbus Slave is negative, like -0.1 or something like that. I am sure there are various ways of doing this, but for our purpose it was easiest to do this in our function node where we do the 16->32 conversion. So we add in this for the signed values.
``````let tag35 = (raw << 16) + (raw);
let tag36 = (raw << 16) + (raw);
// tag37 is signed 32bit, use >>> 0 operator to convert to signed
let tag37 = ((raw << 16) + (raw)) >>> 0;
let tag38 = (raw << 16) + (raw);
let tag39 = (raw << 16) + (raw);
``````

This will do the conversion nicely and with minimal fuss and we can feed it to the foFloat node, which then gives us the correct value like -0.1.

1. And lastly we create a new numbered tag list.

2. Oh and yes, we do use Excel CONCAT to create easily these longish lists of tag definitions

The complete flow is below.

``````[{"id":"75de6ce4c6486604","type":"function","z":"1f78ec24476b22f4","name":"Read 1-120","func":"msg.payload = {\n    'fc': 3, \n    'unitid': 1,\n    'address': 0,\n    'quantity': 120\n}; \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":80,"wires":[["11d71e27b87d8efb"]]},{"id":"11d71e27b87d8efb","type":"modbus-flex-getter","z":"1f78ec24476b22f4","name":"Modbus device","showStatusActivities":true,"showErrors":true,"showWarnings":true,"logIOActivities":false,"server":"31437533b8c066eb","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"delayOnStart":false,"startDelayTime":"","x":460,"y":80,"wires":[["ad01bbd36f355197","8805930fae41330f"],[]]},{"id":"ad01bbd36f355197","type":"function","z":"1f78ec24476b22f4","name":"Scale and tag","func":"let raw = msg.payload;\nlet tag1 = (raw << 16) + (raw);\nlet tag2 = (raw << 16) + (raw);\nlet tag3 = (raw << 16) + (raw);\nlet tag4 = (raw << 16) + (raw);\nlet tag5 = (raw << 16) + (raw);\nlet tag6 = (raw << 16) + (raw);\nlet tag7 = (raw << 16) + (raw);\nlet tag8 = (raw << 16) + (raw);\nlet tag9 = (raw << 16) + (raw);\nlet tag10 = (raw << 16) + (raw);\nlet tag11 = (raw << 16) + (raw);\nlet tag12 = (raw << 16) + (raw);\nlet tag13 = (raw << 16) + (raw);\nlet tag14 = (raw << 16) + (raw);\nlet tag15 = (raw << 16) + (raw);\nlet tag16 = (raw << 16) + (raw);\nlet tag17 = (raw << 16) + (raw);\nlet tag18 = (raw << 16) + (raw);\nlet tag19 = (raw << 16) + (raw);\nlet tag20 = (raw << 16) + (raw);\nlet tag21 = (raw << 16) + (raw);\nlet tag22 = (raw << 16) + (raw);\nlet tag23 = (raw << 16) + (raw);\nlet tag24 = (raw << 16) + (raw);\nlet tag25 = (raw << 16) + (raw);\nlet tag26 = (raw << 16) + (raw);\nlet tag27 = (raw << 16) + (raw);\nlet tag28 = (raw << 16) + (raw);\nlet tag29 = (raw << 16) + (raw);\nlet tag30 = (raw << 16) + (raw);\nlet tag31 = (raw << 16) + (raw);\nlet tag32 = (raw << 16) + (raw);\nlet tag33 = (raw << 16) + (raw);\nlet tag34 = (raw << 16) + (raw);\nlet tag35 = (raw << 16) + (raw);\nlet tag36 = (raw << 16) + (raw);\n// tag37 rpmPs is signed 32bit, use >>> 0 operator to convert\nlet tag37 = ((raw << 16) + (raw)) >>> 0;\nlet tag38 = (raw << 16) + (raw);\nlet tag39 = (raw << 16) + (raw);\nlet tag40 = (raw << 16) + (raw);\nlet tag41 = (raw << 16) + (raw);\nlet tag42 = (raw << 16) + (raw);\nlet tag43 = (raw << 16) + (raw);\n// tag44 rpmSb is signed 32bit, use >>> 0 operator to convert\nlet tag44 = ((raw << 16) + (raw)) >>> 0;\nlet tag45 = (raw << 16) + (raw);\nlet tag46 = (raw << 16) + (raw);\nlet tag47 = (raw << 16) + (raw);\nlet tag48 = (raw << 16) + (raw);\nlet tag49 = (raw << 16) + (raw);\nlet tag50 = (raw << 16) + (raw);\nlet tag51 = (raw << 16) + (raw);\nlet tag52 = (raw << 16) + (raw);\nlet tag53 = (raw << 16) + (raw);\nlet tag54 = (raw << 16) + (raw);\nlet tag55 = (raw << 16) + (raw);\nlet tag56 = (raw << 16) + (raw);\nmsg.payload = [\n    tag1,\n    tag2,\n    tag3,\n    tag4,\n    tag5,\n    tag6,\n    tag7,\n    tag8,\n    tag9,\n    tag10,\n    tag11,\n    tag12,\n    tag13,\n    tag14,\n    tag15,\n    tag16,\n    tag17,\n    tag18,\n    tag19,\n    tag20,\n    tag21,\n    tag22,\n    tag23,\n    tag24,\n    tag25,\n    tag26,\n    tag27,\n    tag28,\n    tag29,\n    tag30,\n    tag31,\n    tag32,\n    tag33,\n    tag34,\n    tag35,\n    tag36,\n    tag37,\n    tag38,\n    tag39,\n    tag40,\n    tag41,\n    tag42,\n    tag43,\n    tag44,\n    tag45,\n    tag46,\n    tag47,\n    tag48,\n    tag49,\n    tag50,\n    tag51,\n    tag52,\n    tag53,\n    tag54,\n    tag55,\n    tag56\n]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":80,"wires":[["e7306e59870db61d","73f92ed0032e7c70"]]},{"id":"0b8bd62e15fce13b","type":"debug","z":"1f78ec24476b22f4","name":"debug 4","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":920,"y":40,"wires":[]},{"id":"e7306e59870db61d","type":"toFloat","z":"1f78ec24476b22f4","name":"","toFixed":"2","x":790,"y":80,"wires":[["0b8bd62e15fce13b","66b7426cb45e6bf9"]]},{"id":"73f92ed0032e7c70","type":"debug","z":"1f78ec24476b22f4","name":"debug 7","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":780,"y":40,"wires":[]},{"id":"66b7426cb45e6bf9","type":"function","z":"1f78ec24476b22f4","name":"to taglist","func":"let data = msg.payload;\nlet tag1 = data;\nmsg.payload = {\n    \"1\": data,\n    \"3\": data,\n    \"5\": data,\n    \"7\": data,\n    \"9\": data,\n    \"11\": data,\n    \"13\": data,\n    \"15\": data,\n    \"17\": data,\n    \"19\": data,\n    \"21\": data,\n    \"23\": data,\n    \"25\": data,\n    \"27\": data,\n    \"29\": data,\n    \"31\": data,\n    \"33\": data,\n    \"35\": data,\n    \"37\": data,\n    \"39\": data,\n    \"41\": data,\n    \"43\": data,\n    \"45\": data,\n    \"47\": data,\n    \"49\": data,\n    \"51\": data,\n    \"53\": data,\n    \"55\": data,\n    \"57\": data,\n    \"59\": data,\n    \"61\": data,\n    \"63\": data,\n    \"65\": data,\n    \"67\": data,\n    \"69\": data,\n    \"71\": data,\n    \"73\": data,\n    \"75\": data,\n    \"77\": data,\n    \"79\": data,\n    \"81\": data,\n    \"83\": data,\n    \"85\": data,\n    \"87\": data,\n    \"89\": data,\n    \"91\": data,\n    \"93\": data,\n    \"95\": data,\n    \"97\": data,\n    \"99\": data,\n    \"101\": data,\n    \"103\": data,\n    \"105\": data,\n    \"107\": data,\n    \"109\": data,\n    \"111\": data\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":920,"y":80,"wires":[["74d72a657927c89c"]]},{"id":"74d72a657927c89c","type":"debug","z":"1f78ec24476b22f4","name":"debug 8","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1080,"y":40,"wires":[]},{"id":"4246cc8376664773","type":"comment","z":"1f78ec24476b22f4","name":"Modbus data","info":"","x":130,"y":40,"wires":[]},{"id":"c4b16c03af453a22","type":"inject","z":"1f78ec24476b22f4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":130,"y":80,"wires":[["75de6ce4c6486604"]]},{"id":"8805930fae41330f","type":"debug","z":"1f78ec24476b22f4","name":"debug 9","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":40,"wires":[]}]
``````