![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
programme
Dependencies: EthernetInterface mbed-rtos mbed
main.cpp@0:8a6f406dd109, 2015-06-16 (annotated)
- Committer:
- Starz
- Date:
- Tue Jun 16 12:13:57 2015 +0000
- Revision:
- 0:8a6f406dd109
c pour val
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Starz | 0:8a6f406dd109 | 1 | #include "mbed.h" |
Starz | 0:8a6f406dd109 | 2 | #include "EthernetInterface.h" |
Starz | 0:8a6f406dd109 | 3 | |
Starz | 0:8a6f406dd109 | 4 | |
Starz | 0:8a6f406dd109 | 5 | |
Starz | 0:8a6f406dd109 | 6 | EthernetInterface eth; |
Starz | 0:8a6f406dd109 | 7 | char ip[] = "192.168.1.10"; |
Starz | 0:8a6f406dd109 | 8 | char masque[] = "255.255.255.0"; |
Starz | 0:8a6f406dd109 | 9 | char passerelle[] = "192.168.1.255"; |
Starz | 0:8a6f406dd109 | 10 | TCPSocketServer server; |
Starz | 0:8a6f406dd109 | 11 | TCPSocketConnection client; |
Starz | 0:8a6f406dd109 | 12 | int serveur_port = 2000; |
Starz | 0:8a6f406dd109 | 13 | DigitalOut myled(LED1); |
Starz | 0:8a6f406dd109 | 14 | Serial pc(USBTX, USBRX); |
Starz | 0:8a6f406dd109 | 15 | int main() { |
Starz | 0:8a6f406dd109 | 16 | bool client_connected = false; |
Starz | 0:8a6f406dd109 | 17 | char buffer [100]; |
Starz | 0:8a6f406dd109 | 18 | pc.printf("Hello, I'm happy my Serial port is ok.\n\r"); |
Starz | 0:8a6f406dd109 | 19 | if (eth.init(ip,masque,passerelle)!=0) { |
Starz | 0:8a6f406dd109 | 20 | pc.printf("ERREUR INIT ETHERNET\r\n"); |
Starz | 0:8a6f406dd109 | 21 | return -1; |
Starz | 0:8a6f406dd109 | 22 | } |
Starz | 0:8a6f406dd109 | 23 | if (eth.connect()!=0) { |
Starz | 0:8a6f406dd109 | 24 | pc.printf("ERREUR CONNECT\r\n"); |
Starz | 0:8a6f406dd109 | 25 | return -1; |
Starz | 0:8a6f406dd109 | 26 | } |
Starz | 0:8a6f406dd109 | 27 | pc.printf("L addresse Ip est %s\r\n", eth.getIPAddress()); |
Starz | 0:8a6f406dd109 | 28 | pc.printf("IP est OK, vous pouvez continuer\r\n"); |
Starz | 0:8a6f406dd109 | 29 | if (server.bind (serveur_port)!=0) { |
Starz | 0:8a6f406dd109 | 30 | pc.printf("Erreur Tcp bind\r\n"); |
Starz | 0:8a6f406dd109 | 31 | return -1; |
Starz | 0:8a6f406dd109 | 32 | } |
Starz | 0:8a6f406dd109 | 33 | pc.printf("Succes Tcp Bind\r\n"); |
Starz | 0:8a6f406dd109 | 34 | if (server.listen()!=0){ |
Starz | 0:8a6f406dd109 | 35 | pc.printf("ERREUR Tcp Listen\r\n"); |
Starz | 0:8a6f406dd109 | 36 | return -1; |
Starz | 0:8a6f406dd109 | 37 | } |
Starz | 0:8a6f406dd109 | 38 | pc.printf("Succes Tcp listen\r\n"); |
Starz | 0:8a6f406dd109 | 39 | pc.printf("Connectez un client Tcp port 2000...\r\n"); |
Starz | 0:8a6f406dd109 | 40 | client.set_blocking(false, 100); // Timeout after (100ms) |
Starz | 0:8a6f406dd109 | 41 | while(1) { |
Starz | 0:8a6f406dd109 | 42 | if (client_connected==false) |
Starz | 0:8a6f406dd109 | 43 | { |
Starz | 0:8a6f406dd109 | 44 | if (server.accept (client)!=0){ |
Starz | 0:8a6f406dd109 | 45 | pc.printf("ERREUR connexion client\r\n"); // erreur |
Starz | 0:8a6f406dd109 | 46 | return -1; |
Starz | 0:8a6f406dd109 | 47 | } |
Starz | 0:8a6f406dd109 | 48 | client_connected = true; |
Starz | 0:8a6f406dd109 | 49 | pc.printf("Client Tcp connecte\r\n"); |
Starz | 0:8a6f406dd109 | 50 | pc.printf("Adresse Ip client %s\r\n",client.get_address()); |
Starz | 0:8a6f406dd109 | 51 | client.send("Hello\r\n",strlen("Hello\r\n")); |
Starz | 0:8a6f406dd109 | 52 | } |
Starz | 0:8a6f406dd109 | 53 | else |
Starz | 0:8a6f406dd109 | 54 | { |
Starz | 0:8a6f406dd109 | 55 | if ( client.is_connected() ) |
Starz | 0:8a6f406dd109 | 56 | { |
Starz | 0:8a6f406dd109 | 57 | if (pc.readable()) // interface virtuel serie recoit donnees |
Starz | 0:8a6f406dd109 | 58 | { |
Starz | 0:8a6f406dd109 | 59 | buffer[0] = pc.getc(); |
Starz | 0:8a6f406dd109 | 60 | client.send(buffer,1); |
Starz | 0:8a6f406dd109 | 61 | } |
Starz | 0:8a6f406dd109 | 62 | int nbcaractere = client.receive(buffer,sizeof(buffer)); |
Starz | 0:8a6f406dd109 | 63 | if (nbcaractere>0) // interface Ethernet recoit donnees |
Starz | 0:8a6f406dd109 | 64 | { |
Starz | 0:8a6f406dd109 | 65 | buffer[nbcaractere] = 0; |
Starz | 0:8a6f406dd109 | 66 | pc.printf("%s",buffer); |
Starz | 0:8a6f406dd109 | 67 | } |
Starz | 0:8a6f406dd109 | 68 | } |
Starz | 0:8a6f406dd109 | 69 | else |
Starz | 0:8a6f406dd109 | 70 | { |
Starz | 0:8a6f406dd109 | 71 | client_connected = false; |
Starz | 0:8a6f406dd109 | 72 | client.close(); |
Starz | 0:8a6f406dd109 | 73 | pc.printf("Client deconnecte\r\n"); |
Starz | 0:8a6f406dd109 | 74 | pc.printf("Connectez un client Tcp port 2000...\r\n"); |
Starz | 0:8a6f406dd109 | 75 | } |
Starz | 0:8a6f406dd109 | 76 | } |
Starz | 0:8a6f406dd109 | 77 | } |
Starz | 0:8a6f406dd109 | 78 | } |