Hi, I am programming my Final degree Project using an ESP32.
In this application, I send the information from the laptop in a SLIP frame.
I read this frame, and save the information in Strings, in Hexadecimal.
I want to save and interpret these Bytes as a Float or double. (Usingfloat, I have an OVF whit Serial.println).
Finally I have:
String s1 = "3fa00000"; These four bytes in float are "1,25".
I want:
double d1 = 1,25; And I want to print in Serial port this variable.
I hope your response. Thanks and sorry for my English.
system
March 27, 2019, 10:16am
2
Finally I have:
String s1 = "3fa00000"; These four bytes in float are "1,25".
What "four bytes" are you talking about? There are no bytes anywhere. There is just an instance of the String class which wraps a collection of characters.
Post your code that collects this String. It is extremely likely that you should NOT be using a String AT ALL.
I have a byte Array like this:
byte datosLeidos[TAM_TRAMA];
for (i = 0; i < TAM_TRAMA; i++) {
datosLeidos[i] = client.read();
}
datosLeidos[0] = 0xc0; (header)
datosLeidos[1] = 0x3F;
datosLeidos[2] = 0xA0;
datosLeidos[3] = 0x0;
datosLeidos[4] = 0x0;
I parsed and concatenate it into String like "0x3fa00000", but i think it is not neccesary.
I only need to interpret those bytes like this:
Thanks for your answer.
system
March 27, 2019, 11:06am
4
I parsed and concatenate it into String like "0x3fa00000", but i think it is not neccesary.
It was a complete waste of time.
Look up what a union is, and see what happens when you union 4 byte array and a float. Assign values to the byte array, and read the float value.
When I use the union, I have an overfload using Serial.println().
system
March 27, 2019, 11:19am
6
pericoj:
When I use the union, I have an overfload using Serial.println().
You're using the wrong part of the union, possibly? (Assuming you meant "overload" and not "overflowed")
This is the code using an union:
union u_tag {
byte b[4];
float dato;
} u;
byte datosLeidos[TAM_TRAMA];
const char * ssid = "PERICO";
const char * password = "12345678";
// Puerto del servidor
WiFiServer server(80);
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
delay(50);
}
void loop() {
// put your main code here, to run repeatedly:
client = server.available(); // listen for incoming clients
if (client) {
Serial.println("New Client"); // Se ha conectado un cliente.
}
if (client && client.connected()) {
if (client.available()) {
//Recibimos datos del Labview.
while (client.available()) {
for (int i = 0; i < TAM_TRAMA; i++) {
datosLeidos[i] = client.read();
Serial.print("datosLeidos[i]: ");
Serial.println(datosLeidos[i],HEX);
delay(50);
}
if(datosLeidos[0] == 0xC0)
{
delay(1000);
Serial.println("");
Serial.print("Byte 1: ");
Serial.println(datosLeidos[1]);
Serial.print("Byte 2: ");
Serial.println(datosLeidos[2]);
Serial.print("Byte 3: ");
Serial.println(datosLeidos[3]);
Serial.print("Byte 4: ");
Serial.println(datosLeidos[4]);
u.b[0]=datosLeidos[1];
u.b[1]=datosLeidos[2];
u.b[2]=datosLeidos[3];
u.b[3]=datosLeidos[4];
}
Serial.print("Dato: ");
Serial.println(u.dato);
}
}
}
}
After that, dato values is 0.00
This is the exit:
datosLeidos[i]: C0
datosLeidos[i]: 3F
datosLeidos[i]: A0
datosLeidos[i]: 0
datosLeidos[i]: 0
datosLeidos[i]: 40
datosLeidos[i]: A4
datosLeidos[i]: 28
datosLeidos[i]: F6
datosLeidos[i]: 41
datosLeidos[i]: 32
datosLeidos[i]: 9D
datosLeidos[i]: E
datosLeidos[i]: C0
Byte 1: 63
Byte 2: 160
Byte 3: 0
Byte 4: 0
Dato: 0.00
Help please!!
I fix the problem. Thanks a lot.