Hi, im doing a test of the UDP communication and i can not achieve that the arduino send datagrams with this protocol.
Im using a Arduino Due, this is the code i use (its a modification of the UDPSendReceiveString sketch):
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress IPArduino(169, 254, 240, 41);
IPAddress IPPc(169, 254, 223, 221);
unsigned int PortArduino = 4000;
unsigned int PortPc = 8000;
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "Hello"; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,IPArduino);
Udp.begin(PortArduino);
Serial.begin(9600);
delay(2000);
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
Udp.beginPacket(IPPc, PortPc);
Udp.write(ReplyBuffer);
int A = Udp.endPacket();
Serial.println(A);
}
delay(10);
}
I send a packet from Matlab and wait response but nothing come back.
I am showing the value that the endPacket returns and it is always a 0 (error), i have not problem with the beginPacket (returns 1).
I think that the IP directions are ok, but im not sure about de MAC address, my Ethernet shield didnt come with a sticker (but im not sure if it should), and i use several MAC address but none works.
I hope you can help, it is for a college proyect and i need it working.
Thanks! and sorry for my english...