A simple echo server for LINAC MO Monitor TCP function test; can be run with hercules_3_2_8 as the test client

Dependencies:   TextLCD WIZnetInterface mbed

Fork of TCP_LED_Control-WIZwiki-W7500 by Kiyong Lee

Revision:
13:1a7e0594c25a
Parent:
12:aee11a1d7f14
--- a/main.cpp	Mon Jul 20 07:54:18 2015 +0000
+++ b/main.cpp	Fri Dec 15 20:54:59 2017 +0000
@@ -1,45 +1,71 @@
+// TCP test of LINAC MO Monitor
+// Hengjie Ma
+// BNL, NSLS-II, RF
+// Nov. 2, 2017
+//
 #include "mbed.h"
 #include "EthernetInterface.h"
+#include "TextLCD.h"
+#include <stdio.h>
 
+// set TCP parameters
 #define ECHO_SERVER_PORT   7
+#define MAC_ADDRESS        {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}
 
-DigitalOut myled(LED1);
+DigitalOut myled(LED1);  // on-board blue LED, an idiot light for TCP sanity check
+char lcd_buffer[20];
 
+// set up 2004 LCD display panel, 4-bit production wiring
+// RS, E, D4-D7, LCDType=LCD16x2, BL=NC, E2=NC, LCDTCtrl=US2066
+TextLCD lcd(P9, P10, P5,P8, P7, P6, TextLCD::LCD20x4D, NC, NC, TextLCD::US2066_3V3); 
+
+int buffer_length = 256;
 int compare_strings(char [], char []); 
 
 int main (void) 
 {
     myled = 1; // LED OFF in WIZwiki-W7500
     int flag=1;
-
-    printf("Wait a second...\r\n");
-    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x55, 0x51, 0x52}; 
+    lcd.cls();
+    printf("Wait a second...\r\n"); 
+    lcd.printf("Wait a second...\r\n"); 
+    // TCP initialization
+    uint8_t mac_addr[6] = MAC_ADDRESS;
     EthernetInterface eth;
-    eth.init(mac_addr); //Use DHCP
+    eth.init(mac_addr);       //Use DHCP
     eth.connect();
-    printf("Server IP Address is %s\r\n", eth.getIPAddress());
+    // annouce DHCP set echo-sever IP address
+    printf("Server IP Address is %s\r\n\n", eth.getIPAddress());
+    lcd.locate(0,0);
+    lcd.printf("ServerIP:%s\r\n", eth.getIPAddress());
+    // command for LED turn-on/off
+    printf("Usage: \r\n  Client command to turn on  LED: LED_ON \r\n");
+    printf("  Client command to turn off LED: any other string. \r\n\n");
     
     TCPSocketServer server;
     server.bind(ECHO_SERVER_PORT);
-    server.listen();
-    
-    while (true) 
+    server.listen();  
+    while(true)
     {
-        printf("Wait for new connection...\r\n");
+        printf("Wait for new connection...\r\n"); 
+        lcd.locate(0,3); 
+        lcd.printf("Wait for connect...");  
         TCPSocketConnection client;
         server.accept(client);
-        client.set_blocking(false, 15000); // Timeout after (1.5)s
-        
-        printf("Connection from: %s\r\n", client.get_address());
-        char buffer[256];
+        client.set_blocking(false, 3000); // Timeout after (3)s
+        printf("Connect from: %s\r\n", client.get_address());
+        lcd.locate(0,3);
+        lcd.printf("Connect:%s", client.get_address());
+        char buffer[buffer_length];
         while (true) {
             int n = client.receive(buffer, sizeof(buffer));
             if (n <= 0) break;
-            
             // print received message to terminal
             buffer[n] = '\0';
             printf("Received message from Client :'%s'\r\n",buffer);
-
+            lcd.locate(0,1);
+            memcpy(lcd_buffer, buffer, 20);
+            lcd.printf("Client msg:'%s'\r",lcd_buffer);
           
             // LED control if received message matches pre-defined command
             /*
@@ -48,26 +74,23 @@
             else
               myled = 1;
             */
-            
+                     
             // LED control if received message matches pre-defined command
             char command_buf[256] = {'L', 'E', 'D', '_', 'O', 'N', '\0'};
 
-            char string[256];
-            strcpy (string, command_buf);
-            printf("Received command : %s\n", string);
-
+            // char string[256];
+            // strcpy (string, command_buf);
+            // printf("Is received command : %s  ??? \r\n", string);
             flag = compare_strings(buffer, command_buf);
             
             if (flag == 0) {
               myled = 0; // LED ON in WIZwiki-W7500
-              printf("LED is turned on!\r\n");
+              printf("Yes, command = LED_ON, LED is turned on!\r\n\n");
             }
             else {
               myled = 1;
-              printf("LED is turned off!\r\n");
+              printf("No, command does not match, LED is turned off!\r\n\n");
             }
-
-
             // LED blink one time                        
             //myled = 0; // LED ON in WIZwiki-W7500
             //wait(1.0);
@@ -80,18 +103,18 @@
                 buffer[f] = buffer[l];
                 buffer[l] = temp;
                 }
-            
             // print reversed message to terminal
-            printf("Sending message to Client: '%s'\r\n",buffer);
-            
+            printf("Sending reversed message to Client: '%s'\r\n",buffer);
+            memcpy(lcd_buffer, buffer, 20);
+            lcd.locate(0,2);
+            lcd.printf("ServerEcho:'%s'",lcd_buffer);
+            //          123456789abcdef1234
             // Echo received message back to client
             client.send_all(buffer, n);
             if (n <= 0) break;
-        }
-        
+        }      
         client.close();
     }
-    
 }
 
 int compare_strings(char a[], char b[])