tcp_socket lab
Dependencies: mbed mbed-rtos EthernetInterface
main.cpp@9:87c9a4f4c0b2, 2019-12-10 (annotated)
- Committer:
- brunostgr
- Date:
- Tue Dec 10 16:20:32 2019 +0000
- Revision:
- 9:87c9a4f4c0b2
- Parent:
- 8:23b1fba109b0
tcp_socket
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brunostgr | 9:87c9a4f4c0b2 | 1 | /* |
brunostgr | 9:87c9a4f4c0b2 | 2 | * Le 12 novembre 2019 |
brunostgr | 9:87c9a4f4c0b2 | 3 | * Microcontroleurs et reseaux |
brunostgr | 9:87c9a4f4c0b2 | 4 | * |
brunostgr | 9:87c9a4f4c0b2 | 5 | * Bruno St-Georges & Benjamin Lapointe |
brunostgr | 9:87c9a4f4c0b2 | 6 | * LPC1768 |
brunostgr | 9:87c9a4f4c0b2 | 7 | * |
brunostgr | 9:87c9a4f4c0b2 | 8 | * TP3 - TCPServer |
brunostgr | 9:87c9a4f4c0b2 | 9 | * Ce programme sert a heberger un serveur TCP sur une carte mbed LPC1768 qui relie le TCPsocket client (script python) |
brunostgr | 9:87c9a4f4c0b2 | 10 | * aux deux PICs(noeuds lumiere et temperature) connectes sur le bus CAN. Un connecteur Mag-Jack est connecte sur les ports TD+/- et RD+/- |
brunostgr | 9:87c9a4f4c0b2 | 11 | * du LPC1768 pour permettre la communication Ethernet. |
brunostgr | 9:87c9a4f4c0b2 | 12 | * |
brunostgr | 9:87c9a4f4c0b2 | 13 | * Connexions externes: |
brunostgr | 9:87c9a4f4c0b2 | 14 | * CANBus (p9,p10) |
brunostgr | 9:87c9a4f4c0b2 | 15 | * Mag-Jack (Ethernet pins) |
brunostgr | 9:87c9a4f4c0b2 | 16 | * |
brunostgr | 9:87c9a4f4c0b2 | 17 | * Couleurs offertes pour le module de lumiere: |
brunostgr | 9:87c9a4f4c0b2 | 18 | * C: Cyan |
brunostgr | 9:87c9a4f4c0b2 | 19 | * L: Lime |
brunostgr | 9:87c9a4f4c0b2 | 20 | * M: Magenta |
brunostgr | 9:87c9a4f4c0b2 | 21 | * G: Gold |
brunostgr | 9:87c9a4f4c0b2 | 22 | * F: Forest |
brunostgr | 9:87c9a4f4c0b2 | 23 | * |
brunostgr | 9:87c9a4f4c0b2 | 24 | */ |
brunostgr | 9:87c9a4f4c0b2 | 25 | |
mbed_official | 0:38cbb854d85f | 26 | #include "mbed.h" |
emilmont | 1:5cebe0e38cd2 | 27 | #include "EthernetInterface.h" |
mbed_official | 8:23b1fba109b0 | 28 | |
brunostgr | 9:87c9a4f4c0b2 | 29 | #define ECHO_SERVER_PORT 69 |
brunostgr | 9:87c9a4f4c0b2 | 30 | |
brunostgr | 9:87c9a4f4c0b2 | 31 | // Fonction d'appel periodique |
brunostgr | 9:87c9a4f4c0b2 | 32 | Ticker ticker; |
brunostgr | 9:87c9a4f4c0b2 | 33 | |
brunostgr | 9:87c9a4f4c0b2 | 34 | // Bus CAN |
brunostgr | 9:87c9a4f4c0b2 | 35 | CAN canbus(p9, p10); |
brunostgr | 9:87c9a4f4c0b2 | 36 | |
brunostgr | 9:87c9a4f4c0b2 | 37 | // Variables |
brunostgr | 9:87c9a4f4c0b2 | 38 | CANMessage msg,msg2; |
brunostgr | 9:87c9a4f4c0b2 | 39 | bool flag_temp = 0, flag_lum = 0; |
brunostgr | 9:87c9a4f4c0b2 | 40 | char etat_temp[2] = {0,0}, etat_lum[2] = {0,0}; |
brunostgr | 9:87c9a4f4c0b2 | 41 | char lect_temp[2] = {0,0}, lect_lum[1] = {0}; |
brunostgr | 9:87c9a4f4c0b2 | 42 | |
brunostgr | 9:87c9a4f4c0b2 | 43 | |
brunostgr | 9:87c9a4f4c0b2 | 44 | // Port Serie |
brunostgr | 9:87c9a4f4c0b2 | 45 | Serial pc(USBTX, USBRX); |
brunostgr | 9:87c9a4f4c0b2 | 46 | |
brunostgr | 9:87c9a4f4c0b2 | 47 | // LED active a l'envoi de trames CAN |
brunostgr | 9:87c9a4f4c0b2 | 48 | DigitalOut LED(LED1); |
brunostgr | 9:87c9a4f4c0b2 | 49 | DigitalOut DEL(LED2); |
brunostgr | 9:87c9a4f4c0b2 | 50 | |
brunostgr | 9:87c9a4f4c0b2 | 51 | // Fonction d'envoi - appel aux 100ms |
brunostgr | 9:87c9a4f4c0b2 | 52 | void send() { |
brunostgr | 9:87c9a4f4c0b2 | 53 | |
brunostgr | 9:87c9a4f4c0b2 | 54 | if(flag_temp){ // Message CAN de temperature |
brunostgr | 9:87c9a4f4c0b2 | 55 | LED = not LED; |
brunostgr | 9:87c9a4f4c0b2 | 56 | flag_temp = 0; |
brunostgr | 9:87c9a4f4c0b2 | 57 | msg2.id = 0x300; |
brunostgr | 9:87c9a4f4c0b2 | 58 | msg2.data[0] = etat_temp[0]; |
brunostgr | 9:87c9a4f4c0b2 | 59 | msg2.data[1] = etat_temp[1]; |
brunostgr | 9:87c9a4f4c0b2 | 60 | canbus.write(msg2); |
brunostgr | 9:87c9a4f4c0b2 | 61 | } |
brunostgr | 9:87c9a4f4c0b2 | 62 | else if(flag_lum){ // Message CAN de lumiere |
brunostgr | 9:87c9a4f4c0b2 | 63 | DEL = not DEL; |
brunostgr | 9:87c9a4f4c0b2 | 64 | flag_lum = 0; |
brunostgr | 9:87c9a4f4c0b2 | 65 | msg2.id = 0x400; |
brunostgr | 9:87c9a4f4c0b2 | 66 | msg2.data[0] = etat_lum[0]; |
brunostgr | 9:87c9a4f4c0b2 | 67 | msg2.data[1] = etat_lum[1]; |
brunostgr | 9:87c9a4f4c0b2 | 68 | canbus.write(msg2); |
brunostgr | 9:87c9a4f4c0b2 | 69 | } |
brunostgr | 9:87c9a4f4c0b2 | 70 | } |
mbed_official | 8:23b1fba109b0 | 71 | |
emilmont | 1:5cebe0e38cd2 | 72 | int main (void) { |
emilmont | 1:5cebe0e38cd2 | 73 | EthernetInterface eth; |
brunostgr | 9:87c9a4f4c0b2 | 74 | eth.init(); // Use DHCP |
emilmont | 1:5cebe0e38cd2 | 75 | eth.connect(); |
mbedAustin | 7:a5ead1402704 | 76 | printf("\nServer IP Address is %s\n", eth.getIPAddress()); |
emilmont | 1:5cebe0e38cd2 | 77 | |
emilmont | 1:5cebe0e38cd2 | 78 | TCPSocketServer server; |
emilmont | 3:36fd3cfad85a | 79 | server.bind(ECHO_SERVER_PORT); |
emilmont | 3:36fd3cfad85a | 80 | server.listen(); |
emilmont | 1:5cebe0e38cd2 | 81 | |
brunostgr | 9:87c9a4f4c0b2 | 82 | canbus.frequency(125000); // Bus CAN a 125kHz |
brunostgr | 9:87c9a4f4c0b2 | 83 | ticker.attach(&send, 0.1); // Attache du ticker a la fonction periodique d'envoi |
brunostgr | 9:87c9a4f4c0b2 | 84 | |
emilmont | 1:5cebe0e38cd2 | 85 | while (true) { |
emilmont | 1:5cebe0e38cd2 | 86 | printf("\nWait for new connection...\n"); |
emilmont | 1:5cebe0e38cd2 | 87 | TCPSocketConnection client; |
emilmont | 1:5cebe0e38cd2 | 88 | server.accept(client); |
emilmont | 3:36fd3cfad85a | 89 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
emilmont | 1:5cebe0e38cd2 | 90 | |
emilmont | 1:5cebe0e38cd2 | 91 | printf("Connection from: %s\n", client.get_address()); |
emilmont | 1:5cebe0e38cd2 | 92 | char buffer[256]; |
emilmont | 1:5cebe0e38cd2 | 93 | while (true) { |
brunostgr | 9:87c9a4f4c0b2 | 94 | |
brunostgr | 9:87c9a4f4c0b2 | 95 | //-------------------------------------------- |
brunostgr | 9:87c9a4f4c0b2 | 96 | // Traitement de la trame recue sur le bus CAN |
brunostgr | 9:87c9a4f4c0b2 | 97 | //-------------------------------------------- |
brunostgr | 9:87c9a4f4c0b2 | 98 | if(canbus.read(msg)){ // Verifie si une trame a ete recue sur le bus CAN |
brunostgr | 9:87c9a4f4c0b2 | 99 | if(msg.id == 0x301){ |
brunostgr | 9:87c9a4f4c0b2 | 100 | lect_temp[0] = msg.data[0]; // Dizaines |
brunostgr | 9:87c9a4f4c0b2 | 101 | lect_temp[1] = msg.data[1]; // Unites |
brunostgr | 9:87c9a4f4c0b2 | 102 | } |
brunostgr | 9:87c9a4f4c0b2 | 103 | if(msg.id == 0x401){ |
brunostgr | 9:87c9a4f4c0b2 | 104 | lect_lum[0] = msg.data[0]; |
brunostgr | 9:87c9a4f4c0b2 | 105 | } |
brunostgr | 9:87c9a4f4c0b2 | 106 | } |
brunostgr | 9:87c9a4f4c0b2 | 107 | |
brunostgr | 9:87c9a4f4c0b2 | 108 | //-------------------------------------------- |
brunostgr | 9:87c9a4f4c0b2 | 109 | // Traitement de la trame recue du TCPclient |
brunostgr | 9:87c9a4f4c0b2 | 110 | //-------------------------------------------- |
emilmont | 3:36fd3cfad85a | 111 | int n = client.receive(buffer, sizeof(buffer)); |
brunostgr | 9:87c9a4f4c0b2 | 112 | printf("n value :'%s'\n",n); |
emilmont | 1:5cebe0e38cd2 | 113 | if (n <= 0) break; |
emilmont | 1:5cebe0e38cd2 | 114 | |
mbedAustin | 7:a5ead1402704 | 115 | // print received message to terminal |
mbedAustin | 7:a5ead1402704 | 116 | buffer[n] = '\0'; |
mbedAustin | 7:a5ead1402704 | 117 | printf("Received message from Client :'%s'\n",buffer); |
brunostgr | 9:87c9a4f4c0b2 | 118 | |
brunostgr | 9:87c9a4f4c0b2 | 119 | //------------------------------------------- |
brunostgr | 9:87c9a4f4c0b2 | 120 | // Traitement du statut |
brunostgr | 9:87c9a4f4c0b2 | 121 | //------------------------------------------- |
brunostgr | 9:87c9a4f4c0b2 | 122 | if(buffer[0] == 'S'){ // Statut |
brunostgr | 9:87c9a4f4c0b2 | 123 | if(buffer[1] == 'T'){ // Temp |
brunostgr | 9:87c9a4f4c0b2 | 124 | printf("Temp = %c",&lect_temp); |
brunostgr | 9:87c9a4f4c0b2 | 125 | client.send((char*)&lect_temp, 2); |
brunostgr | 9:87c9a4f4c0b2 | 126 | |
mbedAustin | 7:a5ead1402704 | 127 | } |
brunostgr | 9:87c9a4f4c0b2 | 128 | else if(buffer[1] == 'L'){ // Lum |
brunostgr | 9:87c9a4f4c0b2 | 129 | printf("Lum = %c", &lect_lum); |
brunostgr | 9:87c9a4f4c0b2 | 130 | client.send((char*)&lect_lum,1); |
brunostgr | 9:87c9a4f4c0b2 | 131 | |
brunostgr | 9:87c9a4f4c0b2 | 132 | } |
brunostgr | 9:87c9a4f4c0b2 | 133 | } |
mbedAustin | 7:a5ead1402704 | 134 | |
brunostgr | 9:87c9a4f4c0b2 | 135 | //-------------------------------------------- |
brunostgr | 9:87c9a4f4c0b2 | 136 | // Traitement de la commande |
brunostgr | 9:87c9a4f4c0b2 | 137 | //-------------------------------------------- |
brunostgr | 9:87c9a4f4c0b2 | 138 | if(buffer[0] == 'C'){ // Commande |
brunostgr | 9:87c9a4f4c0b2 | 139 | if(buffer[1] == 'L'){ // Lumiere |
brunostgr | 9:87c9a4f4c0b2 | 140 | etat_lum[0] = buffer[2]; // Etat demande |
brunostgr | 9:87c9a4f4c0b2 | 141 | etat_lum[1] = buffer[3]; // Couleur choisie |
brunostgr | 9:87c9a4f4c0b2 | 142 | flag_lum = 1; |
brunostgr | 9:87c9a4f4c0b2 | 143 | } |
brunostgr | 9:87c9a4f4c0b2 | 144 | if(buffer[1] == 'T'){ // Temperature |
brunostgr | 9:87c9a4f4c0b2 | 145 | etat_temp[0] = buffer[2]; // Temperature demandee (dizaines) |
brunostgr | 9:87c9a4f4c0b2 | 146 | etat_temp[1] = buffer[3]; // Temperature demandee (unites) |
brunostgr | 9:87c9a4f4c0b2 | 147 | flag_temp = 1; |
brunostgr | 9:87c9a4f4c0b2 | 148 | } |
brunostgr | 9:87c9a4f4c0b2 | 149 | } |
brunostgr | 9:87c9a4f4c0b2 | 150 | } |
emilmont | 1:5cebe0e38cd2 | 151 | client.close(); |
emilmont | 1:5cebe0e38cd2 | 152 | } |
emilmont | 1:5cebe0e38cd2 | 153 | } |
mbed_official | 8:23b1fba109b0 | 154 |