Go Down

Topic: Important delay sending via Ethernet Udp some processed datas from serial port  (Read 151 times) previous topic - next topic

Joris75

Hello everyone,

I'm using an RFD 868x modem connected to the RX2 port of my Arduino Mega. The baud rates for all the chain is 57600.

This modem receive datas with the following format:

# 145 167 19 19.78 189 0 9 /     (data presented here are choosen randomly, except "#" and "/" which start and end characters of the message received)

The reception process of these message is done by the arduino in the void serialEvent2()
Once the message is re-build in shall be sent to my PC via Ethernet. That's why we can find Udp.begin(); Udp.write() and Udp.end() in the void serialEvent2().

---------------------------------------------------------------------------------------------------------------------------------------------------------
My problem:

When I comment Udp lines, I can see the message printed each 200-300ms which is ok, for me, according the fact that a message is send to my Arduino Mega every 100ms.
But when I use Udp lines to send it to my PC via Ethernet, it takes about 2 or 3 seconds to print the message.

---------------------------------------------------------------------------------------------------------------------------------------------------------

I don't understand why there is this delay and I don't know what to do to resolve this issue.
I've tried a lot of things but I would appreciate some help.

My Arduino code is shown below:

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>


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

unsigned int localPort = 8888;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char packetBuffer_precedent[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged";       // a string to send back
int paquet_identique=0;
int compteur = 0;
EthernetUDP Udp;

String msg = "";
String msg_str="";
int blanc = 0;
char mot;
char mot_precedent;
char charBuf[27];
char msg_udp;
boolean reception = false;

//==========================================================================

void setup() {
 
Ethernet.begin(mac,ip);
Udp.begin(localPort);

Serial.begin(57600);
Serial1.begin(57600);
Serial2.begin(57600);


}


void loop() {
  int packetSize = Udp.parsePacket();
  IPAddress remote = Udp.remoteIP();
  if(packetSize)
  {
 
    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
      for (int i = 0; i <packetSize; i++)
       {   
          if (packetBuffer_precedent[i]==packetBuffer[i])
          {

            compteur=compteur+1;

          }
       }
      if (compteur!=packetSize)
        {
           Serial.println(packetBuffer);
           Serial1.write(packetBuffer);
           compteur=0;
        }

      else
      {
       // Serial.println("paquet identique");
        compteur=0;
      }
         

  }
  else
  { 
  }

        for (int i = 0; i <=packetSize; i++)
       {
          packetBuffer_precedent[i]=packetBuffer[i];
       }
       
  delay(1);

}


void serialEvent2() {


/* acquisition capteur*/
  if ((Serial2.available()>0))
  {

    byte incomingdata = Serial2.read();
    mot =(char)incomingdata;

    if (mot ==' '){
      blanc++;
    }


    if ((mot=='#')&&(reception==false))
    {
    reception=true;
    msg = (char)incomingdata;
   
    mot_precedent=mot;
    }

    if ((mot!='#')&&(reception==true)&&(mot!='/'))
    {
     msg += (char)incomingdata;

     msg_str=msg;  //juste pour pouvoir manipuler msg en string afin de faire msg.length plus bas
     
     mot_precedent=mot;
     
    }

    if ((mot=='/')&&(reception==true)&&(msg_str.length()>10))
    {
     
      msg += (char)incomingdata;
      msg += '\n'; 
      reception =false;
      blanc = 0;
      msg.toCharArray(charBuf,27);


      Udp.beginPacket(Udp.remoteIP(), 8889);
      Udp.write(charBuf);
      Udp.endPacket();
      Serial.println(msg); //for debug
   
      mot_precedent=mot;
      msg="";

    }

    if (blanc>8)
    {
      blanc=0;
      msg="";
      reception=false;
    }


}

}



Thanks in advance to anyone who wants to help me.

Joris

Robin2

My advice has two elements. Don't use serialEvent() and don't do the UPD stuff in the same function that is receive serial data - they are two different tasks.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

When the full message is received the variable newData will be set to true and you can test that to see if it is appropriate to call the function that sends your UDP data. When the UDP data has been sent you can set newData back to false so the next message is received.

Even if this does not solve the problem it will at least isolate where it is.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

mikb55

Check that you have a valid destination address before you attempt to send a UDP message.

Alternatively alter the retry count and timeout values as per post #26 in this thread
https://forum.arduino.cc/index.php?topic=140780.15

A network sniffer program like Wireshark is really useful in this type of situation as it allows you to see the packet destination addresses.

Joris75

Thanks a lot to both of you,

mikb55, your solution allows to solve the problem !
Robin2 your suggestions improve the velocity of the code !

I would never find it alone !

Thanks again,

Regards

Joris

Go Up