Search code examples
arduinoudpethernet

Unintentional strange characters added to packets during udp communication in Arduino


Stackoverflow!

Unusual strange characters are added to packets during udp communication in Arduino.

Hi. Stack overflow. I encountered problems that I could not solve while using Udp with Arduino. I have looked up a lot of information to solve this problem. I also tested various cases. But I can not solve it.

The problem occurs from the 24th byte of the packet. The characters exceeding 23 characters are converted into strange characters and the subsequent sentences are not output. Is the packet transmission of udp communication limited to 24 bytes? If so, I am glad, but I want to know how to solve it. Please help me.

The test environment is as follows. Two Arduino are connected via a LAN cable. In addition, the following source code has been uploaded to Arduino. The serial monitor is as follows.

TX serial monitor

. . 17:46:31.521 -> Sending UDP message

17:46:32.516 -> Sending UDP message

17:46:33.514 -> Sending UDP message

17:46:34.519 -> Sending UDP message

17:46:35.515 -> Sending UDP message

17:46:36.510 -> Sending UDP message

RX serial monitor

17:38:51.664 -> 010010010101001010101001K;⸮ <----------problem

17:38:52.662 -> Received packet of size 31

17:38:52.662 -> From 192.168.1.251, port 5678

17:38:52.662 -> Contents:

17:38:52.662 -> 01001001010100101010100j⸮ <------- problem

17:38:53.663 -> Received packet of size 31

17:38:53.663 -> From 192.168.1.251, port 5678

17:38:53.663 -> Contents:

17:38:53.663 -> 010010010101001010101001;ے <------------problem

17:38:56.770 -> Received packet of size 31

---------Source Code-------------

///////////TX
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

#define MSG_INTERVAL 1000

// network parameters
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x0E, 0x05, 0x04};  // ethernet interface MAC address
IPAddress localIp(192, 168, 1, 251);    // local ip address
IPAddress destIp(192, 168, 1, 15);      // destination ip address
unsigned int port = 5678;               // destination port

// EthernetUDP to send and receive messages.
EthernetUDP Udp;

// message string
char message[] = "0100100101010010101010010101010";

// timing
//unsigned long previousLedMillis;
//unsigned long ledInterval;
unsigned long previousSendMillis;
unsigned long sendInterval;

// setup the arduino and shields
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // start ethernet and udp
  Ethernet.begin(mac,localIp);    // static ip version

  // open UDP port
  Udp.begin(port);

  // show the local ip address (useful for dhcp)
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());

  // initialize send interval
  sendInterval = MSG_INTERVAL;

}

// do tasks
void loop() {  
  // check if udp string has to be sent
  if(millis() - previousSendMillis >= sendInterval) {
    sendMessage();
  }
}

// send udp string
void sendMessage() {
  Serial.println("Sending UDP message");

  // store current millis
  previousSendMillis = millis();

  // send udp message
  Udp.beginPacket(destIp, port);
  Udp.write(message);
  Udp.endPacket();

}

/////RX
#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 15);
IPAddress remIp(92, 168, 1, 176);

unsigned int localPort = 5678;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

void setup() 
{
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  pinMode(5, OUTPUT);
  Serial.begin(115200);
  Serial.print("Local IP: ");
 Serial.println(Ethernet.localIP());
}

void loop() 
{  
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    //Udp.read(packetBuffer,1024);

    Serial.println("Contents:");
    Serial.println(packetBuffer);
    //for (int i=0;i<packetSize;i++){
    //  Serial.print(packetBuffer[i]);
    //}
    //Serial.print('\n');
}

}

Solution

  • yes you have right, some problem with length 24!!

    if you read inside the EthernetUDP.h file, you will see:

     #define UDP_TX_PACKET_MAX_SIZE 24
    

    so if you want use more characters, dont use this const, choose your personal size.

    change

    char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
    

    to

    char packetBuffer[50];
    

    or use another define:

    #define UDP_MAX_BUFFER  50 //for example
    
    char packetBuffer[UDP_MAX_BUFFER];