Redirecting Serial and UDP

Hi all..

Consider this (it compiles) :

void showWelcome(Print &p)
{
    p.println();
   p.println("Hello");

}

// Then :

showWelcome (Serial) ;

showWelcome (Serial1) ;

showWelcome (client) ;

Can this also be done? Or whats the equivalent?

bool UDPPacketREceived (Udp &u){

if(u.available()==0) {return false;}
 packetBuffer[0]  = u.parsePacket();

...
..
}

if  (UDPPacketREceived  (udp_ethernet) )    {   .... }

Understood.

In other words Serial is a Stream, UDP is not.

So, how would it be possible to achieve the same result with UDP ie redirect a UDP packet to alternative hardware destinations (eg Udp_wifi , and UDP_ethernet) ? Without writing duplicate functions that is...

UDP is Stream. just print to a packet you want to send and read from a parsed packet.
printing to received packet doesn't make sense

What I am trying to do is to re-direct a packet to one of two alternative hardware modules (eg wifi and ethernet) without having to write a separate function for each one.

the base class is UDP.

Care to give an example how it can be used in this case?

I guess what you want is bool UDPPacketREceived (UDP &u){

bool UDPPacketREceived(UDP &u){


 packetBuffer[0]  = u.parsePacket();
 
 if (packetBuffer[0]) {

  IPAddress remoteIp = u.remoteIP();
 
  int len = u.read(packetBuffer, 255);    // read the packet into packetBufffer
  
   
  if (len > 0) { 
    packetBuffer[len] = 0;
     }
    
  if (packetBuffer[26] !=0xAA || packetBuffer[27] !=0xAA) return false;
  
 IncPacket[0] = packetBuffer[28];
  
 for (byte x=1; x<IncPacket[0]; x++)
   {
  IncPacket[x] = packetBuffer[x+28];
   }
  
  
  
  bool passed = Check_crc(IncPacket);
  
   
  digitalWrite(wifiLed,HIGH);
  return passed;
  }

  else return false;
  }

The above compiles fine.

But then the following doesn't

if (UDPPacketREceived(Udp_eth) == true) { }

I guess it is as always:
post the complete sketch. Only a deep analysis of a complete sketch will help.

From your snippet it is unclear how is Udp_eth declared?

Update:

Problem solved.
Just to close this issue in case someone else reads this in the future:
There was effectively incorrect instantiation of the UDP library (misplaced Ifdef statement) which however resulted in a misleading error message from the compiler.

The most helpful posting would be to post the complete working code.
The arduino-forum is a volunteer community project where the community should be able to participate on solutions.

best regards Stefan

The complete code is a total of 10 tabs on arduino IDE resulting in about 80kb of flash memory of the 1284p.
What I can do is provide a simple equivalent sketch demonstrating the above solution.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.