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. I´m using the Arduino Due with a Transceiver. The Programm which sends and reads the Messages is Vector Canoe
#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; //Divides the Ethernet packet by 8 to match with CAN packet
int count =0;
int Rest=packetSize%8; //Needed if the Ethernet packetsize isn´t a multiple of 8
for(int j=0;j<=Canframestosend;j++){
CAN_FRAME NewFrame; //CAN frames which are sent
NewFrame.id=0xFA; //ID of the frames
NewFrame.length=8; //Maximum of bytes a Can frame can have
for (int i=0;i<8;i++){
NewFrame.data.byte[i] = packetBuffer[count]; //Write the Ethernet info byte by byte on the CAN frames
count++;
}
if(packetSize<count){
NewFrame.length=Rest; //Shortens the last frame to avoid unnessecary zeros
}
Can0.sendFrame(NewFrame);
}
}
delay(10);
}
Maybe there is an endless Loop but I don´t know which and why.