The online racing simulator
OutGauge - Arduino: Get Speed from a UDP packet
Hi all!
I'm trying to get the speed from an UDP packet on my arduino.
I show how I receive the float speed variable:

char speed[4];
speed[0] = packetBuffer[12];
speed[1] = packetBuffer[13];
speed[2] = packetBuffer[14];
speed[3] = packetBuffer[15];

And I obtain an output like it (Serial.println(speed):

z{Aý

I tried by many ways but I don't get the speed in numbers.

Somebody can let me know how I can get the float speed variable and show it in the serial port?


Thanks!!!
What language are you writing your application in?

C

char raw_speed[4];
raw_speed[0]= og_packet[12];
raw_speed[1]= og_packet[13];
raw_speed[2]= og_packet[14];
raw_speed[3]= og_packet[15];

float speed = *(float*)raw_speed;
Serial.println(speed);

C++

char raw_speed[4];
raw_speed[0]= og_packet[12];
raw_speed[1]= og_packet[13];
raw_speed[2]= og_packet[14];
raw_speed[3]= og_packet[15];

float speed = reinterpret_cast<float&>(raw_speed);
Serial.println(speed);

Also a solution for Java

byte[] rawSpeed = new byte[4];
rawSpeed[0] = og_packet[12];
rawSpeed[1] = og_packet[13];
rawSpeed[2] = og_packet[14];
rawSpeed[3] = og_packet[15];

int bits = 0;
for(int i = 3; i >= 0; i--) {
bits |= rawSpeed[i] << 8*i;
}

float speed = Float.intBitsToFloat(bits);
Serial.println(speed);

The Arduino language (based on Wiring) is implemented in C/C++, and therefore has some differences from the Processing language, which is based on Java.
The C approach should work fine then...
Would have thought floats should be avoided whenever possible on the Arduino, there is no floating point unit on AVR's and software based floating point calcs waste a hell of a lot of cycles.
Great!!! It works fine!! Thanks!!!!!
Quote from pancheitor :Hi all!
I'm trying to get the speed from an UDP packet on my arduino.
I show how I receive the float speed variable:

char speed[4];
speed[0] = packetBuffer[12];
speed[1] = packetBuffer[13];
speed[2] = packetBuffer[14];
speed[3] = packetBuffer[15];

And I obtain an output like it (Serial.println(speed):

z{Aý

I tried by many ways but I don't get the speed in numbers.

Somebody can let me know how I can get the float speed variable and show it in the serial port?


Thanks!!!

How do you receive UDP packets in Arduino?

Como lees los paquetes UDP en Arduino?

FGED GREDG RDFGDR GSFDG