Wrong received message using UDP server- Solved

Dear All,

I'm trying to communicate between two systems using UDP server, the 1st system send a message in this form as example 0024008c0a00000050000000000000000000000000000000000000006400000090010000
the total length is 36 bytes, where byte 0 is 0 always, byte 1 is 36 always, byte 2 is 0 always, byte 4-7 is variable0 the type is INT32, byte 8-11 is variable1 the type is INT32,...., until 32-35 which is the last variable7.
in the message above the value for var1 is 10 , var2 = 80, var6 = 100 and var7 is 400.

but when i use this code to read the UDP packet i got different message .

The code

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
   

#define WIFI_SSID "****"
#define WIFI_PASS ""
#define UDP_PORT 9180

WiFiUDP UDP;
char packet[255];
char reply[] = "Packet received!";
signed long INT0;
signed long INT1;
signed long INT7;
signed long INT6;

void setup() {

  Serial.begin(115200);
  Serial.println();
   

  WiFi.begin(WIFI_SSID , WIFI_PASS);
   

  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID );
  Serial.print(WIFI_PASS);
  // Loop continuously while WiFi is not connected
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(100);
    Serial.print(".");
  }
   

  Serial.println();
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());

  // Begin listening to UDP port
  UDP.begin(UDP_PORT);
  Serial.print("Listening on UDP port ");
  Serial.println(UDP_PORT);
   
}
   
void loop() {

  int packetSize = UDP.parsePacket();
  if (packetSize) {
    Serial.print("Received packet! Size: ");
    Serial.println(packetSize); 
    int len = UDP.read(packet, 255);
   if (len > 0)
    {
      packet[len] = '\0';
    }

for (int i = 0; i < packetSize ; i = i + 1) {
 // Serial.print(i);

  Serial.print(packet[i],HEX);

Serial.println();
    // Send return packet
    UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
    UDP.write(reply);
    UDP.endPacket();
 
  }

I'm getting this message

Received packet! Size: 36
02408CA0005000000000000000000006400090100

I really need some help since i tried so many times to know what causing the issue.

Values in the printed message matches what is indicated in the header.
Your message is printed shorter because you are not printing leading zeros.

Edit your print loop to:

1 Like

In general, printing hex values on one line without separators is a bad idea.
For example, the line 00AA13- what is it?
0 0A A1 3 (0x00 0x0A 0xA1 0x03)
or
0 0 AA 13 (0x00 0x00 0xAA 0x13)
?

Thank you so much, i didn't think about it before, you made my day sir.
Can you give me a clue about trimming it to get bytes 4-7 only as one variable ?

Thank you

let's assume that bytes 4-7 is uint32_t variable:

uint32_t x;                                  // variable for storing value of 4-7 bytes
memcpy(&x,(uint8_t*)packet +4, sizeof(x));  //copy 4 bytes starting from packet[4] to variable x 
1 Like

Man, i don't know what to say, thank you so much for your help.

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