Convert float to byte

hi guys, i need a little help to convert the data i send via bluetooth to bytes, does anyone know how to do this? thank you <3

void loop() {

  float frequency = meter.getFrequency();

  if(frequency > 0){

  //String convert = String (frequency, 0);
  Serial1.print(frequency);
  Serial1.print("\n");
  
    if(Serial1.available()){
      Serial.write(Serial1.read());
    }
  }
  delay(2000);
}```

Please explain in detail.

Are you thinking of sending binary data over the serial link? What is on the receiving end?

yes, I want to send the data to an application that I am developing in flutter, there the data must be received from the Uint8List type. When I send the data as it is in the code above, it should arrive "AB" but it arrives "A" and then "B", sometimes it arrives correct. (Sorry for my terrible english).

_onDataReceived(Uint8List data)  {
.
.
.
  }

I don't know what "flutter" is.

You can't send naked binary data over a serial link without special handling on the sending and receiving ends of the connection.

You can send data bytes as ASCII integers or in hexadecimal representation without special handling.

What is connected to "Serial1" in the code you posted?

You can send the raw bytes of a particular type by using something like:

/*
 * Sketch: sketch_feb19b
 * Target: *
 *
 */

float valToSend = 3.14159;
byte bytesToSend[sizeof(float)];
char
    szChar[20];
    
void setup() 
{
    Serial.begin(115200);

    Serial.print( "Float value is: " );
    Serial.println( valToSend, 5 );

    Serial.print( "Bytes: " );
    memcpy( bytesToSend, (uint8_t *)&valToSend, sizeof( float ) );
    for( uint8_t i=0; i<sizeof( float ); i++ )
    {
        sprintf( szChar, "0x%02X ", bytesToSend[i] );
        Serial.print( szChar );
        
    }//if
        
    Serial.println();
    
}//setup

void loop() 
{        
}//loop

If you want to make purists' heads explode you can use a union too but might not always get the accurate result if you want to do so over different platforms.

a conventional way of sending fractional data as an integer is to multiply the data by 10, 100, assuming it still fits within the number of bytes used to represent the value

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.