Versão do protegemed que calcula o tempo em ms da fuga, calcula o numero de onverflow (valores muito baixo) e underflow (valores muito altos). Além disso, calcula um valor médio a partir dos valores capturados e não apenas pela fft.

Dependencies:   EthernetInterface mbed-rtos mbed

Committer:
rebonatto
Date:
Mon Jul 21 00:58:34 2014 +0000
Revision:
2:86c3cb25577b
Parent:
0:c64e1194230b
Problemas com objeto para aquisi??o da rfid (serial)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rebonatto 0:c64e1194230b 1 /*
rebonatto 0:c64e1194230b 2 * CommTCP.cpp
rebonatto 0:c64e1194230b 3 *
rebonatto 0:c64e1194230b 4 * Created on: 07/07/2013
rebonatto 0:c64e1194230b 5 * Author: Rebonatto
rebonatto 0:c64e1194230b 6 */
rebonatto 0:c64e1194230b 7 #include "CommTCP.h"
rebonatto 0:c64e1194230b 8 #include "EventDetector.h"
rebonatto 0:c64e1194230b 9
rebonatto 0:c64e1194230b 10 void CommTCP::CommTCP_Thread(void const *arg)
rebonatto 0:c64e1194230b 11 {
rebonatto 0:c64e1194230b 12 char buffer[5];
rebonatto 0:c64e1194230b 13 int ret,i, r;
rebonatto 0:c64e1194230b 14 int cont = 0;
rebonatto 0:c64e1194230b 15 TCPSocketServer ServerSocket;
rebonatto 0:c64e1194230b 16
rebonatto 0:c64e1194230b 17 ServerSocket.bind(Settings::get_PortTCP());
rebonatto 0:c64e1194230b 18 ServerSocket.listen();
rebonatto 0:c64e1194230b 19 printf("TCP Thread starting...\r\n");
rebonatto 0:c64e1194230b 20 // printf("ServerSocket %s:%d\n", get_address(), get_port());
rebonatto 0:c64e1194230b 21
rebonatto 0:c64e1194230b 22 while(1){
rebonatto 0:c64e1194230b 23 TCPSocketConnection sock;
rebonatto 0:c64e1194230b 24 for(i=0; i < Settings::get_MaxTries(); i++){
rebonatto 0:c64e1194230b 25 printf("Aguarda Conexao\n");
rebonatto 0:c64e1194230b 26 r = ServerSocket.accept(sock);
rebonatto 0:c64e1194230b 27 if (r == 0) //Accept Ok
rebonatto 0:c64e1194230b 28 break;
rebonatto 0:c64e1194230b 29 else{
rebonatto 0:c64e1194230b 30 printf("Error in Acceppt\n");
rebonatto 0:c64e1194230b 31 wait_ms(Settings::get_DelayTry());
rebonatto 0:c64e1194230b 32 }
rebonatto 0:c64e1194230b 33 }
rebonatto 0:c64e1194230b 34 //sock.set_blocking(true, 1500);
rebonatto 0:c64e1194230b 35
rebonatto 0:c64e1194230b 36 //printf("Conected %d at %s\n", r, sock.get_address());
rebonatto 0:c64e1194230b 37 for(i=0; i < Settings::get_MaxTries(); i++){
rebonatto 0:c64e1194230b 38 r = sock.receive(buffer, sizeof(buffer));
rebonatto 0:c64e1194230b 39 if (r != -1) // Receive Ok
rebonatto 0:c64e1194230b 40 break;
rebonatto 0:c64e1194230b 41 else{
rebonatto 0:c64e1194230b 42 printf("Erro na recepcao do Socket TCP\n");
rebonatto 0:c64e1194230b 43 wait_ms(Settings::get_DelayTry());
rebonatto 0:c64e1194230b 44 }
rebonatto 0:c64e1194230b 45 buffer[ret] = '\0';
rebonatto 0:c64e1194230b 46 }
rebonatto 0:c64e1194230b 47 if (cont % 60 == 0)
rebonatto 0:c64e1194230b 48 printf("Recebida conexao %d msg TCP: %d chars: *%s*\n", cont++, ret, buffer);
rebonatto 0:c64e1194230b 49
rebonatto 0:c64e1194230b 50 //printf("Received %d chars:\n*%s*\n", ret, buffer);
rebonatto 0:c64e1194230b 51 sock.close();
rebonatto 0:c64e1194230b 52 SendAcom(0,0);
rebonatto 0:c64e1194230b 53
rebonatto 0:c64e1194230b 54 }
rebonatto 0:c64e1194230b 55 }
rebonatto 0:c64e1194230b 56
rebonatto 0:c64e1194230b 57 void CommTCP::RequestAcom(){
rebonatto 0:c64e1194230b 58 // Metodo para solicitar os acompanhamentos
rebonatto 0:c64e1194230b 59 char msg[] = "3;-1";
rebonatto 0:c64e1194230b 60 TCPSocketConnection s[NEIGHBORS];
rebonatto 0:c64e1194230b 61 int escritos, i;
rebonatto 0:c64e1194230b 62 //TCPSocketConnection s[NEIGHBORS];
rebonatto 0:c64e1194230b 63
rebonatto 0:c64e1194230b 64 /*
rebonatto 0:c64e1194230b 65 Formato das mensagens de requisicao
rebonatto 0:c64e1194230b 66 Tipo ; Tomada
rebonatto 0:c64e1194230b 67 Tipos: 1 Solicitacao de fase
rebonatto 0:c64e1194230b 68 2 Solicitacao de diferencial
rebonatto 0:c64e1194230b 69 3 Acompanhamento (manda fase e diferencial)
rebonatto 0:c64e1194230b 70 Tomada: Numero da tomada
rebonatto 0:c64e1194230b 71 -1 (zero) quando de todas as tomadas
rebonatto 0:c64e1194230b 72 */
rebonatto 0:c64e1194230b 73 /*Timer t;
rebonatto 0:c64e1194230b 74 //t.start();
rebonatto 0:c64e1194230b 75
rebonatto 0:c64e1194230b 76 for(i=0;i<Settings::get_NumNeighbors();i++){
rebonatto 0:c64e1194230b 77 s[i].connect(Settings::get_Neighbor(i), Settings::get_PortTCP());
rebonatto 0:c64e1194230b 78 }
rebonatto 0:c64e1194230b 79 //t.stop();
rebonatto 0:c64e1194230b 80 //printf("The time taken in connection was %d useconds\n", t.read_us());
rebonatto 0:c64e1194230b 81 */
rebonatto 0:c64e1194230b 82 for(i=0; i< Settings::get_NumNeighbors(); i++){
rebonatto 0:c64e1194230b 83 //talvez verificar se o socket est conectado. Se no estiver, destruir objeto e conectar
rebonatto 0:c64e1194230b 84 TCPSocketConnection sock;
rebonatto 0:c64e1194230b 85 sock.connect(Settings::get_Neighbor(i), Settings::get_PortTCP());
rebonatto 0:c64e1194230b 86 //escritos = Settings::get_Socket(i).send_all(msg, sizeof(msg)-1);
rebonatto 0:c64e1194230b 87 //printf("Socket %d\n",s[i].is_connected());
rebonatto 0:c64e1194230b 88 //Timer t1;
rebonatto 0:c64e1194230b 89 //t1.start();
rebonatto 0:c64e1194230b 90 escritos = sock.send_all(msg, strlen(msg));
rebonatto 0:c64e1194230b 91 wait_ms(100);
rebonatto 0:c64e1194230b 92 if(escritos != strlen(msg)){
rebonatto 0:c64e1194230b 93 printf("Erro ao enviar mensagem para vizinho\n");
rebonatto 0:c64e1194230b 94 break;
rebonatto 0:c64e1194230b 95 }
rebonatto 0:c64e1194230b 96 //t1.stop();
rebonatto 0:c64e1194230b 97 //printf("The time taken in send was %d useconds\n", t1.read_us());
rebonatto 0:c64e1194230b 98
rebonatto 0:c64e1194230b 99 sock.close();
rebonatto 0:c64e1194230b 100
rebonatto 0:c64e1194230b 101 }
rebonatto 0:c64e1194230b 102 }
rebonatto 0:c64e1194230b 103
rebonatto 0:c64e1194230b 104 void CommTCP::SendAcom(int tipo,int tomada){
rebonatto 0:c64e1194230b 105 // Aqui chama a funço para enviar um acompanhamento
rebonatto 0:c64e1194230b 106 for(int i=0;i<Settings::get_MaxChannels();i++)
rebonatto 0:c64e1194230b 107 {
rebonatto 0:c64e1194230b 108 EventDetector::get_Detector(i).ExternalTrigger();
rebonatto 0:c64e1194230b 109 }
rebonatto 0:c64e1194230b 110 }
rebonatto 0:c64e1194230b 111