udp.write stops

Hi everyone,

I have a question when i use udp.write (udp.print). any comments is welcome.

my setup is that Arduino uno r3 + Ethernet shield v2 + can bus shield, you may find the links

the issue is that i can receive CAN data correctly, but when i try to send CAN data out by Ethernet, after several seconds, it stops sending, (before stops, the server can receive the correct data).

the relative code is

  char buffer[128];

void loop()
{
    tCAN message;

 if (mcp2515_check_message())
  {
        
    if (mcp2515_get_message(&message))
    {
     
      sprintf(buffer, "ID: 0x%X  Data: %.2X %.2X %.2X \n\r",
              message.id, message.id[0], message.id[1], message.id[2]);


      UDP.beginPacket(IP_dest, remPort);
      UDP.write(buffer);
      UDP.endPacket();

   //    UDP.flush();
//       delay(10);
    }
}

thank you so much!

the relative code is

You are wrong, there. The RELEVANT code is ALL of it.

Hi PaulS, thank you very much for the comments. I attached all code here. sorry about that.

if I remove 3 lines under “Serial.println(“udp send to server”);” then Arduino can receive CAN data correctly

If I don’t remove them, the server can only receive CAN data for several seconds, then the loop keeps printing “11111”.

did I misuse the “sprintf” and “UDP.write” ?

#include <Canbus.h>
#include <mcp2515.h>
#include <SPI.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>

byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0xC9, 0xBB};// Arduino board specified.
IPAddress    IP_dest(10, 1, 10, 111);//
unsigned int localPort_Arduino = 5005;//
unsigned int   remPort = 5005;//
char buffer[128];
EthernetUDP UDP;

void setup()
{
  Serial.begin(9600);

  while (!(Canbus.init(CANSPEED_500))) {
    Serial.println("Error Initializing MCP2515, retying in 5 seconds ...");
    delay(5000);
  }
  Ethernet.begin(mac);
  UDP.begin(localPort_Arduino);
  Serial.println(Ethernet.localIP());

}

void loop()
{
  tCAN message;

  if (mcp2515_check_message())
  {

    Serial.println("11111");

    if (mcp2515_get_message(&message))
    {
      Serial.println("22222");

      sprintf(buffer, "ID: 0x%X  Data: %.2X %.2X %.2X", message.id, message.data[0], message.data[1], message.data[2]);

       Serial.println("udp send to server"); // remove 3 lines below then I could get CAN data correctly
      UDP.beginPacket(IP_dest, remPort);
      UDP.write(buffer);
      UDP.endPacket();
    }
  }
}

did I misuse the "sprintf" and "UDP.write" ?

Not that I can see, although the size of buffer is much larger than it needs to be.

I don't know what type message.data is, so I don't know that the %X format specifier is appropriate, but sprintf() should just write ? in the buffer if it doesn't know how to output the data using the format specifier.

I didn't look to see what udp.write() expects as input. I don't know how it handles an array as input, especially an array of chars. I don't know if there is an overload that expects a string.

Hi PaulS, thanks.

it is hard to understand that, even I changed the code like this, UDP.write(“hello world”,11), the issue is still there.

#include <Canbus.h>
#include <mcp2515.h>
#include <SPI.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>

byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0xC9, 0xBB};// Arduino board specified.
IPAddress    IP_dest(10, 1, 10, 111);//
unsigned int localPort_Arduino = 5005;//
unsigned int   remPort = 5005;//
char buffer[128];
EthernetUDP UDP;

void setup()
{
  Serial.begin(9600);

  while (!(Canbus.init(CANSPEED_500))) {
    Serial.println("Error Initializing MCP2515, retying in 5 seconds ...");
    delay(5000);
  }
  Ethernet.begin(mac);
  UDP.begin(localPort_Arduino);
  Serial.println(Ethernet.localIP());

}

void loop()
{
  tCAN message;

  if (mcp2515_check_message())
  {

    Serial.println("11111");

    if (mcp2515_get_message(&message))
    {
      Serial.println("22222");


       Serial.println("udp send to server"); // remove 3 lines below then I could get CAN data correctly
      UDP.beginPacket(IP_dest, remPort);
      UDP.write("hello world",11); // problem still happens
      UDP.endPacket();
    }
  }
}

update. the issue was because of the hardware connection between ethernet shield and CAN shield.