esp32 modbus rtu hex to float convert

Viewed 257

I can send a request and get an response (modbus RTU frame) from the device (flow meter) but I cannot convert hex to float. Otherwise everything works fine and the way I want it. Does anyone know how to improve it in a simple way?

void loop() {
  const char msg_reguest[] = {0x01, 0x04, 0x00, 0x08, 0x00, 0x02, 0xf0, 0x09};
 delay(200);
 int i;
 int len=8; 
 
Serial.println("WYSŁANA RAMKA");
for(i = 0 ; i < len ; i++){
      Serial2.write(msg_reguest[i]); 
      Serial.print("[");
      Serial.print(i);
      Serial.print("]");
      Serial.print("="); 
      Serial.print("Ox");
      Serial.print(String(msg_reguest[i], HEX));      
      Serial.print(" ");
 }
 len = 0;
 Serial.println();
 Serial.println();

int a = 0;
 while(Serial2.available()) 
 {
 ByteArray[a] = Serial2.read();
 a++;
 }

 int b = 0;
 String registros;
    Serial.println("ODEBRANA RAMKA");
    for(b = 3 ; b < a-2 ; b++){      
      Serial.print("[");
      Serial.print(b);
      Serial.print("]");
      Serial.print("=");    
      Serial.print("Ox");
      registros =String(ByteArray[b], HEX);   
      Serial.print(registros);
      Serial.print(" "); 
      }

byte hexArray[] = "registros";
  float value;
  memcpy(&value, hexArray, sizeof(hexArray));
  Serial.println(value);   
}

the point is that I have an esp32, TTL rs485 and a flow meter with rs485. I download a frame each time with a different data value. I display the frame with the flow data in the serial monitor in the form of HEX. Esp32 are little Endian, so I want to change the order of numbers and write a function that will automatically convert to float. In the above code I try to do it but it doesn't work

0 Answers
Related