# Reading UDP packets

**URL:** <https://forum.arduino.cc/t/reading-udp-packets/589256>\
**Category:** Programming\
**Created:** [May 1, 2019, 7:36pm UTC](https://forum.arduino.cc/t/reading-udp-packets/589256 "2019-05-01T19:36:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cs75](https://avatars.discourse-cdn.com/v4/letter/c/7ab992/32.png) [@cs75](https://forum.arduino.cc/u/cs75)\
**Post date:** [May 1, 2019, 7:36pm UTC](https://forum.arduino.cc/t/reading-udp-packets/589256/1 "2019-05-01T19:36:57Z")

</div>

I'm playing with the UDP Send Receive String example found here; [https://www.arduino.cc/en/Tutorial/UDPSendReceiveString](https://www.arduino.cc/en/Tutorial/UDPSendReceiveString)

With the code provided I can (obviously) successfully send and receive chars.

I'm trying to go a step further now and perform different tasks (starting with simply printing a different line to serial monitor) depending on what data is received in the UDP packet.

My arduino code looked like this;

```cpp
#include <Ethernet.h>

//DEFINITIONS-----------------------------------------

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //mac adress
IPAddress ip(2, 0, 0, 100); //ip address

unsigned int localPort = 8888; //udp port to listen for packets on

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

EthernetUDP Udp; //creates Udp instance

char f1 = 10; //char to compare packetBuffer to
char f2 = 24; //char to compare packetBuffer to

void setup() {
  
  Ethernet.init(10); //sets CS pin for ethernet shield

  Ethernet.begin(mac, ip); //initializes ethernet with mac and ip addresses

  Serial.begin(9600); //initializes serial connection. Waits for serial to be available (native usb only). 
  while (!Serial) {
    ;
  }

  //Checks for presence of Ethernet shield. Halts if no ethernet hardware present. 
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
    while (true) {
      delay(1);
    }
  }
  //Checks for presence of etherner link.Halts if no link present. 
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }
  Udp.begin(localPort); //begins udp on port specified above. 
}

void loop() {
  
  int packetSize = Udp.parsePacket(); 
  if (packetSize) {                    //if data available, prints following;

    Serial.println("----------------------------------------------");
    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());

    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.print("packetBuffer = ");
    Serial.println(packetBuffer);
    if (packetBuffer == f1) {
      Serial.println("Contents:");
      Serial.println("10 received via UDP!");
    } else if (packetBuffer == f2) {
      Serial.println("Contents");
      Serial.println("24 received via UDP!");
    } else { 
      Serial.println("SOMETHING WENT WRONG");
    }
    Serial.println("----------------------------------------------");

    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write("acknowledged", 13);
    Udp.endPacket();
  }
  
}

```

And my Processing 3 code to send UDP looked like this;

```cpp
import hypermedia.net.*;

UDP udp; //define the UDP object

void setup() {

udp = new UDP (this, 6000); //create new datagram connection on port 6000
udp.log(true); //print out the connection activity (optional)
udp.listen(true); //wait for incoming message
}

void draw() {
}

void keyPressed() {
  int buffer = 24;
  String ip = "2.0.0.100"; //remote IP address to send string to
  int port = 8888; //destination port
  
  udp.send("24", ip, port); //the message to send
}

void receive( byte[] data ) { //default handler
//void receive( byte[] data, String ip, int port ) { // extended handler
  for(int i=0; i < data.length; i++)
  print(char(data[i]));
  println();
}

```

When sending either "10" or "24" to the arduino I always get my "Something went wrong message".

I assume this is something to do with the processing sketch sending the 10/24 as chars encoded in ascii, but I'm not sure how to account for that in the arduino code. When I try to change the packetBuffer from char to byte I get an error saying

"no matching function for call to 'println(byte [24])' "

Any help would be much appreciated! Thank you

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [May 2, 2019, 4:22am UTC](https://forum.arduino.cc/t/reading-udp-packets/589256/2 "2019-05-02T04:22:41Z")

</div>

You are comparing the buffer address to your incoming bytes, You should be comparing the contents of your buffer (specifically, the first byte)

```cpp
    if (packetBuffer[0] == f1) {
      Serial.println("Contents:");
      Serial.println("10 received via UDP!");
    } else if (packetBuffer[0] == f2) {
      Serial.println("Contents");
      Serial.println("24 received via UDP!");
    }

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 2, 2019, 10:50am UTC](https://forum.arduino.cc/t/reading-udp-packets/589256/3 "2019-05-02T10:50:08Z")

</div>

```cpp
char f1 = 10; //char to compare packetBuffer to
char f2 = 24; //char to compare packetBuffer to

```

You are NOT sending the char 10 or the char 24. You are sending the string "10" or the string "24".

packetBuffer isn't a string, so you can't use strcmp() to see if you got "10" or "24".

You could easily make it a string, so you could. Research what a string, in C, is, and you'll discover that it is a NULL terminated array of chars. Add a NULL to the char array, and the array will become a string.

Now, strcmp() doesn't expect an array of bytes, but you can use a cast, to pretend that the byte array is actually a char array.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:48pm UTC](https://forum.arduino.cc/t/reading-udp-packets/589256/4 "2021-05-05T23:48:05Z")

</div>

Closed on May 5, 2021, 11:48pm UTC


