问题
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...
回答1:
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
回答2:
The correct solution is to use writeUInt16BE as below-
msg.payload = [0, 16833];
let pay = msg.payload;
const buf = Buffer.allocUnsafe(4);
buf.writeUInt16BE(pay[0],2);
buf.writeUInt16BE(pay[1],0);
msg.payload = buf.readFloatBE(0);
return msg;enter code here
来源:https://stackoverflow.com/questions/56257673/reading-32-bit-float-from-modbus-tcp-using-node-red