tcp_client.c 2.68 KB
/**
******************************************************************************
* @file         tcp_client.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_client.h"
#include "socket.h"
#include "wizchip_conf.h"
#include "w5100s_conf.h"
#include "bsp.h"

uint8_t ClientRecvBuff[DATA_BUF_SIZE] = {0};	
uint8_t ClientSendBuff[DATA_BUF_SIZE] = {0};	

void analysInfo()
{
	
}
void do_tcp_client(void)
{
	uint16 len=0,i = 0;	

	switch(getSn_SR(SOCK_TCPC))                           // 获取socket的状态
	{
		case SOCK_CLOSED:                                   // socket处于关闭状态
			socket(SOCK_TCPC,Sn_MR_TCP,local_port2,Sn_MR_ND);
			break;

		case SOCK_INIT:                                     // socket处于初始化状态
			connect(SOCK_TCPC,remote_ip,remote_port);         // socket连接服务器 
			break;

		case SOCK_ESTABLISHED:                              // socket处于连接建立状态
			if(getSn_IR(SOCK_TCPC) & Sn_IR_CON)
			{
				setSn_IR(SOCK_TCPC, Sn_IR_CON);                 // 清除接收中断标志位
			}

			len=getSn_RX_RSR(SOCK_TCPC);                      // 定义len为已接收数据的长度
			if(len>0)
			{
				recv(SOCK_TCPC,ClientRecvBuff,len);                       // 接收来自Server的数据
				
				for(i = 0; i < len; i++)
				{
					/**解碼接收的數據**/
					if (CenterCommand.NewReceive == false)
					{
						if (CenterDecode(ClientRecvBuff[i], &CenterCommand.CmdList) == teDecodSuccess)
						{
							CenterCommand.NewReceive = true;
						}
					}
					/**處理接收的數據**/
					if (CenterCommand.NewReceive)
					{
						if (CommandAnalysis() == 0)  //解码命令字
						{
							SendOrReplyTypeHandle();		// 命令字码处理
						}
						CenterCommand.NewReceive = false;
						//如果接收回来的数据是中控应答的,不再做处理
						if (CenterCommand.SendOrReply == sort_Receive)//判断是否是接受中控协议
						{
							Recive_check();	//取消重新发送
						}
					}
				}
				
				ClientRecvBuff[len]=0x00;                                 // 添加字符串结束符
				Uart_Printf(COM1,"来自服务器数据%s\r\n",ClientRecvBuff);
								
				memcpy(ClientSendBuff,"abcdefge\r\n",10); 

				send(SOCK_TCPC,ClientSendBuff,10);                       // 向Server发送数据
			}		  
			break;

		case SOCK_CLOSE_WAIT:                               // socket处于等待关闭状态
			close(SOCK_TCPC);
			break;
	}
}