Need some help with float numbers

Hi,

I have some difficulties with float numbers. I am usind node-red modbus to read some modbus values. I read two values and they are in array [34853, 200]. I need to convert that first number to negative float. Can someone help ? also I return two values from one output.

var msg1 = {payload: msg.payload[0], topic: "RW4010"};
var msg11;
if (msg1 > 32767) {
  msg11 = -65536 + msg1;
} else if (msg1 <= 32767) {
  msg11 = msg1;
}
var msg2 = {payload: msg.payload[1], topic: "RW4011"};
var msg21;
if (msg2 > 32767) {
  msg21 = -65536 + msg2;
} else if (msg1 <= 32767) {
  msg21 = msg2;
}
return [ [msg11, msg21] ];

As modbus returns 16bit number, that "first number" is a 16bit (2 byte) number. A float is 32bit (4 bytes) so I presume the manual for your modbus device will give you a formula for converting a 2 byte value into a float.

My guess is you should be reading an int (not UINT) and then probably divide by 10 or 100 or 1000

e.g. - using buffer-parser...

demo flow

[{"id":"7eb013ca918cae85","type":"inject","z":"1683bd9a.5e0a02","name":"faking modbus values","props":[{"p":"payload_as_buffer","v":"[136,37,0,200]","vt":"bin"},{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[34853, 200]","payloadType":"json","x":2100,"y":540,"wires":[["8df40ef3a7542857"]]},{"id":"8df40ef3a7542857","type":"buffer-parser","z":"1683bd9a.5e0a02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"myFloat_div10","offset":0,"length":1,"offsetbit":0,"scale":"/ 10","mask":""},{"type":"int16be","name":"myFloat_div100","offset":0,"length":1,"offsetbit":0,"scale":"/ 100","mask":""},{"type":"int16be","name":"myFloat_div1000","offset":0,"length":1,"offsetbit":0,"scale":"/ 1000","mask":""},{"type":"int16be","name":"myNumber","offset":2,"length":1,"offsetbit":0,"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":2070,"y":600,"wires":[["16902e50e15dced7"]]},{"id":"16902e50e15dced7","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":600,"wires":[]}]

alternative version sending multiple messages from one output...

[{"id":"7eb013ca918cae85","type":"inject","z":"1683bd9a.5e0a02","name":"faking modbus values","props":[{"p":"payload_as_buffer","v":"[136,37,0,200]","vt":"bin"},{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[34853, 200]","payloadType":"json","x":2100,"y":540,"wires":[["8df40ef3a7542857"]]},{"id":"8df40ef3a7542857","type":"buffer-parser","z":"1683bd9a.5e0a02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"myFloat_div10","offset":0,"length":1,"offsetbit":0,"scale":"/ 10","mask":""},{"type":"int16be","name":"myFloat_div100","offset":0,"length":1,"offsetbit":0,"scale":"/ 100","mask":""},{"type":"int16be","name":"myFloat_div1000","offset":0,"length":1,"offsetbit":0,"scale":"/ 1000","mask":""},{"type":"int16be","name":"myNumber","offset":2,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2070,"y":600,"wires":[["16902e50e15dced7"]]},{"id":"16902e50e15dced7","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":600,"wires":[]}]

Another alternative version - results to own outputs (fan out)

[{"id":"7eb013ca918cae85","type":"inject","z":"1683bd9a.5e0a02","name":"faking modbus values","props":[{"p":"payload_as_buffer","v":"[136,37,0,200]","vt":"bin"},{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[34853, 200]","payloadType":"json","x":2040,"y":540,"wires":[["8df40ef3a7542857"]]},{"id":"8df40ef3a7542857","type":"buffer-parser","z":"1683bd9a.5e0a02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"myFloat_div10","offset":0,"length":1,"offsetbit":0,"scale":"/ 10","mask":""},{"type":"int16be","name":"myFloat_div100","offset":0,"length":1,"offsetbit":0,"scale":"/ 100","mask":""},{"type":"int16be","name":"myFloat_div1000","offset":0,"length":1,"offsetbit":0,"scale":"/ 1000","mask":""},{"type":"int16be","name":"myNumber","offset":2,"length":1,"offsetbit":0,"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":4,"x":2050,"y":640,"wires":[["16902e50e15dced7"],["575a304c4ce43036"],["f23cf12dcee6af06"],["10843da0954e4d4e"]]},{"id":"16902e50e15dced7","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":580,"wires":[]},{"id":"575a304c4ce43036","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":620,"wires":[]},{"id":"f23cf12dcee6af06","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":660,"wires":[]},{"id":"10843da0954e4d4e","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2250,"y":700,"wires":[]}]

Thank you for info Steve, but maybe you know how to do this in function block ? :smile:

Yes, but i dont because there is a contrib node to do it for me.

Do you not want to use buffer-parser for some reason?

Yes, because I want to put this to one function. Optimizing a node-red flow little bit.

Premature optimisation is the root of all evil

I gave you a solution that works - if it is a problem - you can later optimise.

Thank you for your help Steve :smiley:

And here is proof...

But maybe you can show what it is inside those function blocks ?

Sure, but like I said, there is already a working solution (3 of them) in my first post that is as fast as (and often faster) than DIY solution.

Here you go anyway...

for the functions below to work, you need to get the path to the buffer (usually something like msg.responseBuffer.buffer or something like that.

Output a single object...

/** @type {Buffer} */var buf = msg.payload_as_buffer;
var int1 = buf.readInt16BE(0);
var int2 = buf.readInt16BE(2);

msg.payload = {
    myFloat_div10: int1 / 10,
    myFloat_div100: int1 / 100,
    myFloat_div1000: int1 / 1000,
    myNumber: int2
}

return msg;

output stream of 4 messages...

/** @type {Buffer} */var buf = msg.payload_as_buffer;
var int1 = buf.readInt16BE(0);
var int2 = buf.readInt16BE(2);

node.send({ topic: "myFloat_div10", payload: int1 / 10});
node.send({ topic: "myFloat_div100", payload: int1 / 100});
node.send({ topic: "myFloat_div1000", payload: int1 / 1000});
node.send({ topic: "myNumber", payload: int2});

fan out 4 messages...

/** @type {Buffer} */var buf = msg.payload_as_buffer;
var int1 = buf.readInt16BE(0);
var int2 = buf.readInt16BE(2);

var m1 = { topic: "myFloat_div10", payload: int1 / 10};
var m2 = { topic: "myFloat_div100", payload: int1 / 100};
var m3 = { topic: "myFloat_div1000", payload: int1 / 1000};
var m4 = { topic: "myNumber", payload: int2};

node.send([m1, m2, m3, m4]);
1 Like

Thank you for your help :smiley:

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