Trouble Displaying Udp.read() Contents

Hello,

I am attempting to send data from Simulink to my Arduino via UDP. I have an Arduino UNO with an Ethernet Shield 2 on it. So far, I am having Simulink send a constant number (70 in my case) via UDP to the Arduino. Here is a link that shows my Simulink setup: IMAGE.

On my Arduino, I am currently running this code (I have LED’s in the loop to visually check that everything connects):

#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[] = {0xA8, 0x61, 0x0A, 0xAE, 0x84, 0xA1};
IPAddress IP_PC(192, 168, 1, 190);
const int NTP_PACKET_SIZE = 48;

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

// buffers for receiving and sending data
char packetBuffer[NTP_PACKET_SIZE];  // buffer to hold incoming packet,
String dataReq;

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

void setup() {

  pinMode(12,OUTPUT);
  pinMode(8,OUTPUT);

  Ethernet.begin(mac, IP_PC);
  Serial.begin(9600);

  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Udp.begin(localPort);
}

void loop() {
  digitalWrite(12,HIGH);
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    digitalWrite(8,HIGH);
    Serial.print("Received packet of size ");
    Serial.print(packetSize);
    Serial.print(" (max size ");
    Serial.print(NTP_PACKET_SIZE);
    Serial.println(")");
    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
    //memset(packetBuffer, 0, NTP_PACKET_SIZE);
    Udp.read(packetBuffer, NTP_PACKET_SIZE);
    //String dataReq(packetBuffer); //convert char array to string
    Serial.println("Contents:");
    Serial.println(packetBuffer);
    //Serial.println(dataReq);

  }
  delay(500);
}

In the serial monitor, I get this: IMAGE

It seems like the Simulink model is successfully sending the packet and the Arduino is receiving it, but, for some reason, it isn’t printing out the actual contents within packetBuffer. I tried converting the packetBuffer to a String and printing that (you will see in my commented code) and still had no luck.

I am pretty new to the way UDP works but if anyone has some experience or sees an obvious error please let me know. My end goal is to send an array of 6 values to the Arduino via UDP but I want to start simple with just one value then work my way up.

Thank you.

I would install Wireshark on your PC and check that the packets being sent by Simulink contain what you expect. It looks to me as though your Uno is not receiving the data you expect.

1 Like

Thanks for the reply. I actually did just that–I took a display block and realized that the byte pack was not doing what I expected. I had success by converting it to a uint8 before sending it and then manually arranging the bytes to send. Doing it like this worked like a charm!