tcp_server.c 4.79 KB
/**
******************************************************************************
* @file         tcp_server.c                                         
* @version      V1.0                          
* @date         2018-06-18                 
* @brief        tcp服务器数据回环测试        
*
* @company      深圳炜世科技有限公司
* @information  WIZnet W5100S官方代理商,全程技术支持,价格优势大!
* @website      www.wisioe.com       
* @forum        www.w5100s.com
* @qqGroup      579842114                                                     
******************************************************************************
*/
#include <stdio.h>
#include "tcp_server.h"
#include "socket.h"
#include "w5100s_conf.h"
#include "wizchip_conf.h"
#include "bsp.h"

//unsigned char ServerRecvBuff[BYTELENGTH] = {0};
//unsigned char ServerSendBuff[BYTELENGTH] = {0};

//extern u16 DataFromPLC[WORDLENGTH];			//PLC发送给控制器
//extern u16 DataToPLC[WORDLENGTH];              //控制器发送给PLC的数据数组

//extern void UpdateDataFromHolding(agvHandlePlcData *HandlePlcData);
//extern void UpdateDataToHolding(agvHandlePlcData *HandlePlcData);

void CopyShort(u16 *Des,u16 *Ptr, u16 Length)
{
	u16 i = 0;
	while(i < Length)
	{
		Des[i] = Ptr[i]; 
		i++;
	}
}

void do_tcp_server(void)
{  
	unsigned short len=0;  
	switch(getSn_SR(SOCK_TCPS))                             // 获取socket的状态
	{
		case SOCK_CLOSED:                                     // socket处于关闭状态
			Uart_Printf(COM1,"Creat\r\n");

			socket(SOCK_TCPS ,Sn_MR_TCP,local_port,Sn_MR_ND);   // 打开socket
			break;     

		case SOCK_INIT:                                       // socket已初始化状态
			
			Uart_Printf(COM1,"LISTEN\r\n");
			listen(SOCK_TCPS);                                  // socket建立监听
			break;

		case SOCK_ESTABLISHED:                                // socket处于连接建立状态
			//Uart_Printf(COM1,"plc已连接\r\n");
			if(getSn_IR(SOCK_TCPS) & Sn_IR_CON)
			{
				setSn_IR(SOCK_TCPS, Sn_IR_CON);                   // 清除接收中断标志位
			}
			len=getSn_RX_RSR(SOCK_TCPS);                        // 定义len为已接收数据的长度
			if(len>0)
			{
//				recv(SOCK_TCPS,ServerRecvBuff,len);                         // 接收来自Client的数据
//				ServerRecvBuff[len]=0x00;                                   // 添加字符串结束符				
//				CombineTwoByteToOneShort(DataFromPLC, WORDLENGTH, (char *)ServerRecvBuff);//因为PLC发送short型数据高低位有问题,因此在这里将char按照PLC高低位组成short型
//			
//				if(pSetup[SETUP_SIEMENS_SCREEN_OPEN])
//				{
//					CopyShort(&DataToPLC[60],&usRegHoldingBuf[1],100);
//					CopyShort(&usRegHoldingBuf[101],&DataFromPLC[60],100);	
//				}
//				
//				UpdateDataFromHolding(&HandlePlcData);//将PLC发送来的数据更新到专门结构体中去
//				
//				UpdateDataToHolding(&HandlePlcData);//将PLC需要的数据更新到要发送给PLC的数组中去
//				
//				BreakShortToTwoByte(DataToPLC, WORDLENGTH, (char *)ServerSendBuff);//将要发送给PLC的short型数组拆分成char型数组
//				
//				send(SOCK_TCPS,ServerSendBuff,BYTELENGTH);   					// 向Client发送数据
				
//				ServerSendBuff[0] = 1;
//				ServerSendBuff[1] = 2;
//				ServerSendBuff[2] = 3;
//				send(SOCK_TCPS,ServerSendBuff,3);
				
			}
			break;
    
		case SOCK_CLOSE_WAIT:                                 // socket处于等待关闭状态
			//Uart_Printf(COM1,"CLOSE\r\n");

			close(SOCK_TCPS);
			break;
	}
}

//unsigned char ServerRecvBuffQT[MonitorBYTELENGTH] = {0};
//unsigned char ServerSendBuffQT[MonitorBYTELENGTH] = {0};

//extern unsigned short DataToQt[MonitorWORDLENGTH];			//PLC发送给控制器


void do_tcp_serverQT(void)
{  
	unsigned short len=0;  
	switch(getSn_SR(SOCK_DHCP))                             // 获取socket的状态
	{
		case SOCK_CLOSED:                                     // socket处于关闭状态
			
			Uart_Printf(COM1,"Creat\r\n");

			socket(SOCK_DHCP ,Sn_MR_TCP,local_port3,Sn_MR_ND);   // 打开socket
			break;     

		case SOCK_INIT:                                       // socket已初始化状态
			
			Uart_Printf(COM1,"Listen\r\n");

			listen(SOCK_DHCP);                                  // socket建立监听
			break;

		case SOCK_ESTABLISHED:                                // socket处于连接建立状态

			if(getSn_IR(SOCK_DHCP) & Sn_IR_CON)
			{
				setSn_IR(SOCK_DHCP, Sn_IR_CON);                   // 清除接收中断标志位
			}
						
			len=getSn_RX_RSR(SOCK_DHCP);                        // 定义len为已接收数据的长度
			
			Uart_Printf(COM1,"len=%d\r\n",len);
			if(len>0)
			{
//				recv(SOCK_DHCP,ServerRecvBuffQT,len); 
//				
//				agvPlcToControl();
//				agvControlToPlc();
//				BreakShortToTwoByte(DataToQt, MonitorWORDLENGTH, (char *)ServerSendBuffQT);
//				send(SOCK_DHCP,ServerSendBuffQT,500);   					// 向Client发送数据
//				setSn_IR(SOCK_DHCP, Sn_IR_CON);  
//				len = 0;
			}
			break;
    
		case SOCK_CLOSE_WAIT:                                 // socket处于等待关闭状态
			close(SOCK_DHCP);
			break;
	}
}