Simple code for comunication via TCP between the mbed and PC.

Dependencies:   EthernetInterface SimpleSocket mbed-rtos mbed

Fork of SimpleSocketExamples by Hiroshi Yamaguchi

Files at this revision

API Documentation at this revision

Comitter:
numeral369
Date:
Wed Dec 17 16:06:00 2014 +0000
Parent:
0:6dc3cfd058c6
Commit message:
A simple code for comunication via TCP between the Mbed and a PC

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 6dc3cfd058c6 -r 016774025718 main.cpp
--- a/main.cpp	Mon Feb 04 09:29:18 2013 +0000
+++ b/main.cpp	Wed Dec 17 16:06:00 2014 +0000
@@ -1,13 +1,39 @@
-#include "functions.h"
+ #include "functions.h"
+#include "SimpleSocket.h"
+#include "EthernetInterface.h"
+#include "mbed.h"
+
+DigitalOut led0(LED1);
+DigitalOut led1(LED2);
 
-int main() {
-    for (int i = 0; functions[i].name; i++)
-        printf("[%d] %s\n", i, functions[i]);
+int main()
+{
+
+    EthernetInterface::init("192.169.1.201", "255.255.255.0", NULL);
+    EthernetInterface::connect();
+    ServerSocket server(80);
+    led0.write(1);
+
+    printf("server: %s:1234\n", EthernetInterface::getIPAddress());
 
-    printf("Select => ");
-    int n;
-    scanf("%d", &n);
-    printf("%d was selected, starting %s\n", n,  functions[n].name);
-    
-    functions[n].func();
+    while (1) {
+        ClientSocket socket = server.accept();
+
+        if (socket) {
+            while (socket) {
+                char buf[80];
+                int len = socket.read(buf, sizeof(buf));
+                if(buf[0]=='a')
+                led0=0;
+                led1=!led1;
+                wait(2);
+                //if (len > 0)
+                    socket.write(12);
+                    wait_ms(2);
+            }
+            socket.close();
+            wait_ms(2);
+        }
+        wait(1.0);
+    }
 }
\ No newline at end of file