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:
#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