![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
programme
Dependencies: EthernetInterface mbed-rtos mbed
main.cpp
- Committer:
- Starz
- Date:
- 2015-06-16
- Revision:
- 0:8a6f406dd109
File content as of revision 0:8a6f406dd109:
#include "mbed.h" #include "EthernetInterface.h" EthernetInterface eth; char ip[] = "192.168.1.10"; char masque[] = "255.255.255.0"; char passerelle[] = "192.168.1.255"; TCPSocketServer server; TCPSocketConnection client; int serveur_port = 2000; DigitalOut myled(LED1); Serial pc(USBTX, USBRX); int main() { bool client_connected = false; char buffer [100]; pc.printf("Hello, I'm happy my Serial port is ok.\n\r"); if (eth.init(ip,masque,passerelle)!=0) { pc.printf("ERREUR INIT ETHERNET\r\n"); return -1; } if (eth.connect()!=0) { pc.printf("ERREUR CONNECT\r\n"); return -1; } pc.printf("L addresse Ip est %s\r\n", eth.getIPAddress()); pc.printf("IP est OK, vous pouvez continuer\r\n"); if (server.bind (serveur_port)!=0) { pc.printf("Erreur Tcp bind\r\n"); return -1; } pc.printf("Succes Tcp Bind\r\n"); if (server.listen()!=0){ pc.printf("ERREUR Tcp Listen\r\n"); return -1; } pc.printf("Succes Tcp listen\r\n"); pc.printf("Connectez un client Tcp port 2000...\r\n"); client.set_blocking(false, 100); // Timeout after (100ms) while(1) { if (client_connected==false) { if (server.accept (client)!=0){ pc.printf("ERREUR connexion client\r\n"); // erreur return -1; } client_connected = true; pc.printf("Client Tcp connecte\r\n"); pc.printf("Adresse Ip client %s\r\n",client.get_address()); client.send("Hello\r\n",strlen("Hello\r\n")); } else { if ( client.is_connected() ) { if (pc.readable()) // interface virtuel serie recoit donnees { buffer[0] = pc.getc(); client.send(buffer,1); } int nbcaractere = client.receive(buffer,sizeof(buffer)); if (nbcaractere>0) // interface Ethernet recoit donnees { buffer[nbcaractere] = 0; pc.printf("%s",buffer); } } else { client_connected = false; client.close(); pc.printf("Client deconnecte\r\n"); pc.printf("Connectez un client Tcp port 2000...\r\n"); } } } }