Hi sir, I had broke down the task and worked on it.
- Program to read data from the Serial Port and this code is working perfectly:
void setup() {
// initialize both serial ports:
Serial.begin(9600);
}
void loop() {
// read from port 0, send to port 0:
if (Serial.available()) {
int inByte = Serial.read();
Serial.write(inByte);
}
}
- Send data Via Ethernet Port using UDP. I tried this below code to send the UDP data to the Hercules Software(PC). Below code is not working:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = {
0xA8, 0x61, 0x0A, 0xAE, 0xE0, 0x81
};
IPAddress ip(192, 168, 100, 48);
unsigned int localPort = 8888; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH Shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
// start the Ethernet
Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start UDP
Udp.begin(localPort);
}
void loop()
{
byte inChar = 0x55;
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(inChar);
Udp.endPacket();
}
In the above code, i am sending the Data =0x55 to the PC(Hercules) from arduino mega + ethernet shield 2 board. Kindly let us know is there any mistake i made in the code.
To cross check the Hardwares and the PC software is working. I had tested the read and write sample code. this code is working
/*
UDPSendReceive.pde:
This sketch receives UDP message strings, prints them to the serial port
and sends an "acknowledge" string back to the sender
A Processing sketch is included at the end of file that can be used to send
and received messages for testing with a computer.
created 21 Aug 2010
by Michael Margolis
This code is in the public domain.
*/
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet2.h>
#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xA8, 0x61, 0x0A, 0xAE, 0xE0, 0x81
};
IPAddress ip(192, 168, 100, 48);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // 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, ip);
Udp.begin(localPort);
Serial.begin(9600);
}
void loop() {
if (Ethernet.linkStatus() == Unknown) {
Serial.println("Link status unknown. Link status detection is only available with W5200 and W5500.");
}
else if (Ethernet.linkStatus() == LinkON) {
Serial.println("Link status: On");
}
else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Link status: Off");
}
// 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);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}
Kindly let us know what is the mistake i made in the Send data Via Ethernet Port using UDP code.