how to read modbus TCP holding values (03) - 32bit float word swap (CD AB) with node red function block ?
I have problem with modbus...
Reading from modbus TCP ( FC3 , data size 32 bit Float, address 272 decimal)...
When Node RED read values ( FC 3, read holding registers, quantity 2 ) returns values like [0,16833] ... Here I'm using msg.payload=msg.payload[1] in function to get value 16833 out from array...
This is my temperature sensor value..
To see if modbus address is correct I'm using external app to read values from sensors ( Rilheva modbus poll ) ...
Correct value is reading from PLC module when is set to :
Read holding values (03) - 32bit float word swap (CD AB) - see screen...
So, does anybody knows how to convert it to real value - here it is 24.25...
You can first save the two integers to a buffer (swapping the words as you mentioned in the OP). Afterward, read the buffer as a float.
This is how the function node will look like (first line added for testing purposes).
msg.payload = [0, 16833];
let pay = msg.payload;
const buf = Buffer.allocUnsafe(4);
buf.writeInt16BE(pay[0],2);
buf.writeInt16BE(pay[1],0);
msg.payload = buf.readFloatBE(0);
return msg;
When testing with your sample data [0,16833] you will get msg.payload = 24.125
this works, it shows correct value.(1-wire temperature sensor 18b20).. I changed first line with msg.payload = [0, msg.payload[1]]; ..... but if I use another register where is input value analog voltage ( 0-5V ) I can't get correct value.(register type is the same).. I'm getting range error....RangeError [ERR_OUT_OF_RANGE]: The value of "value" is out of range. It must be >= -32768 and <= 32767. Received 52880 here must be output 40...
ModBUS holding registers can store any arbitrary binary data in a number of 16-bit registers. If you want to make sense of this data you need to know how what rule was used to represent that data in chunks of 16 bits. From your results, it is clear that the analog voltage was not stored as a float (with two swapped words). The sensor you mentioned even does not need to use all 16 bits as its precision is 12 bits. Perhaps you will need even to make some extra operation (after reading the register) to represent the data in a meaningful way.