integer bits to floating number conversion problem

Hi fellas

I have got troublesome problem during writing some code on arduino. I am trying to send serial floating number to Arduino Mega from Simulink Instrumental Control Toolbox(To instrument block) and process this floting number inside Arduino and get floating number result, and after that send back the result as floating number back to Simulink environment through Query instrument(Serial receive)
In order to do that, I set sending and receiving parts parameters as: format binary and precision 32 bits float with 9600 baudrate. On Arduino I intend to read four bytes which yields to 32 bits inside a for loop and convert them to integer bits as depicted below.

byte inData[4];

void setup()
{
  Serial.begin(9600);
}

void loop()
{
 
  if (Serial.available()) {
    for(int i=0; i<4; i++){
    inData[i] = Serial.read();
    }  
}
    int intbit =0;  
    intbit = (inData[3] << 24) | ((inData[2] & 0xff) << 16) | ((inData[1] & 0xff) << 8) | (inData[0] & 0xff);
    float num = Float.intBitsToFloat(intbit);
    
    ////////
    // Here some calculations
     result = 5*num;
    ///////
    
    serialFloatPrint(result);
    delay(100);
  }
 
  void serialFloatPrint(float f) {
  byte * b = (byte *) &f;
  Serial.write(b[0]);
  Serial.write(b[1]);
  Serial.write(b[2]);
  Serial.write(b[3]);

}

My question is there are various examples are already implemented on programming languages like java and processing that binery to floating number conversions are realized by

float f = Float.intBitsToFloat(number)

How can i realize the same conversion on Arduino IDE. I appreciate your contributions and efforts. Thanks in advance.

(inData[3] << 24)

An eight bit value shifted left 24 bits in a sixteen bit representation is going to be zero.
Some casts are needed.

You need to put the bytes in a byte array which can be cast to a float (ie its a union of float and byte[])

See, for instance, http://forum.arduino.cc/index.php?topic=199604.0

  if (Serial.available()) {
    for(int i=0; i<4; i++){
    inData[i] = Serial.read();
    }  
}

If there is at least one byte to read, read all 4 of them. I’d ask how that’s working for you, but I already know. Not well.

Hi PaulS,

Serial.read() could read 1 byte so i need to read 4 bytes to reunion the 32 bits number ? Isn't it true ?

It's true you need to read four bytes, but it best to wait until you have a byte to read, before reading it.

If you want to read all 4 bytes at once:

  if (Serial.available() >= 4)
  {
     for(int i=0; i<4; i++)
     {
        inData[i] = Serial.read();
     }
  }

(properly indented, with { where they belong)

Thank you very much i got the point on reading bytes issue. But i got one more question. Sorry for my lack of programming knowledge. How can I interprete binary data that i have previously converted from float data, to float data again on arduino. Suppose that ? have read 32 bit data. How could I convert to float again. :). I mean how to realize Float.intBitsToFloat code on Arduino or other algorithm which is identical. You know that this code is working on Processing, Java, etc.. languages. I really appreciate your efforts. Thanks alot.

I mean how to realize Float.intBitsToFloat code on Arduino

Feel free to find the source code for that method and do the same stuff on the Arduino.

The simplest method, as has already been pointed out, is to use a union.

union
{
   byte b[4];
   float f;
} rats;

if(Serial.available() >= 4)
{
   for(byte i=0; i<4; i++)
   {
       rats.b[i] = Serial.read();
   }
}
// rats.f is the float you just read.

Thank you very much PaulS and other friends who has contributed solving my problem. I have learned many from you.