Hi all,
I've written a sketch where I'm sending UDP packets via Multicast to two different ports. To do so, I needed to instantiate to EthernetUDP instances.
Here is a simplified demo sketch:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte message[3] = {0x90, 0x00, 0x00};
byte mac[] = {
0x06, 0xE9, 0xE5, 0x03, 0x22, 0x4F
};
IPAddress ip(192, 168, 0, 33); //local IP of Arduino/Teensy
IPAddress ipMulti(225, 0, 0, 37); // ipMidi Multicast address
unsigned int portMulti1 = 21928; // ipMidi Mutlicast port 1
unsigned int portMulti2 = 21929; // ipMidi Mutlicast port 2
byte packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet
// Two EthernetUDP instances to send and receive packets over two different UDP multicast ports
EthernetUDP Udp1;
EthernetUDP Udp2;
void setup() {
Ethernet.begin(mac, ip);
Udp1.beginMulticast(ipMulti, portMulti1);
Udp2.beginMulticast(ipMulti, portMulti2);
Serial.begin(9600);
}
void loop() {
sendMsgPort1();
delay(1000);
sendMsgPort2();
delay(1000);
}
void sendMsgPort1() {
Udp1.beginPacket(ipMulti, portMulti1);
Udp1.write(message[0]);
Udp1.write(message[1]);
Udp1.write(message[2]);
Udp1.endPacket();
Serial.println("send packet to port 1");
}
void sendMsgPort2() {
Udp2.beginPacket(ipMulti, portMulti2);
Udp2.write(message[0]);
Udp2.write(message[1]);
Udp2.write(message[2]);
Udp2.endPacket();
Serial.println("send packet to port 2");
}
As you see, I've written two identical functions with the exception of the appropriate UDP object.
My question: is there a way to put this into one function and select which object to use e.g. by using a function argument?
Thanks,
Stefan