Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "EthernetNKCAGV.h"
- #include "utilityNKCAGV/EthernetUdp.h"
- #include <SPI.h>
- #define LAN_RESET_PIN 47 //for LAN reset
- #define PACKET_MAX_SIZE 300
- #define port_agv 9600
- #define port_agv2 9601
- unsigned int port_PC=0x5353;
- IPAddress ip_PC(192, 168, 11, 10);
- IPAddress ip_PC2(192, 168, 11, 15);
- IPAddress ip_AGV(192, 168, 11, 11);
- static byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0xB4, 0x61};
- static char receive_buffer[300] = {0};
- static char ReplyBuffer[] = "acknowledged";
- EthernetUDP Udp1;
- EthernetUDP Udp2;
- void setup() {
- Serial.begin(9600);
- while (!Serial) {
- }
- pinMode(LAN_RESET_PIN, OUTPUT);
- digitalWrite(LAN_RESET_PIN, 1);
- delay(5000);
- digitalWrite(LAN_RESET_PIN, 0);
- delay(20);
- digitalWrite(LAN_RESET_PIN, 1);
- delay(500);
- Ethernet.begin(mac, ip_AGV);
- Udp1.begin(port_agv);
- Udp2.begin(port_agv2);
- }
- void loop() {
- Udp1.beginPacket(ip_PC,port_PC);
- Udp1.write("abcde", 5); //Udp1.write(char buffer,int buffer_of_length);
- Udp1.endPacket();
- Serial.println("UDP Packet Sent to PLC");
- delay(1000);
- //recieve
- int packetSize = Udp1.parsePacket();
- if(packetSize)
- {
- Serial.println("PLC detected");
- if (packetSize) {
- Serial.print("Received packet of size ");
- Serial.println(packetSize);
- Serial.print("From ");
- IPAddress remote = Udp1.remoteIP();
- for (int i=0; i < 4; i++) {
- Serial.print(remote[i], DEC);
- if (i < 3) {
- Serial.print(".");
- }
- }
- Serial.print(", port ");
- Serial.println(Udp1.remotePort());
- Udp1.read(receive_buffer, PACKET_MAX_SIZE);
- Serial.println("Contents:");
- Serial.println(receive_buffer);
- }
- }
- else{
- Serial.println("No PLC found so trying for Tablet");
- Udp2.beginPacket(ip_PC2,port_PC);
- Udp2.write("abcde", 5); //Udp1.write(char buffer,int buffer_of_length);
- Udp2.endPacket();
- Serial.println("UDP Packet Sent to Tablet");
- int packetSize = Udp2.parsePacket();
- if (packetSize) {
- Serial.print("Received packet of size ");
- Serial.println(packetSize);
- Serial.print("From ");
- IPAddress remote = Udp2.remoteIP();
- for (int i=0; i < 4; i++) {
- Serial.print(remote[i], DEC);
- if (i < 3) {
- Serial.print(".");
- }
- }
- Serial.print(", port ");
- Serial.println(Udp2.remotePort());
- Udp2.read(receive_buffer, PACKET_MAX_SIZE);
- Serial.println("Contents:");
- Serial.println(receive_buffer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement