www.pudn.com > TCPmodbushy.rar > send.c
/****************************************************************************** * Project : t_box * Module Name : send.C * CPU Type : C8051F340 * * Description : * * History Author Version Comment * 2006.10.22 HY V0.0 Original version * * Copyright (C) 2006 HY. All rights reserved. ******************************************************************************/ #include "o:\sysdef.h" #include "o:/lwip/tcp.h" #include#define TCP_LOCAL_PORT 502 /********************************************************************************* send data when reply request **********************************************************************************/ void send_data( uchar head, uchar idata *pd ) { ulong ip; uint port; if(*pd == UDP_PROTOCOL) { ip = MAKE_LONG( MAKE_WORD(*(pd+1),*(pd+2)),MAKE_WORD(*(pd+3),*(pd+4))); port = MAKE_WORD(*(pd+5),*(pd+6)); UDP_send_data(head,ip,port,MAKE_WORD(*(pd+7),*(pd+8))); } else { uchar num; num = *(pd+8); SendTcpDATA( Manager_Tcp_Table[( num )].pPointTcp_Pcb, &(memory[head])); free_memb(&(memory[head])); } } /********************************************************************************* send data when reply request **********************************************************************************/ void send_com_data( uchar head, uchar idata *pd ) { ulong ip; uint port; if(*pd == UDP_PROTOCOL) { ip = MAKE_LONG( MAKE_WORD(*(pd+1),*(pd+2)),MAKE_WORD(*(pd+3),*(pd+4))); port = MAKE_WORD(*(pd+5),*(pd+6)); UDP_send_data(head,ip,port,MAKE_WORD(*(pd+7),*(pd+8))); } else { ip = MAKE_LONG( MAKE_WORD(*(pd+8),*(pd+9)),MAKE_WORD(*(pd+10),*(pd+11))); port = MAKE_WORD(*(pd+12),*(pd+13)); if( ( Manager_Tcp_Table[( memory[head].TCP_connect_num & 0x7F )].pPointTcp_Pcb->remote_ip.addr == ip ) &&( Manager_Tcp_Table[( memory[head].TCP_connect_num & 0x7F )].pPointTcp_Pcb->remote_port == port ) ) { SendTcpDATA( Manager_Tcp_Table[( memory[head].TCP_connect_num & 0x7F )].pPointTcp_Pcb, &(memory[head])); } free_memb(&(memory[head])); } } /******************************************************************************************* use UDP send data ********************************************************************************************/ void UDP_send_data(uchar head,ulong ip,uint port,uint self_port) { UDP_SendData( &(memory[head]),self_port, ip, port ); free_memb(&(memory[head])); } /********************************************************************************************* use TCP send request **********************************************************************************************/ void TCP_send_data(uchar head,ulong ip) { uchar i; struct tcp_pcb xdata *connect; for(i = 0;i remote_ip.addr == ip ) &&( Manager_Tcp_Table[i].pPointTcp_Pcb->remote_port == TCP_LOCAL_PORT ) ) { SendTcpDATA(Manager_Tcp_Table[i].pPointTcp_Pcb, &(memory[head])); free_memb(&(memory[head])); return; } } } connect = ConnectedDest(0,ip,TCP_LOCAL_PORT); if(connect != NULL) SendTcpDATA( connect, &(memory[head])); else { #ifdef TESTDEBUG test[5]++; #endif } free_memb(&(memory[head])); } /*************************************************************************************** send message to serve1 ***************************************************************************************/ void send_to_serve1(uchar head) { ulong xdata ip; uint xdata port; ReadBytesFromFlash( SEVER1_IP_ADDRESS, SEVER1_IP_LEN, (uchar xdata *)&ip); ReadBytesFromFlash( SEVER1_PORT_ADDRESS, SEVER1_PORT_LEN, (uchar xdata *)&port); UDP_send_data(head,ip,port,UDP_LOCAL_PORT); } /*************************************************************************************** send message to serve1 ***************************************************************************************/ void send_to_serve2(uchar head) { ulong xdata ip; uint xdata port; ReadBytesFromFlash( SEVER2_IP_ADDRESS, SEVER2_IP_LEN, (uchar xdata *)&ip); ReadBytesFromFlash( SEVER2_PORT_ADDRESS, SEVER2_PORT_LEN, (uchar xdata *)&port); UDP_send_data(head,ip,port,UDP_LOCAL_PORT); }