Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos mbed
CommTCP.cpp
00001 /* 00002 * CommTCP.cpp 00003 * 00004 * Created on: 07/07/2013 00005 * Author: Rebonatto 00006 */ 00007 #include "CommTCP.h" 00008 #include "EventDetector.h" 00009 00010 void CommTCP::CommTCP_Thread(void const *arg) 00011 { 00012 char buffer[5]; 00013 int ret,i, r; 00014 int cont = 0; 00015 TCPSocketServer ServerSocket; 00016 00017 ServerSocket.bind(Settings::get_PortTCP()); 00018 ServerSocket.listen(); 00019 printf("TCP Thread starting...\r\n"); 00020 // printf("ServerSocket %s:%d\n", get_address(), get_port()); 00021 00022 while(1){ 00023 TCPSocketConnection sock; 00024 for(i=0; i < Settings::get_MaxTries(); i++){ 00025 printf("Aguarda Conexao\n"); 00026 r = ServerSocket.accept(sock); 00027 if (r == 0) //Accept Ok 00028 break; 00029 else{ 00030 printf("Error in Acceppt\n"); 00031 wait_ms(Settings::get_DelayTry()); 00032 } 00033 } 00034 //sock.set_blocking(true, 1500); 00035 00036 //printf("Conected %d at %s\n", r, sock.get_address()); 00037 for(i=0; i < Settings::get_MaxTries(); i++){ 00038 r = sock.receive(buffer, sizeof(buffer)); 00039 if (r != -1) // Receive Ok 00040 break; 00041 else{ 00042 printf("Erro na recepcao do Socket TCP\n"); 00043 wait_ms(Settings::get_DelayTry()); 00044 } 00045 buffer[ret] = '\0'; 00046 } 00047 if (cont % 60 == 0) 00048 printf("Recebida conexao %d msg TCP: %d chars: *%s*\n", cont++, ret, buffer); 00049 00050 //printf("Received %d chars:\n*%s*\n", ret, buffer); 00051 sock.close(); 00052 SendAcom(0,0); 00053 00054 } 00055 } 00056 00057 void CommTCP::RequestAcom(){ 00058 // Metodo para solicitar os acompanhamentos 00059 char msg[] = "3;-1"; 00060 TCPSocketConnection s[NEIGHBORS]; 00061 int escritos, i; 00062 //TCPSocketConnection s[NEIGHBORS]; 00063 00064 /* 00065 Formato das mensagens de requisicao 00066 Tipo ; Tomada 00067 Tipos: 1 Solicitacao de fase 00068 2 Solicitacao de diferencial 00069 3 Acompanhamento (manda fase e diferencial) 00070 Tomada: Numero da tomada 00071 -1 (zero) quando de todas as tomadas 00072 */ 00073 /*Timer t; 00074 //t.start(); 00075 00076 for(i=0;i<Settings::get_NumNeighbors();i++){ 00077 s[i].connect(Settings::get_Neighbor(i), Settings::get_PortTCP()); 00078 } 00079 //t.stop(); 00080 //printf("The time taken in connection was %d useconds\n", t.read_us()); 00081 */ 00082 for(i=0; i< Settings::get_NumNeighbors(); i++){ 00083 //talvez verificar se o socket est conectado. Se no estiver, destruir objeto e conectar 00084 TCPSocketConnection sock; 00085 sock.connect(Settings::get_Neighbor(i), Settings::get_PortTCP()); 00086 //escritos = Settings::get_Socket(i).send_all(msg, sizeof(msg)-1); 00087 //printf("Socket %d\n",s[i].is_connected()); 00088 //Timer t1; 00089 //t1.start(); 00090 escritos = sock.send_all(msg, strlen(msg)); 00091 wait_ms(100); 00092 if(escritos != strlen(msg)){ 00093 printf("Erro ao enviar mensagem para vizinho\n"); 00094 break; 00095 } 00096 //t1.stop(); 00097 //printf("The time taken in send was %d useconds\n", t1.read_us()); 00098 00099 sock.close(); 00100 00101 } 00102 } 00103 00104 void CommTCP::SendAcom(int tipo,int tomada){ 00105 // Aqui chama a funço para enviar um acompanhamento 00106 for(int i=0;i<Settings::get_MaxChannels();i++) 00107 { 00108 EventDetector::get_Detector(i).ExternalTrigger(); 00109 } 00110 } 00111
Generated on Fri Jul 22 2022 19:25:55 by
