mbed TCP connectivity program

Files at this revision

API Documentation at this revision

Comitter:
davyu
Date:
Tue Nov 14 05:57:19 2017 +0000
Commit message:
Initial test program

Changed in this revision

README.md Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r a254c66d6be2 README.md
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Tue Nov 14 05:57:19 2017 +0000
@@ -0,0 +1,23 @@
+# Getting started sample with ethernet TCP on mbed OS
+
+
+### Import NuMaker-mbed-tcp from on-line IDE
+1. Please choose Nuvoton NuMaker-PFM-XXX as your target platform.
+2. Please press the left-up icon "New", then choose "NuMaker Ethernet TCP" from the template list.
+3. Your NuMaker-mbed-tcp program is existed.
+
+#### Now compile
+Please press compile icon.
+
+#### Burn Code & Execute
+1. Connect the board NuMaker-PFM-XXX with your PC by USB cable, then there will be one "mbed" disk.
+2. Copy the built binary file into "mbed" disk on you PC.
+3. Press device's reset button to execute, and you could get the below log by uart console.
+-------------------------------------------------------------------------------
+Start TCP test 
+100 full
+        Link Up
+TCP client IP Address is 10.28.10.14
+--------------------------------------------------------------------------
+   User could use tool or "ping" command to test RX/TX of TCP.
+   
\ No newline at end of file
diff -r 000000000000 -r a254c66d6be2 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Nov 14 05:57:19 2017 +0000
@@ -0,0 +1,47 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+
+// Network interface
+EthernetInterface net;
+
+// Socket demo
+int main() {
+    // Bring up the ethernet interface
+    printf("Ethernet socket example\n");
+    net.connect();
+
+    // Show the network address
+    const char *ip = net.get_ip_address();
+    const char *netmask = net.get_netmask();
+    const char *gateway = net.get_gateway();
+    printf("IP address: %s\n", ip ? ip : "None");
+    printf("Netmask: %s\n", netmask ? netmask : "None");
+    printf("Gateway: %s\n", gateway ? gateway : "None");
+
+    // Open a socket on the network interface, and create a TCP connection to mbed.org
+    TCPSocket socket;
+    socket.open(&net);
+    socket.connect("api.ipify.org", 80);
+    char *buffer = new char[256];
+
+    // Send an HTTP request
+    strcpy(buffer, "GET / HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n");
+    int scount = socket.send(buffer, strlen(buffer));
+    printf("sent %d [%.*s]\n", scount, strstr(buffer, "\r\n")-buffer, buffer);
+
+    // Recieve an HTTP response and print out the response line
+    int rcount = socket.recv(buffer, 256);
+    printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
+
+    // The api.ipify.org service also gives us the device's external IP address
+    const char *payload = strstr(buffer, "\r\n\r\n")+4;
+    printf("External IP address: %.*s\n", rcount-(payload-buffer), payload);
+
+    // Close the socket to return its memory and bring down the network interface
+    socket.close();
+    delete[] buffer;
+
+    // Bring down the ethernet interface
+    net.disconnect();
+    printf("Done\n");
+}
\ No newline at end of file
diff -r 000000000000 -r a254c66d6be2 mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Tue Nov 14 05:57:19 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#cc7556a92fb9320f4bebb190c6e1315af116c50c