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

Committer:
mahengjie
Date:
Fri Dec 15 20:54:59 2017 +0000
Revision:
13:1a7e0594c25a
Parent:
12:aee11a1d7f14
LINAC MO Monitor TCP  function test with a simple echo server. Hercules_3_2_8.exe can be used as the test client

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mahengjie 13:1a7e0594c25a 1 // TCP test of LINAC MO Monitor
mahengjie 13:1a7e0594c25a 2 // Hengjie Ma
mahengjie 13:1a7e0594c25a 3 // BNL, NSLS-II, RF
mahengjie 13:1a7e0594c25a 4 // Nov. 2, 2017
mahengjie 13:1a7e0594c25a 5 //
justinkim 8:f837e0d255e8 6 #include "mbed.h"
justinkim 8:f837e0d255e8 7 #include "EthernetInterface.h"
mahengjie 13:1a7e0594c25a 8 #include "TextLCD.h"
mahengjie 13:1a7e0594c25a 9 #include <stdio.h>
hjjeon 9:a63ff95c354b 10
mahengjie 13:1a7e0594c25a 11 // set TCP parameters
hjjeon 11:0da8667a9201 12 #define ECHO_SERVER_PORT 7
mahengjie 13:1a7e0594c25a 13 #define MAC_ADDRESS {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}
hjjeon 9:a63ff95c354b 14
mahengjie 13:1a7e0594c25a 15 DigitalOut myled(LED1); // on-board blue LED, an idiot light for TCP sanity check
mahengjie 13:1a7e0594c25a 16 char lcd_buffer[20];
kzl108 12:aee11a1d7f14 17
mahengjie 13:1a7e0594c25a 18 // set up 2004 LCD display panel, 4-bit production wiring
mahengjie 13:1a7e0594c25a 19 // RS, E, D4-D7, LCDType=LCD16x2, BL=NC, E2=NC, LCDTCtrl=US2066
mahengjie 13:1a7e0594c25a 20 TextLCD lcd(P9, P10, P5,P8, P7, P6, TextLCD::LCD20x4D, NC, NC, TextLCD::US2066_3V3);
mahengjie 13:1a7e0594c25a 21
mahengjie 13:1a7e0594c25a 22 int buffer_length = 256;
kzl108 12:aee11a1d7f14 23 int compare_strings(char [], char []);
kzl108 12:aee11a1d7f14 24
hjjeon 11:0da8667a9201 25 int main (void)
justinkim 8:f837e0d255e8 26 {
kzl108 12:aee11a1d7f14 27 myled = 1; // LED OFF in WIZwiki-W7500
kzl108 12:aee11a1d7f14 28 int flag=1;
mahengjie 13:1a7e0594c25a 29 lcd.cls();
mahengjie 13:1a7e0594c25a 30 printf("Wait a second...\r\n");
mahengjie 13:1a7e0594c25a 31 lcd.printf("Wait a second...\r\n");
mahengjie 13:1a7e0594c25a 32 // TCP initialization
mahengjie 13:1a7e0594c25a 33 uint8_t mac_addr[6] = MAC_ADDRESS;
hjjeon 11:0da8667a9201 34 EthernetInterface eth;
mahengjie 13:1a7e0594c25a 35 eth.init(mac_addr); //Use DHCP
justinkim 8:f837e0d255e8 36 eth.connect();
mahengjie 13:1a7e0594c25a 37 // annouce DHCP set echo-sever IP address
mahengjie 13:1a7e0594c25a 38 printf("Server IP Address is %s\r\n\n", eth.getIPAddress());
mahengjie 13:1a7e0594c25a 39 lcd.locate(0,0);
mahengjie 13:1a7e0594c25a 40 lcd.printf("ServerIP:%s\r\n", eth.getIPAddress());
mahengjie 13:1a7e0594c25a 41 // command for LED turn-on/off
mahengjie 13:1a7e0594c25a 42 printf("Usage: \r\n Client command to turn on LED: LED_ON \r\n");
mahengjie 13:1a7e0594c25a 43 printf(" Client command to turn off LED: any other string. \r\n\n");
hjjeon 11:0da8667a9201 44
hjjeon 11:0da8667a9201 45 TCPSocketServer server;
hjjeon 11:0da8667a9201 46 server.bind(ECHO_SERVER_PORT);
mahengjie 13:1a7e0594c25a 47 server.listen();
mahengjie 13:1a7e0594c25a 48 while(true)
hjjeon 11:0da8667a9201 49 {
mahengjie 13:1a7e0594c25a 50 printf("Wait for new connection...\r\n");
mahengjie 13:1a7e0594c25a 51 lcd.locate(0,3);
mahengjie 13:1a7e0594c25a 52 lcd.printf("Wait for connect...");
hjjeon 11:0da8667a9201 53 TCPSocketConnection client;
hjjeon 11:0da8667a9201 54 server.accept(client);
mahengjie 13:1a7e0594c25a 55 client.set_blocking(false, 3000); // Timeout after (3)s
mahengjie 13:1a7e0594c25a 56 printf("Connect from: %s\r\n", client.get_address());
mahengjie 13:1a7e0594c25a 57 lcd.locate(0,3);
mahengjie 13:1a7e0594c25a 58 lcd.printf("Connect:%s", client.get_address());
mahengjie 13:1a7e0594c25a 59 char buffer[buffer_length];
hjjeon 11:0da8667a9201 60 while (true) {
hjjeon 11:0da8667a9201 61 int n = client.receive(buffer, sizeof(buffer));
hjjeon 11:0da8667a9201 62 if (n <= 0) break;
hjjeon 11:0da8667a9201 63 // print received message to terminal
hjjeon 11:0da8667a9201 64 buffer[n] = '\0';
hjjeon 11:0da8667a9201 65 printf("Received message from Client :'%s'\r\n",buffer);
mahengjie 13:1a7e0594c25a 66 lcd.locate(0,1);
mahengjie 13:1a7e0594c25a 67 memcpy(lcd_buffer, buffer, 20);
mahengjie 13:1a7e0594c25a 68 lcd.printf("Client msg:'%s'\r",lcd_buffer);
kzl108 12:aee11a1d7f14 69
kzl108 12:aee11a1d7f14 70 // LED control if received message matches pre-defined command
kzl108 12:aee11a1d7f14 71 /*
kzl108 12:aee11a1d7f14 72 if ((buffer[0] == 'L') & (buffer[1] == '\0'))
kzl108 12:aee11a1d7f14 73 myled = 0; // LED ON in WIZwiki-W7500
kzl108 12:aee11a1d7f14 74 else
kzl108 12:aee11a1d7f14 75 myled = 1;
kzl108 12:aee11a1d7f14 76 */
mahengjie 13:1a7e0594c25a 77
kzl108 12:aee11a1d7f14 78 // LED control if received message matches pre-defined command
kzl108 12:aee11a1d7f14 79 char command_buf[256] = {'L', 'E', 'D', '_', 'O', 'N', '\0'};
kzl108 12:aee11a1d7f14 80
mahengjie 13:1a7e0594c25a 81 // char string[256];
mahengjie 13:1a7e0594c25a 82 // strcpy (string, command_buf);
mahengjie 13:1a7e0594c25a 83 // printf("Is received command : %s ??? \r\n", string);
kzl108 12:aee11a1d7f14 84 flag = compare_strings(buffer, command_buf);
kzl108 12:aee11a1d7f14 85
kzl108 12:aee11a1d7f14 86 if (flag == 0) {
kzl108 12:aee11a1d7f14 87 myled = 0; // LED ON in WIZwiki-W7500
mahengjie 13:1a7e0594c25a 88 printf("Yes, command = LED_ON, LED is turned on!\r\n\n");
kzl108 12:aee11a1d7f14 89 }
kzl108 12:aee11a1d7f14 90 else {
kzl108 12:aee11a1d7f14 91 myled = 1;
mahengjie 13:1a7e0594c25a 92 printf("No, command does not match, LED is turned off!\r\n\n");
kzl108 12:aee11a1d7f14 93 }
kzl108 12:aee11a1d7f14 94 // LED blink one time
kzl108 12:aee11a1d7f14 95 //myled = 0; // LED ON in WIZwiki-W7500
kzl108 12:aee11a1d7f14 96 //wait(1.0);
kzl108 12:aee11a1d7f14 97 //myled = 1; // LED OFF in WIZwiki-W7500
hjjeon 11:0da8667a9201 98
hjjeon 11:0da8667a9201 99 // reverse the message
hjjeon 11:0da8667a9201 100 char temp;
hjjeon 11:0da8667a9201 101 for(int f = 0, l = n-1; f<l; f++,l--){
hjjeon 11:0da8667a9201 102 temp = buffer[f];
hjjeon 11:0da8667a9201 103 buffer[f] = buffer[l];
hjjeon 11:0da8667a9201 104 buffer[l] = temp;
hjjeon 11:0da8667a9201 105 }
hjjeon 11:0da8667a9201 106 // print reversed message to terminal
mahengjie 13:1a7e0594c25a 107 printf("Sending reversed message to Client: '%s'\r\n",buffer);
mahengjie 13:1a7e0594c25a 108 memcpy(lcd_buffer, buffer, 20);
mahengjie 13:1a7e0594c25a 109 lcd.locate(0,2);
mahengjie 13:1a7e0594c25a 110 lcd.printf("ServerEcho:'%s'",lcd_buffer);
mahengjie 13:1a7e0594c25a 111 // 123456789abcdef1234
hjjeon 11:0da8667a9201 112 // Echo received message back to client
hjjeon 11:0da8667a9201 113 client.send_all(buffer, n);
hjjeon 11:0da8667a9201 114 if (n <= 0) break;
mahengjie 13:1a7e0594c25a 115 }
hjjeon 11:0da8667a9201 116 client.close();
hjjeon 9:a63ff95c354b 117 }
justinkim 8:f837e0d255e8 118 }
justinkim 8:f837e0d255e8 119
kzl108 12:aee11a1d7f14 120 int compare_strings(char a[], char b[])
kzl108 12:aee11a1d7f14 121 {
kzl108 12:aee11a1d7f14 122 int c = 0;
kzl108 12:aee11a1d7f14 123
kzl108 12:aee11a1d7f14 124 while (a[c] == b[c]) {
kzl108 12:aee11a1d7f14 125 if (a[c] == '\0' || b[c] == '\0')
kzl108 12:aee11a1d7f14 126 break;
kzl108 12:aee11a1d7f14 127 c++;
kzl108 12:aee11a1d7f14 128 }
kzl108 12:aee11a1d7f14 129
kzl108 12:aee11a1d7f14 130 if (a[c] == '\0' && b[c] == '\0')
kzl108 12:aee11a1d7f14 131 return 0;
kzl108 12:aee11a1d7f14 132 else
kzl108 12:aee11a1d7f14 133 return -1;
kzl108 12:aee11a1d7f14 134 }