SHARE
TWEET

Untitled

a guest May 26th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "EthernetNKCAGV.h"
  2. #include "utilityNKCAGV/EthernetUdp.h"
  3. #include <SPI.h>
  4.  
  5. #define LAN_RESET_PIN 47 //for LAN reset
  6. #define PACKET_MAX_SIZE 300
  7. #define port_agv 9600
  8. #define port_agv2 9601
  9.  unsigned int port_PC=0x5353;
  10. IPAddress ip_PC(192, 168, 11, 10);
  11. IPAddress ip_PC2(192, 168, 11, 15);
  12. IPAddress ip_AGV(192, 168, 11, 11);
  13. static byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0xB4, 0x61};
  14.  
  15. static char receive_buffer[300] = {0};
  16. static char ReplyBuffer[] = "acknowledged";
  17.  
  18. EthernetUDP Udp1;
  19. EthernetUDP Udp2;
  20.  
  21. void setup() {
  22.    Serial.begin(9600);
  23.   while (!Serial) {
  24.    
  25.   }
  26.  
  27.  pinMode(LAN_RESET_PIN, OUTPUT);
  28.  digitalWrite(LAN_RESET_PIN, 1);
  29.  
  30.  delay(5000);
  31.  
  32.  digitalWrite(LAN_RESET_PIN, 0);
  33.   delay(20);
  34.   digitalWrite(LAN_RESET_PIN, 1);
  35.   delay(500);
  36.  
  37.   Ethernet.begin(mac, ip_AGV);
  38.   Udp1.begin(port_agv);
  39.   Udp2.begin(port_agv2);
  40. }
  41.  
  42. void loop() {
  43.  
  44.   Udp1.beginPacket(ip_PC,port_PC);
  45.   Udp1.write("abcde", 5); //Udp1.write(char buffer,int buffer_of_length);
  46.   Udp1.endPacket();
  47.   Serial.println("UDP Packet Sent to PLC");
  48.  delay(1000);
  49.   //recieve
  50.   int packetSize = Udp1.parsePacket();
  51.   if(packetSize)
  52.   {
  53.     Serial.println("PLC detected");
  54.   if (packetSize) {
  55.     Serial.print("Received packet of size ");
  56.     Serial.println(packetSize);
  57.     Serial.print("From ");
  58.     IPAddress remote = Udp1.remoteIP();
  59.     for (int i=0; i < 4; i++) {
  60.       Serial.print(remote[i], DEC);
  61.       if (i < 3) {
  62.         Serial.print(".");
  63.       }
  64.     }
  65.     Serial.print(", port ");
  66.     Serial.println(Udp1.remotePort());
  67.     Udp1.read(receive_buffer, PACKET_MAX_SIZE);
  68.     Serial.println("Contents:");
  69.     Serial.println(receive_buffer);
  70.  
  71.  
  72.   }
  73.   }
  74.   else{
  75.     Serial.println("No PLC found so trying for Tablet");
  76.    Udp2.beginPacket(ip_PC2,port_PC);
  77.    Udp2.write("abcde", 5); //Udp1.write(char buffer,int buffer_of_length);
  78.    Udp2.endPacket();
  79.    Serial.println("UDP Packet Sent to Tablet");
  80.   int  packetSize = Udp2.parsePacket();
  81.   if (packetSize) {
  82.     Serial.print("Received packet of size ");
  83.     Serial.println(packetSize);
  84.     Serial.print("From ");
  85.     IPAddress remote = Udp2.remoteIP();
  86.     for (int i=0; i < 4; i++) {
  87.       Serial.print(remote[i], DEC);
  88.       if (i < 3) {
  89.         Serial.print(".");
  90.       }
  91.     }
  92.     Serial.print(", port ");
  93.     Serial.println(Udp2.remotePort());
  94.     Udp2.read(receive_buffer, PACKET_MAX_SIZE);
  95.     Serial.println("Contents:");
  96.     Serial.println(receive_buffer);
  97.   }
  98.   }
  99. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top