Sketch does not repeat

Hello everyone,

I have a Problem with my code.

My Goal is to receive a Ethernet message via UDP and send the message back via CAN.

It works fine but only with the first packet I send. When I send the same packet again there is no Response as you can see in the screenshot

#include "variant.h"
#include "due_can.h"
#include "Arduino.h"

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

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);

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

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


EthernetUDP Udp;

void setup() {
                                           
Can0.init(250000);
Serial.begin(9600);

Ethernet.begin(mac, ip);
Udp.begin(localPort);

}

void loop() {
  
    int packetSize = Udp.parsePacket();
    Udp.read(packetBuffer,packetSize);
  
   if (packetSize)
  {
    
    int Canframestosend= packetSize/8;
    int count =0;
    int Rest=packetSize%8;

    
    for(int j=0;j<=Canframestosend;j++){
    
    CAN_FRAME NewFrame; 
    NewFrame.id=0xFA;
    NewFrame.length=8;
    
    for (int i=0;i<8;i++){
       
       NewFrame.data.byte[i] = packetBuffer[count];
       count++;
       }
    if(packetSize<count){
      NewFrame.length=Rest;
      }
       Can0.sendFrame(NewFrame);
       
      }     

   }
   delay(10);
}

Maybe there is an endless Loop but I don´t know which and why.

Hope anybody can help me with that!

    int packetSize = Udp.parsePacket();
    Udp.read(packetBuffer,packetSize);

You want to parse a packet that you haven't read? Why? What idiot came up with those method names?

Are you getting another packet?

What is sending the UDP packets? What CAN shield do you have?

I wanted to parse and read the packet like it is done in the UDPSendReceiveString example.
Don´t know exactly what you mean by method names. I send the message via Vector Canoe and I don´t use a shield but only a Transceiver.

Don´t know exactly what you mean by method names.

parsePacket() and read() are method names, since they belong to a class.

I send the message via Vector Canoe

I have no idea what Vector Canoe is.

I don´t use a shield but only a Transceiver.

OK. I still don't have a clue about your hardware, or a way to find the library you are using, to see if there are pin conflicts that would explain your problem, since you are having a hard time doing it.

Are you failing to get a second packet, or are you failing to transmit that packet?

I don´t think that it´s a Hardware Problem because the first packet is received and sent back as it should.
When I send a second Packet the Rx LED blinks but there is no Response. So there must be an error in the code. It works again for one Packet when I unplug the Arduino and plug it.

You should only call 'read()' if 'packetSize' is larger than 0.
(That's how the example you refer to does it.)

void loop()
{
    // if there's data available, read a packet
    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);
        Serial.println("Contents:");
        Serial.println(packetBuffer);

        // send a reply to the IP address and port that sent us the packet we received
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write(ReplyBuffer);
        Udp.endPacket();
    }
    delay(10);
}

Okay my mistake was in Udp.read(packetBuffer,packetSize) when I put in UDP_TX_PACKET_MAX_SIZE instead of packetSize it works fine but the size of the packets I send is more limited. A quick Explanation for this behavior would be nice.

Thank you Paul and Steve for responding.

A quick Explanation for this behavior would be nice.

The second argument to read() is the size of the array, not the amount of data to read.