I am building a project where I transmit sensor data to its receiver via LoRa E5 module. After receiving, I plan it to be converted into an int/float value so that it can be stored to its corresponding variable. Upon using the function:
static void recv_prase(char *p_msg)
{
if (p_msg == NULL)
{
Serial.println("Received null");
return;
}
char *p_start = NULL;
char data[128]; // To hold the received bytes as characters
int bytes_len = 0;
p_start = strstr(p_msg, "RX"); // Find the "RX" keyword in the message
if (p_start && (1 == sscanf(p_start, "RX \"%127[^\"]\"", data)))
{
// Calculate the length of the hex string
bytes_len = strlen(data);
Serial.print("Data received as hex string: ");
Serial.println(data);
// Convert the hex string to an integer
int result = 0;
sscanf(data, "%X", &result);
// Print the received bytes (optional, to show the bytes)
Serial.print("Received Bytes: ");
for (int i = 0; i < bytes_len / 2; i++)
{
Serial.print((result >> (8 * (bytes_len / 2 - 1 - i))) & 0xFF, HEX);
Serial.print(" ");
}
Serial.println();
// Print the converted int value directly as the hex string interpreted
Serial.print("Converted Int: ");
Serial.println(result);
}
}
+TEST: LEN:3, RSSI:-48, SNR:6
+TEST: RX "101203"
Data received as hex string: 101203
Received Bytes: 10 12 3
Converted Int: 1053187
This is the output I get.
I wish to have my Converted Int the same with the string after RX.
I have some hypotheses as to why this problem occurred but I need advises from the people in the community who is/are more knowledgeable in the field and I'm not sure if I am right.