Simple code for comunication via TCP between the mbed and PC.
Dependencies: EthernetInterface SimpleSocket mbed-rtos mbed
Fork of SimpleSocketExamples by
main.cpp@1:016774025718, 2014-12-17 (annotated)
- Committer:
- numeral369
- Date:
- Wed Dec 17 16:06:00 2014 +0000
- Revision:
- 1:016774025718
- Parent:
- 0:6dc3cfd058c6
A simple code for comunication via TCP between the Mbed and a PC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
numeral369 | 1:016774025718 | 1 | #include "functions.h" |
numeral369 | 1:016774025718 | 2 | #include "SimpleSocket.h" |
numeral369 | 1:016774025718 | 3 | #include "EthernetInterface.h" |
numeral369 | 1:016774025718 | 4 | #include "mbed.h" |
numeral369 | 1:016774025718 | 5 | |
numeral369 | 1:016774025718 | 6 | DigitalOut led0(LED1); |
numeral369 | 1:016774025718 | 7 | DigitalOut led1(LED2); |
yamaguch | 0:6dc3cfd058c6 | 8 | |
numeral369 | 1:016774025718 | 9 | int main() |
numeral369 | 1:016774025718 | 10 | { |
numeral369 | 1:016774025718 | 11 | |
numeral369 | 1:016774025718 | 12 | EthernetInterface::init("192.169.1.201", "255.255.255.0", NULL); |
numeral369 | 1:016774025718 | 13 | EthernetInterface::connect(); |
numeral369 | 1:016774025718 | 14 | ServerSocket server(80); |
numeral369 | 1:016774025718 | 15 | led0.write(1); |
numeral369 | 1:016774025718 | 16 | |
numeral369 | 1:016774025718 | 17 | printf("server: %s:1234\n", EthernetInterface::getIPAddress()); |
yamaguch | 0:6dc3cfd058c6 | 18 | |
numeral369 | 1:016774025718 | 19 | while (1) { |
numeral369 | 1:016774025718 | 20 | ClientSocket socket = server.accept(); |
numeral369 | 1:016774025718 | 21 | |
numeral369 | 1:016774025718 | 22 | if (socket) { |
numeral369 | 1:016774025718 | 23 | while (socket) { |
numeral369 | 1:016774025718 | 24 | char buf[80]; |
numeral369 | 1:016774025718 | 25 | int len = socket.read(buf, sizeof(buf)); |
numeral369 | 1:016774025718 | 26 | if(buf[0]=='a') |
numeral369 | 1:016774025718 | 27 | led0=0; |
numeral369 | 1:016774025718 | 28 | led1=!led1; |
numeral369 | 1:016774025718 | 29 | wait(2); |
numeral369 | 1:016774025718 | 30 | //if (len > 0) |
numeral369 | 1:016774025718 | 31 | socket.write(12); |
numeral369 | 1:016774025718 | 32 | wait_ms(2); |
numeral369 | 1:016774025718 | 33 | } |
numeral369 | 1:016774025718 | 34 | socket.close(); |
numeral369 | 1:016774025718 | 35 | wait_ms(2); |
numeral369 | 1:016774025718 | 36 | } |
numeral369 | 1:016774025718 | 37 | wait(1.0); |
numeral369 | 1:016774025718 | 38 | } |
yamaguch | 0:6dc3cfd058c6 | 39 | } |