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.
ethernet.cpp
00001 /******************************************************************** 00002 // RUTINAS DE COMUNICACION POR ETHERNET PARA MONITORES Y PLCs 00003 ********************************************************************/ 00004 00005 #include "declaraciones.h" 00006 #include "EthernetInterface.h" 00007 00008 00009 // Network interface 00010 extern EthernetInterface net; 00011 00012 00013 #define BROADCAST_PORT 37 00014 00015 00016 00017 extern ntp_packet in_data; 00018 extern bool newtime; 00019 00020 extern st_datos_servo datos_servo; // ESTRUCTURA DE VARIABLES DE ESTADO DEL SERVO DE DIRECCION 00021 extern DigitalIn SEL0; //Selector 1 = BABOR/ESTRIBOR, ON=BABOR OFF=ESTRIBOR 00022 extern DigitalOut led2; //LED1 00023 00024 extern int babor_estribor; // 0=babor 1=estribor 00025 00026 00027 /* 00028 uint8_t cmd_r[]={0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x01, 0x04, 0x70, 0x12, 0x00, 0x04}; 00029 uint8_t cmd_w1[]={0x00,0x03,0x00,0x00,0x00,0x0F,0x01,0x10,0x70,0x12,0x00,0x04,0x08,0x00,0x00,0x40,0x40,0xD7,0x0A,0x40,0x73}; // pone a min=3.0 (40 40 00 00), max=3.8 (40 73 D7 0A) 00030 uint8_t cmd_w2[]={0x00,0x03,0x00,0x00,0x00,0x0F,0x01,0x10,0x70,0x12,0x00,0x04,0x08,0x00,0x00,0x40,0x80,0x51,0xEC,0x40,0xA0}; // pone a min=4.0 (40 80 00 00), max=5.01 (40 A0 51 EC) 00031 00032 00033 // ******************************************************************************* 00034 // * ENVIA TRAMA MODBUS 00035 // ******************************************************************************* 00036 void envia_trama_modbus(){ 00037 00038 static long tiempo = millis(); 00039 static byte timon_babor_old; // %de timon babor bajado, para 100% 5s, 1% = 50ms 00040 static byte timon_estribor_old; // %de timon estribor bajado, para 100% 5s 00041 00042 00043 if( (timon_babor_old == datos_servo.timon_babor && timon_estribor_old == datos_servo.timon_estribor) || (millis()-tiempo)<1000 ) 00044 return; // Si no ha cambiado, no lo envia 00045 timon_babor_old = datos_servo.timon_babor; 00046 timon_estribor_old = datos_servo.timon_estribor; 00047 00048 float hist = 0.2; 00049 long v_max = 8000; 00050 long v_min = 3000; 00051 00052 float val_tb_min = ((float)map(datos_servo.timon_babor, 0, 100, v_min, v_max)/1000); 00053 float val_tb_max = ((float)map(datos_servo.timon_babor, 0, 100, v_min, v_max)/1000)+hist; 00054 float val_te_min = ((float)map(datos_servo.timon_babor, 0, 100, v_min, v_max)/1000); 00055 float val_te_max = ((float)map(datos_servo.timon_babor, 0, 100, v_min, v_max)/1000)+hist; 00056 00057 uint8_t *p_tb_min = (uint8_t *)&val_tb_min; 00058 uint8_t *p_tb_max = (uint8_t *)&val_tb_max; 00059 uint8_t *p_te_min = (uint8_t *)&val_te_min; 00060 uint8_t *p_te_max = (uint8_t *)&val_te_max; 00061 00062 // printf("tb=%.2f te=%.2f\r\n", val_tb_min, val_te_min); 00063 // PrintHex8(p_tb_min, 4); // prints 8-bit data in hex with leading zeroes 00064 // printf("\r\n"); 00065 00066 00067 // Show the network address 00068 SocketAddress a; 00069 00070 // Open a socket on the network interface, and create a TCP connection to mbed.org 00071 TCPSocket socket; 00072 socket.open(&net); 00073 socket.set_timeout(10); 00074 00075 a.set_port(502); 00076 a.set_ip_address("192.168.1.200"); 00077 socket.connect(a); 00078 00079 // Send a simple http request 00080 uint8_t buf[40]; 00081 memcpy(buf, cmd_w1, sizeof cmd_w1); 00082 buf[13] = p_tb_min[1]; 00083 buf[14] = p_tb_min[0]; 00084 buf[15] = p_tb_min[3]; 00085 buf[16] = p_tb_min[2]; 00086 buf[17] = p_tb_max[1]; 00087 buf[18] = p_tb_max[0]; 00088 buf[19] = p_tb_max[3]; 00089 buf[20] = p_tb_max[2]; 00090 00091 int scount = socket.send(buf, sizeof cmd_w1); 00092 // printf("sent %d bytes\r\n", scount); 00093 printf(">%d\r\n", millis()-tiempo); 00094 00095 00096 // Recieve a simple http response and print out the response line 00097 char rbuffer[64]; 00098 int rcount = socket.recv(rbuffer, sizeof rbuffer); 00099 // printf("recv %dbytes\r\n", rcount); 00100 // PrintHex8((uint8_t *)rbuffer, rcount); // prints 8-bit data in hex with leading zeroes 00101 // printf("\r\n"); 00102 printf("<%d\r\n", millis()-tiempo); 00103 tiempo = millis(); 00104 00105 00106 // Close the socket to return its memory and bring down the network interface 00107 socket.close(); 00108 } 00109 00110 00111 00112 //void onTCPSocketEvent(void* buffer, size_t size) { 00113 // printf("TCP: bytes %d\r\n", size); 00114 //} 00115 */ 00116 00117 00118 /* 00119 * ENVIA TRAMA DE ESTADO DEL CONTROL SERVO 0=babor 1=estribor 00120 */ 00121 void envia_trama_estado(){ 00122 static st_datos_servo datos_ant; // ultimos datos enviados 00123 static uint16_t ntramas_repetidas = 0; 00124 uint8_t buf[100]; 00125 uint16_t chk; 00126 uint8_t nbytes=0; 00127 bool envia=true; 00128 uint16_t tipo=TRAMA_ESTADO_SERVO_DIR; 00129 00130 static float rumbo = 0.0; 00131 00132 UDPSocket sock(&net); 00133 00134 //**************************************** 00135 // ENVIO DE ESTADO A PANTALLAS 00136 00137 nbytes = sizeof(datos_servo); 00138 datos_servo.n_servo = babor_estribor; 00139 00140 /* 00141 if(datos_servo.estado == DIR_MASTER || datos_servo.estado == DIR_REPOSO) 00142 datos_servo.posicion_mando = desviacion_servo; 00143 else 00144 datos_servo.posicion_mando = pos_objetivo_servo; // PARA PILOTO AUTOMATICO 00145 */ 00146 // DEBUG_PORT.print("enviado: "); 00147 // DEBUG_PORT.println(datos_servo.posicion_mando); 00148 00149 //Comprueba si los datos han variado 00150 if(memcmp((void *)&datos_ant, (void *)&datos_servo, nbytes)==0 && ntramas_repetidas<10){ 00151 envia = false; 00152 ntramas_repetidas++; // cuenta tramas 00153 } 00154 else{ 00155 memcpy((void *)&datos_ant, (void *)&datos_servo, nbytes); 00156 ntramas_repetidas=0; 00157 } 00158 memcpy(&buf[4], (void *)&datos_servo, nbytes); 00159 00160 uint8_t *p = buf; 00161 buf[0]=STX; 00162 buf[1]=nbytes+5; 00163 buf[2]=(byte)tipo; // tipo de trama 00164 buf[3]=(byte)(tipo>>8); 00165 p = (p+nbytes+4); 00166 chk = checksum(buf, nbytes+4); 00167 *p=(byte)(chk); p++; 00168 *p=(byte)(chk>>8); p++; 00169 *p=ETX; 00170 00171 // for(int i=0; i<nbytes+7; i++){ 00172 // DEBUG_PORT.print(" 0x"); 00173 // if(buf[i]<16) 00174 // DEBUG_PORT.print("0"); 00175 // DEBUG_PORT.print(buf[i], HEX); 00176 // } 00177 // DEBUG_PORT.println(); 00178 00179 if(envia==true || ntramas_repetidas>=10){ 00180 if(0 > sock.sendto("255.255.255.255", 6000, buf, nbytes+7)) { 00181 printf("Error enviando estado a monitores\r\n"); 00182 // return -1; 00183 } 00184 else{ 00185 // printf("Enviado estado a monitores\r\n"); 00186 } 00187 ntramas_repetidas = 0; 00188 00189 // Udp.beginPacket(ip_PC, 6000); // IP y puerto del receptor (broadcast) 00190 // Udp.write(buf, nbytes+7); 00191 // Udp.endPacket(); 00192 } 00193 00194 sock.close(); 00195 } 00196 00197 00198 00199 00200 void onUDPSocketEvent(void* buffer, size_t size) { 00201 printf("UDP event detected\r\n"); 00202 } 00203 00204 00205 00206 /***************************************************************** 00207 // RUTINA PRINCIPAL HILO ETHERNET 00208 ******************************************************************/ 00209 void udp_main() { 00210 long contador=0; // Contador segundos 00211 00212 /* 00213 // Open Ethernet connection 00214 do{ 00215 net.disconnect(); 00216 int j = net.set_network(IP, MASK, GATEWAY); 00217 printf("set IP status: %i \r\n",j); 00218 00219 if(0 != net.connect()) { 00220 printf("Error connecting\r\n"); 00221 Thread::wait(1000); // espera de 1s 00222 // return -1; 00223 } 00224 }while(net.connect() != 0); 00225 00226 // Show the network address 00227 const char *ip = net.get_ip_address(); 00228 printf("IP address is: %s\r\n", ip ? ip : "No IP"); 00229 00230 // UDPSocket sock(&net); 00231 SocketAddress sockAddr; 00232 */ 00233 00234 while(1){ 00235 00236 /* 00237 if(contador%10==0){ // cada 10s 00238 UDPSocket sock(&net); 00239 led2 = 1; 00240 char out_buffer[] = "time"; 00241 // char out_buffer_2[50]; 00242 // int nc=0; 00243 if(0 > sock.sendto("time.nist.gov", BROADCAST_PORT, out_buffer, sizeof(out_buffer))) { 00244 printf("Error sending data\r\n"); 00245 // return -1; 00246 } 00247 00248 int n = sock.recvfrom(&sockAddr, &in_data, sizeof(ntp_packet)); 00249 in_data.secs = ntohl( in_data.secs ) - 2208988800; // 1900-1970 00250 newtime = true; // nueva fecha recibida 00251 00252 printf("\r\n\r\nTime Received %lu seconds since 1/01/1900 00:00 GMT\r\n", (uint32_t)in_data.secs); 00253 nc = sprintf(out_buffer_2, "%s", ctime(( const time_t* )&in_data.secs) ); 00254 printf("Time = %s\r\n", out_buffer_2); 00255 00256 printf("Time Server Address: %s Port: %d\r\n\r\n", sockAddr.get_ip_address(), sockAddr.get_port()); 00257 00258 if(0 > sock.sendto("192.168.1.37", 6000, out_buffer_2, nc)) { 00259 printf("\r\nError sending data 2\r\n\r\n"); 00260 return -1; 00261 } 00262 // Close the socket and bring down the network interface 00263 sock.close(); 00264 led2 = 0; 00265 } 00266 */ 00267 led2 = 1; 00268 envia_trama_estado(); // envia trama de estado 00269 led2 = 0; 00270 Thread::wait(250); // espera de 150ms 00271 contador++; 00272 } 00273 net.disconnect(); 00274 } 00275
Generated on Fri Jul 15 2022 16:48:31 by
1.7.2