Hi, recently I tried to read data from an Ultrasonic flow meter using Arduino, the protocol I used is MODBUS RTU. I found some codes that help me out, some data were Integers, and I read it right but there are some data that have decimals but with the code, it only shows the whole number only.
I will show the part of the code that needed.
#define Reg_FlowRate 0
SoftwareSerial swSer;
ModbusMaster node;
float Flowrate = 0.0;
float HexTofloat(uint32_t x) {
return (*(float *)&x);
}
void loop() {
Flowrate = Read_Meter_1(ID_meter, Reg_FlowRate);
Serial.print(Flowrate);
}
long Read_Meter_1(char addr, int REG)
{
long i = 0;
uint8_t j, result;
uint16_t data[2];
uint32_t value = 0;
node.begin(addr, swSer);
result = node.readHoldingRegisters(REG, 2);
delay(200);
if (result == node.ku8MBSuccess) {
for (j = 0; j < 2; j++) {
data[j] = node.getResponseBuffer(j);
}
value = data[1];
value = value << 16;
value = value | data[0];
i = HexTofloat(value);
//Serial.println("connect modbus ok. ");
return i;
} else {
Serial.print("connect modbus failed");
Serial.println(REG);
delay(1000);
return 0;
}
}
Result
-3.00