1

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.

Hope anybody can help me with that! enter image description here

0

I'd guess it's in the if(packetsize) logic -- if you get a packetsize of zero, you still try to read into a zero-size buffer and who knows what would happen. Move the read inside the if(){...}

See https://www.arduino.cc/en/Reference/EthernetUDPRead

Also, you want to limit the reading to the UDP_TX_PACKET_MAX_SIZE size of the buffer, not to the size of the packet.

Update: The UDP_TX_PACKET_MAX_SIZE is only used in the user code, and nowhere else within the Ethernet Library. You could use a larger buffer if you need to by defining and allocating a larger buffer and specifying its size in EthernetUDP.read() (see https://www.arduino.cc/en/Reference/EthernetUDPRead).

  • Thanks for your Response, the mistake was that i limited the buffer in PacketSize and not in UDP_TX_PACKET_MAX_SIZE. But what if my packets are bigger than UDP_TX_PACKET_MAX_SIZE? I wanted to be more flexible by limiting it to the packetSize i get from parsePacket. – user14943 Nov 20 '15 at 7:37
  • If the packets are bigger than your buffer, you still do not want to read them into the smaller buffer. If it does write outside its allocated space, it could damage other variables. – Dave X Nov 20 '15 at 8:06
  • Okay so is there an Limitation for my UDP_TX_PACKET_MAX_SIZE? I can change the value in the Code of the library and Wikipedia says a UDP packet can contain up to 65.535 Bytes – user14943 Nov 20 '15 at 8:26
  • Looks like you can: forum.arduino.cc/index.php?topic=103502.0 Still, move your read inside the if(){} so you don't attempt to read if there is nothing to read. – Dave X Nov 20 '15 at 14:28
  • Don't limit your receive buffer to the TX packet size, make it bigger than any expected received packet: BUFFERSIZE=2048; char packetBuffer[BUFFERSIZE]; ... Udp.read(packetBuffer,BUFFERSIZE); – Dave X Nov 20 '15 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.