LWIP同时实现服务器和客户端的程序-STM32的UCOSII下实现

BY 冰河马Icehippo    by glassine  

首先,省略

其次,看程序吧

select服务器:

/*
by glassine
 */


#include <string.h>

#include "chargen.h"
#include "tcpecho_multi.h"
#include "malloc.h"
#include "ssprotocol.h"

#if LWIP_SOCKET

#define MAX_SERV                 25         /* Maximum number of chargen services. Don't need too many */
#define CHARGEN_THREAD_NAME      "chargen"
#define CHARGEN_PRIORITY         254       /* Really low priority */
#define CHARGEN_THREAD_STACKSIZE 0
#define SEND_SIZE TCP_SNDLOWAT             /* If we only send this much, then when select
                                              says we can send, we know we won't block */

/*------------------------------------------------------------*/
//TCP服务器任务
#define CHARGEN_PRIO			9
//任务堆栈大小
#define CHARGEN_STK_SIZE	512
//任务堆栈
OS_STK CHARGEN_TASK_STK[CHARGEN_STK_SIZE];
extern u8 tcpecho_recvbuf[TCPECHO_RX_BUFSIZE];	//TCP服务器接收数据缓冲区

extern u8 send_relay_flag;
extern u8 sum_recvbuf1[SUM_RX_BUFSIZE];				//烟感数据压缩后的数据缓存
extern u8 sum_recvbuf3[SUM_RX_BUFSIZE];
extern u8 sum_recvbuf5[SUM_RX_BUFSIZE];

extern u8 sum_recvbuf7[SUM_RX_BUFSIZE];
extern u8 sum_recvbuf9[SUM_RX_BUFSIZE];
extern u8 sum_recvbuf11[SUM_RX_BUFSIZE];
//char ssssbuffer[138];
extern u8 relaysend[12];


extern u8 send_hbt_flag;
/**********************************/
extern u8 relay_on_data_complete_flag1;			//继电器上电
extern u8 relay_on_data_complete_flag3;			//继电器上电			
extern u8 relay_on_data_complete_flag5;			//继电器上电			
extern u8 relay_on_data_complete_flag7;			//继电器上电			
extern u8 relay_on_data_complete_flag9;			//继电器上电			
extern u8 relay_on_data_complete_flag11;		//继电器上电			
/**********************************/
extern u8 relay_off_data_complete_flag1;		//继电器断电
extern u8 relay_off_data_complete_flag3;		//继电器断电
extern u8 relay_off_data_complete_flag5;		//继电器断电
extern u8 relay_off_data_complete_flag7;		//继电器断电
extern u8 relay_off_data_complete_flag9;		//继电器断电
extern u8 relay_off_data_complete_flag11;		//继电器断电
/**********************************/
extern u8 relay_on_data_do_flag1;			//继电器上电
extern u8 relay_on_data_do_flag3;			//继电器上电			
extern u8 relay_on_data_do_flag5;			//继电器上电			
extern u8 relay_on_data_do_flag7;			//继电器上电			
extern u8 relay_on_data_do_flag9;			//继电器上电			
extern u8 relay_on_data_do_flag11;		//继电器上电			
/**********************************/
extern u8 relay_off_data_do_flag1;		//继电器断电
extern u8 relay_off_data_do_flag3;		//继电器断电
extern u8 relay_off_data_do_flag5;		//继电器断电
extern u8 relay_off_data_do_flag7;		//继电器断电
extern u8 relay_off_data_do_flag9;		//继电器断电
extern u8 relay_off_data_do_flag11;		//继电器断电
/**********************************/
extern u8 relay_on_do_flag;					//继电器上电标志
extern u8 relay_on_do_flag1;				//继电器上电
extern u8 relay_on_do_flag3;				//继电器上电		
extern u8 relay_on_do_flag5;				//继电器上电		
extern u8 relay_on_do_flag7;				//继电器上电		
extern u8 relay_on_do_flag9;				//继电器上电		
extern u8 relay_on_do_flag11;				//继电器上电		

extern u8 relay_off_do_flag;				//继电器断电标志
extern u8 relay_off_do_flag1;				//继电器断电
extern u8 relay_off_do_flag3;				//继电器断电
extern u8 relay_off_do_flag5;				//继电器断电
extern u8 relay_off_do_flag7;				//继电器断电
extern u8 relay_off_do_flag9;				//继电器断电
extern u8 relay_off_do_flag11;			//继电器断电
/**********************************/
extern u8 rst_delay_flag1;
extern u8 rst_delay_flag3;
extern u8 rst_delay_flag5;
extern u8 rst_delay_flag7;
extern u8 rst_delay_flag9;
extern u8 rst_delay_flag11;
/**********************************/
extern u8	relay_on_reply1;				//上电应答标志
extern u8	relay_on_reply3;				//上电应答标志
extern u8	relay_on_reply5;				//上电应答标志
extern u8	relay_on_reply7;				//上电应答标志
extern u8	relay_on_reply9;				//上电应答标志
extern u8	relay_on_reply11;				//上电应答标志
/**********************************/
extern u8 	pwd_ready_ok1;
extern u8 	pwd_ready_ok3;
extern u8 	pwd_ready_ok5;
extern u8 	pwd_ready_ok7;
extern u8 	pwd_ready_ok9;
extern u8 	pwd_ready_ok11;
/**********************************/
extern u8 	rst_ready_ok1;
extern u8 	rst_ready_ok3;
extern u8 	rst_ready_ok5;
extern u8 	rst_ready_ok7;
extern u8 	rst_ready_ok9;
extern u8 	rst_ready_ok11;
/**********************************/
extern u8 on_status_flag1;
extern u8 on_status_flag3;
extern u8 on_status_flag5;
extern u8 on_status_flag7;
extern u8 on_status_flag9;
extern u8 on_status_flag11;
/**********************************/
extern u8 write_relay_flag;//写PLC内继电器控制的标志
/**********************************/
u8 tcpecho_recvbuf[TCPECHO_RX_BUFSIZE];	//TCP服务器接收数据缓冲区	

/****************************************************************************************************************/
/****************************************************************************************************************/
/***************************************************************/
struct charcb 
{
    struct charcb *next;
    int socket;
    struct sockaddr_in cliaddr;
    socklen_t clilen;
    char nextchar;
};

static struct charcb *charcb_list = 0;
static int do_read(struct charcb *p_charcb);
static void close_chargen(struct charcb *p_charcb);
/****************************************************************************************************************/
/****************************************************************************************************************/
/**************************************************************
 * void chargen_thread(void *arg)
 *
 * chargen task. This server will wait for connections on well
 * known TCP port number: 19. For every connection, the server will
 * write as much data as possible to the tcp port.
 **************************************************************/
static void chargen_thread(void *arg)
{
    int listenfd;
    struct sockaddr_in chargen_saddr;
    fd_set readset;
    fd_set writeset;
    int i, maxfdp1;
    struct charcb *p_charcb;
    LWIP_UNUSED_ARG(arg);
		/****************************************************************/
    /* First acquire our socket for listening for connections */
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    LWIP_ASSERT("chargen_thread(): Socket create failed.\r\n", listenfd >= 0);
    memset(&chargen_saddr, 0, sizeof(chargen_saddr));
    chargen_saddr.sin_family = AF_INET;
    chargen_saddr.sin_addr.s_addr = PP_HTONL(INADDR_ANY);
    chargen_saddr.sin_port = htons(8087);     /* Chargen server port */
		/****************************************************************/
    if (bind(listenfd, (struct sockaddr *) &chargen_saddr, sizeof(chargen_saddr)) == -1)
		{
			printf("Socket bind failed\r\n");
		}
		else
		{
			printf("Socket bind SUCCESS!\r\n");
		}
    LWIP_ASSERT("chargen_thread(): Socket bind failed.\r\n", 0);
    /* Put socket into listening mode */
		//返回0是成功的,非0是失败
    if (listen(listenfd, MAX_SERV) == -1)//将MAX_SERV设置成了50,默认可以连接50个客户端
		{
			printf("Listen failed\r\n");
		}
		else
		{
			printf("Listen bind SUCCESS!\r\n");
		}
    LWIP_ASSERT("chargen_thread(): Listen failed.\r\n", 0);
    /* Wait forever for network input: This could be connections or data */
		for (;;)
    {
        maxfdp1 = listenfd+1;
				
        /* Determine what sockets need to be in readset */
        FD_ZERO(&readset);
        FD_ZERO(&writeset);
        FD_SET(listenfd, &readset);
        for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
        {
           if (maxfdp1 < p_charcb->socket + 1)
           maxfdp1 = p_charcb->socket + 1;
           FD_SET(p_charcb->socket, &readset);
           FD_SET(p_charcb->socket, &writeset);
        }

        /* Wait for data or a new connection */
        i = select(maxfdp1, &readset, &writeset, 0, 0);
        
        if (i == 0)
            continue;
        /* At least one descriptor is ready */
        if (FD_ISSET(listenfd, &readset))
        {
            /* We have a new connection request!!! */
            /* Lets create a new control block */
            p_charcb = (struct charcb *)mem_malloc(sizeof(struct charcb));
            if (p_charcb)
            {
                p_charcb->socket = accept(listenfd,
                                        (struct sockaddr *) &p_charcb->cliaddr,
                                        &p_charcb->clilen);
                if (p_charcb->socket < 0)
                    mem_free(p_charcb);
                else
                {
                    /* Keep this tecb in our list */
                    p_charcb->next = charcb_list;
                    charcb_list = p_charcb;
                    p_charcb->nextchar = 0x21;
                }
            } else {
                /* No memory to accept connection. Just accept and then close */
                int sock;
                struct sockaddr cliaddr;
                socklen_t clilen;

                sock = accept(listenfd, &cliaddr, &clilen);
                if (sock >= 0)
                    close(sock);
            }
        }
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
				/*****************************************************************************************************/
				//读客户端数据,轮询的读,所有连接的设备都轮询一遍
				/*****************************************************************************************************/
					for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
					{
						if (FD_ISSET(p_charcb->socket, &readset))
						{
							if (do_read(p_charcb) < 0)
							break;
						}
					}
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
					/*****************************************************************************************************/
					//写客户端数据,写的是心跳,所有连接的设备都轮询一遍
					/*****************************************************************************************************/
					if(send_hbt_flag==1)
					{
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							if (FD_ISSET(p_charcb->socket, &writeset))
							{						
								int writecount;
								//7E 01 FF FF 0F 00 02 00 00 5F 0C 7E//全部心跳
								u8 line[12]={0x7E,0x01,0xFF,0xFF,0x0F,0x00,0x02,0x00,0x00,0x5F,0x0C,0x7E};
								char setchar = p_charcb->nextchar;
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								send_hbt_flag=0;	
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
							}
						}
					}
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
					/*****************************************************************************************************/
					///7E 01 00 27 0D 00 02 00 00 48 68 7E断开,通电
					/*****************************************************************************************************/
					if(relay_on_do_flag1==1)
					{
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.207"))
							//if (FD_ISSET(p_charcb->socket, &writeset))
							{
								int writecount;
								//7E 01 00 25 0D 00 02 00 01 6A A8 7E
								u8 line[12]={0x7E,0x01,0x00,0x25,0x0D,0x00,0x02,0x00,0x01,0x6A,0xA8,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								/*********发送完后继电器断电标志归0*********/
								//relay_on_do_flag1=0;
							}
							/***********没有轮询到相应的IP************/
						}
					}
					/**************************************************************************/
					else if(relay_on_do_flag3==1)
					{
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.208"))
							{
								int writecount;
								//7E 01 00 26 0D 00 02 00 01 59 A8 7E
								u8 line[12]={0x7E,0x01,0x00,0x26,0x0D,0x00,0x02,0x00,0x01,0x59,0xA8,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								/*********发送完后继电器断电标志归0*********/
								//relay_on_do_flag3=0;
							}
							/***********没有轮询到相应的IP************/
						}
					}
					/**************************************************************************/
					else if(relay_on_do_flag5==1)
					{
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.209"))
							{
								int writecount;
								//7E 01 00 27 0D 00 02 00 01 88 A9 7E
								u8 line[12]={0x7E,0x01,0x00,0x27,0x0D,0x00,0x02,0x00,0x01,0x88,0xA9,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								/*********发送完后继电器断电标志归0*********/
								//relay_on_do_flag5=0;
							}
							/***********没有轮询到相应的IP************/
						}
					}
					else if(relay_on_do_flag7==1)
					{
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.210"))
							{
								int writecount;
								//7E 01 00 28 0D 00 02 00 01 77 A9 7E
								u8 line[12]={0x7E,0x01,0x00,0x28,0x0D,0x00,0x02,0x00,0x01,0x77,0xA9,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								/*********发送完后继电器断电标志归0*********/
								//relay_on_do_flag7=0;
							}
							/***********没有轮询到相应的IP************/
						}
					}
					else if(relay_on_do_flag9==1)
					{
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.211"))
							{
								int writecount;
								//7E 01 00 29 0D 00 02 00 01 A6 A8 7E
								u8 line[12]={0x7E,0x01,0x00,0x29,0x0D,0x00,0x02,0x00,0x01,0xA6,0xA8,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								/*********发送完后继电器断电标志归0*********/
								//relay_on_do_flag9=0;
							}
							/***********没有轮询到相应的IP************/
						}
					}	
					else if(relay_on_do_flag11==1)
					{
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.212"))
							{
								int writecount;
								//7E 01 00 2A 0D 00 02 00 01 95 A8 7E
								u8 line[12]={0x7E,0x01,0x00,0x2A,0x0D,0x00,0x02,0x00,0x01,0x95,0xA8,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								/*********发送完后继电器断电标志归0*********/
								//relay_on_do_flag11=0;
							}
							/***********没有轮询到相应的IP************/
						}
					}
					/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
					/*****************************************************************************************************/
					///7E 01 00 27 0D 00 02 00 00 48 68 7E断开,通电
					/*****************************************************************************************************/
					if(relay_on_data_do_flag1==1)
					{
						relay_on_data_complete_flag1=1;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.207"))
							{
								int writecount;
								//7E 01 00 25 0D 00 02 00 01 6A A8 7E
								u8 line[12]={0x7E,0x01,0x00,0x25,0x0D,0x00,0x02,0x00,0x01,0x6A,0xA8,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								printf("1号完成复位指令的继电器通电操作!!!\r\n");
							}
							/***********没有轮询到相应的IP************/
						}
						relay_off_data_do_flag1=0;
					}
					/**************************************************************************/
					else if(relay_on_data_do_flag3==1)
					{
						relay_on_data_complete_flag3=1;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.208"))
							{
								int writecount;
								//7E 01 00 26 0D 00 02 00 01 59 A8 7E
								u8 line[12]={0x7E,0x01,0x00,0x26,0x0D,0x00,0x02,0x00,0x01,0x59,0xA8,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								printf("3号完成复位指令的继电器通电操作!!!\r\n");
							}
							/***********没有轮询到相应的IP************/
						}
						relay_off_data_do_flag3=0;
					}
					/**************************************************************************/
					else if(relay_on_data_do_flag5==1)
					{
						relay_on_data_complete_flag5=1;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.209"))
							{
								int writecount;
								//7E 01 00 27 0D 00 02 00 01 88 A9 7E
								u8 line[12]={0x7E,0x01,0x00,0x27,0x0D,0x00,0x02,0x00,0x01,0x88,0xA9,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								printf("5号完成复位指令的继电器通电操作!!!\r\n");
							}
							/***********没有轮询到相应的IP************/
						}
						relay_off_data_do_flag5=0;
					}
					else if(relay_on_data_do_flag7==1)
					{
						relay_on_data_complete_flag7=1;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.210"))
							{
								int writecount;
								//7E 01 00 28 0D 00 02 00 01 77 A9 7E
								u8 line[12]={0x7E,0x01,0x00,0x28,0x0D,0x00,0x02,0x00,0x01,0x77,0xA9,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								printf("7号完成复位指令的继电器通电操作!!!\r\n");
							}
							/***********没有轮询到相应的IP************/
						}
					}
					else if(relay_on_data_do_flag9==1)
					{
						relay_on_data_complete_flag9=1;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.211"))
							{
								int writecount;
								//7E 01 00 29 0D 00 02 00 01 A6 A8 7E
								u8 line[12]={0x7E,0x01,0x00,0x29,0x0D,0x00,0x02,0x00,0x01,0xA6,0xA8,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								printf("9号完成复位指令的继电器通电操作!!!\r\n");
							}
							/***********没有轮询到相应的IP************/
						}
					}	
					else if(relay_on_data_do_flag11==1)
					{
						relay_on_data_complete_flag11=1;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.212"))
							{
								int writecount;
								//7E 01 00 2A 0D 00 02 00 01 95 A8 7E
								u8 line[12]={0x7E,0x01,0x00,0x2A,0x0D,0x00,0x02,0x00,0x01,0x95,0xA8,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								printf("11号完成复位指令的继电器通电操作!!!\r\n");
							}
							/***********没有轮询到相应的IP************/
						}
					}
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
					/*****************************************************************************************************/
					//7E 01 00 27 0C 00 02 00 00 88 55 7E吸合,断电
					/*****************************************************************************************************/
					if(relay_off_do_flag1==1)
					{
						/*****未断电前,先停止发送PWD数据*****/
						pwd_ready_ok1=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP*********/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.207"))
							{
								int writecount;
								//7E 01 00 25 0C 00 02 00 01 AA 95 7E
								//7E 01 00 01 0C 00 02 00 01 4E 93 7E//01板的断电指令
								u8 line[12]={0x7E,0x01,0x00,0x25,0x0C,0x00,0x02,0x00,0x01,0xAA,0x95,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
							}
							else
							{
								/*无操作*/
							}
						}
					}
					/**************************************************************************/
					else if(relay_off_do_flag3==1)
					{
						pwd_ready_ok3=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.208"))
							{
								int writecount;
								//7E 01 00 26 0C 00 02 00 01 99 95 7E
								u8 line[12]={0x7E,0x01,0x00,0x26,0x0C,0x00,0x02,0x00,0x01,0x99,0x95,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
							}
							else
							{
								/*无操作*/
							}
						}
					}
					/**************************************************************************/
					else if(relay_off_do_flag5==1)
					{
						pwd_ready_ok5=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.209"))
							{
								int writecount;
								//7E 01 00 27 0C 00 02 00 01 48 94 7E
								u8 line[12]={0x7E,0x01,0x00,0x27,0x0C,0x00,0x02,0x00,0x01,0x48,0x94,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
							}
							else
							{
								/*无操作*/
							}
						}
					}
					else if(relay_off_do_flag7==1)
					{
						pwd_ready_ok7=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.210"))
							{
								int writecount;
								//7E 01 00 28 0C 00 02 00 01 B7 94 7E
								u8 line[12]={0x7E,0x01,0x00,0x28,0x0C,0x00,0x02,0x00,0x01,0xB7,0x94,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
							}
							else
							{
								/*无操作*/
							}
						}
					}
					else if(relay_off_do_flag9==1)
					{
						pwd_ready_ok9=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.211"))
							{
								int writecount;
								//7E 01 00 29 0C 00 02 00 01 66 95 7E
								u8 line[12]={0x7E,0x01,0x00,0x29,0x0C,0x00,0x02,0x00,0x01,0x66,0x95,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
							}
							else
							{
								/*无操作*/
							}
						}
					}	
					else if(relay_off_do_flag11==1)
					{
						pwd_ready_ok11=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.212"))
							{
								int writecount;
								//7E 01 00 2A 0C 00 02 00 01 55 95 7E
								u8 line[12]={0x7E,0x01,0x00,0x2A,0x0C,0x00,0x02,0x00,0x01,0x55,0x95,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
							}
							else
							{
								/*无操作*/
							}
						}
					}
					/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
					/*****************************************************************************************************/
					//7E 01 00 27 0C 00 02 00 00 88 55 7E吸合,断电
					/*****************************************************************************************************/
					if(relay_off_data_do_flag1==1)
					{
						relay_off_data_complete_flag1=1;
						rst_ready_ok1=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.207"))
							{
								int writecount;
								//7E 01 00 25 0C 00 02 00 01 AA 95 7E
								//7E 01 00 01 0C 00 02 00 01 4E 93 7E//01板的断电指令
								u8 line[12]={0x7E,0x01,0x00,0x25,0x0C,0x00,0x02,0x00,0x01,0xAA,0x95,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								//relay_on_data_do_flag1=0;
								OSTimeDlyHMSM(0,0,1,0);  //延时30ms
								relay_on_data_do_flag1=1;
								printf("1号完成复位指令的继电器断电操作!!!\r\n");
							}
						}
						//OSTimeDlyHMSM(0,0,0,1000);  //延时30ms
						//relay_on_data_do_flag1=1;
					}
					/**************************************************************************/
					else if(relay_off_data_do_flag3==1)
					{
						relay_off_data_complete_flag3=1;
						rst_ready_ok3=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.208"))
							{
								int writecount;
								//7E 01 00 26 0C 00 02 00 01 99 95 7E
								u8 line[12]={0x7E,0x01,0x00,0x26,0x0C,0x00,0x02,0x00,0x01,0x99,0x95,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								//relay_on_data_do_flag3=0;
								OSTimeDlyHMSM(0,0,1,0);  //延时30ms
								relay_on_data_do_flag3=1;
								printf("3号完成复位指令的继电器断电操作!!!\r\n");
							}
						}
						//OSTimeDlyHMSM(0,0,0,1000);  //延时30ms
						//relay_on_data_do_flag3=1;
					}
					/**************************************************************************/
					else if(relay_off_data_do_flag5==1)
					{
						relay_off_data_complete_flag5=1;
						rst_ready_ok5=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.209"))
							{
								int writecount;
								//7E 01 00 27 0C 00 02 00 01 48 94 7E
								u8 line[12]={0x7E,0x01,0x00,0x27,0x0C,0x00,0x02,0x00,0x01,0x48,0x94,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								//relay_on_data_do_flag5=0;
								OSTimeDlyHMSM(0,0,1,0);  //延时30ms
								relay_on_data_do_flag5=1;
								printf("5号完成复位指令的继电器断电操作!!!\r\n");
							}
						}
						//OSTimeDlyHMSM(0,0,0,1000);  //延时30ms
						//relay_on_data_do_flag5=1;
					}
					else if(relay_off_data_do_flag7==1)
					{
						relay_off_data_complete_flag7=1;
						rst_ready_ok7=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.210"))
							{
								int writecount;
								//7E 01 00 28 0C 00 02 00 01 B7 94 7E
								u8 line[12]={0x7E,0x01,0x00,0x28,0x0C,0x00,0x02,0x00,0x01,0xB7,0x94,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								//relay_on_data_do_flag7=0;
								OSTimeDlyHMSM(0,0,1,0);  //延时30ms
								relay_on_data_do_flag7=1;
								printf("7号完成复位指令的继电器断电操作!!!\r\n");
							}
						}
						//OSTimeDlyHMSM(0,0,0,50);  //延时30ms
						//relay_on_data_do_flag7=1;
					}
					else if(relay_off_data_do_flag9==1)
					{
						relay_off_data_complete_flag9=1;
						rst_ready_ok9=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.211"))
							{
								int writecount;
								//7E 01 00 29 0C 00 02 00 01 66 95 7E
								u8 line[12]={0x7E,0x01,0x00,0x29,0x0C,0x00,0x02,0x00,0x01,0x66,0x95,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								//relay_on_data_do_flag9=0;
								OSTimeDlyHMSM(0,0,1,0);  //延时30ms
								relay_on_data_do_flag9=1;
								printf("9号完成复位指令的继电器断电操作!!!\r\n");
							}
						}
						//OSTimeDlyHMSM(0,0,0,50);  //延时30ms
						//relay_on_data_do_flag9=1;
					}	
					else if(relay_off_data_do_flag11==1)
					{
						relay_off_data_complete_flag11=1;
						rst_ready_ok11=0;
						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
						{
							/***********轮询到相应的IP************/
							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.212"))
							{
								int writecount;
								//7E 01 00 2A 0C 00 02 00 01 55 95 7E
								u8 line[12]={0x7E,0x01,0x00,0x2A,0x0C,0x00,0x02,0x00,0x01,0x55,0x95,0x7E};
								writecount=lwip_send(p_charcb->socket, line, 12, 0);
								if (writecount< 0)
								{
									close_chargen(p_charcb);
									break;
								}
								//relay_on_data_do_flag11=0;
								OSTimeDlyHMSM(0,0,1,0);  //延时30ms
								relay_on_data_do_flag11=1;
								printf("11号完成复位指令的继电器断电操作!!!\r\n");
							}
						}
						//OSTimeDlyHMSM(0,0,0,50);  //延时30ms
						//relay_on_data_do_flag11=1;
					}								
		OSTimeDlyHMSM(0,0,0,30);  //延时10ms
    }
}
/****************************************************************************************************************/
/****************************************************************************************************************/
/**************************************************************
 * void close_chargen(struct charcb *p_charcb)
 *
 * Close the socket and remove this charcb from the list.
 **************************************************************/
static void close_chargen(struct charcb *p_charcb)
//void close_chargen(struct charcb *p_charcb)
{
    struct charcb *p_search_charcb;

        /* Either an error or tcp connection closed on other
         * end. Close here */
        close(p_charcb->socket);
        /* Free charcb */
        if (charcb_list == p_charcb)
            charcb_list = p_charcb->next;
        else
            for (p_search_charcb = charcb_list; p_search_charcb; p_search_charcb = p_search_charcb->next)
            {
                if (p_search_charcb->next == p_charcb)
                {
                    p_search_charcb->next = p_charcb->next;
                    break;
                }
            }
        mem_free(p_charcb);
}
/****************************************************************************************************************/
/****************************************************************************************************************/
/**************************************************************
 * void do_read(struct charcb *p_charcb)
 *
 * Socket definitely is ready for reading. Read a buffer from the socket and
 * discard the data.  If no data is read, then the socket is closed and the
 * charcb is removed from the list and freed.
 **************************************************************/
static int do_read(struct charcb *p_charcb)
{
	char buffer[138];
    int readcount,i;

    /* Read some data */
    readcount = read(p_charcb->socket, &buffer, 138);
    if (readcount <= 0)
    {
      close_chargen(p_charcb);
			mymemset(tcpecho_recvbuf,0x00,138);
      return -1;	
    }
		else
		{
			mymemset(tcpecho_recvbuf,0x00,138);
			//printf("通过串口把数据发送出去:\r\n");
			for(i=0;i<138;i++)
			{
				tcpecho_recvbuf[i]=buffer[i];	
			}
			if(1)//假设设备正常连接
			{
				data_Anal(tcpecho_recvbuf);
			}
			else//假设设备被断开
			{
				mymemset(tcpecho_recvbuf,0x00,138);
				data_Anal(tcpecho_recvbuf);
			}
			//printf("recv data len = %d\r\n", readcount);
			return 0;
		}
}
/****************************************************************************************************************/
/****************************************************************************************************************/
/**************************************************************
 * void chargen_init(void)
 *
 * This function initializes the chargen service. This function
 * may only be called either before or after tasking has started.
 **************************************************************/
//void chargen_init(void)
//{
//    sys_thread_new( CHARGEN_THREAD_NAME, chargen_thread, 0, CHARGEN_THREAD_STACKSIZE, TCPIP_THREAD_PRIO+1);  
//}
INT8U chargen_init(void)
{  	INT8U res;
	OS_CPU_SR cpu_sr;
	OS_ENTER_CRITICAL();	//关中断
    res = OSTaskCreate(chargen_thread,(void*)0,
		(OS_STK*)&CHARGEN_TASK_STK[CHARGEN_STK_SIZE-1],
		CHARGEN_PRIO); //创建TCP服务器线程
	OS_EXIT_CRITICAL();		//开中断
	return res;
}	
#endif /* LWIP_SOCKET */
/****************************************************************************************************************/
/****************************************************************************************************************/



///**************************************************************************/
//					//7E 01 00 27 0D 00 02 00 00 48 68 7E断开,通电
//					if(0)//relay_on_do_flag==1)
//					{
//						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
//						{
//							if (FD_ISSET(p_charcb->socket, &writeset))
//							{
//								int writecount;
//								u8 line[12]={0x7E,0x01,0x00,0x27,0x0D,0x00,0x02,0x00,0x00,0x48,0x68,0x7E};
//								char setchar = p_charcb->nextchar;
//								writecount=lwip_send(p_charcb->socket, line, 12, 0);
//								send_hbt_flag=0;
//								if (writecount< 0)
//								{
//									close_chargen(p_charcb);
//									break;
//								}
//							}
//						}
//					}
//					/**************************************************************************/
//					//7E 01 00 27 0C 00 02 00 00 88 55 7E吸合,断电
//					if(relay_off_do_flag1||relay_off_do_flag3||relay_off_do_flag5||relay_off_do_flag7||
//								relay_off_do_flag9||relay_off_do_flag11)
//					{
//						for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
//						{
//							if(p_charcb->cliaddr.sin_addr.s_addr == inet_addr("192.168.0.175"))
//							{
//								
//							}
//							if (FD_ISSET(p_charcb->socket, &writeset))
//							{
//								int writecount;
//								u8 line[12]={0x7E,0x01,0x00,0x27,0x0C,0x00,0x02,0x00,0x00,0x88,0x55,0x7E};
//								char setchar = p_charcb->nextchar;
//								writecount=lwip_send(p_charcb->socket, line, 12, 0);
//								send_hbt_flag=0;
//								if (writecount< 0)
//								{
//									close_chargen(p_charcb);
//									break;
//								}
//							}
//						}
//					}  

client客户端:

/*********************************** (C) 2019 COPYRIGHT ENGINEER ZHANG ***************************
* @file    tcp_client_demo.c
* @author  glassine.zhang
* @version V1.0.0
* @date    26-6-2018
* @brief   转换-Box
**************************************************************************************************/
//头文件的声明
#include "tcp_client_demo.h"
#include "lwip/opt.h"
#include "lwip_comm.h"
#include "lwip/lwip_sys.h"
#include "lwip/api.h"
#include "includes.h"
#include "key.h"
#include "sys.h"
#include "ssprotocol.h"
#include "iwdg.h"

//#include "lwip/tcp_impl.h"
//#include "lwip/def.h"
//#include "lwip/mem.h"
//#include "lwip/memp.h"
//#include "lwip/ip_addr.h"
//#include "lwip/netif.h"
//#include "lwip/inet_chksum.h"
//#include "lwip/stats.h"
//#include "lwip/snmp.h"


struct netconn *tcp_clientconn;					//TCP CLIENT网络连接结构体
//struct netconn *tcp_clientconn2;					//TCP CLIENT网络连接结构体

u8 tcp_client_recvbuf[TCP_CLIENT_RX_BUFSIZE];	//TCP客户端接收数据缓冲区
//u8 tcp_client_recvbuf2[TCP_CLIENT_RX_BUFSIZE];	//TCP客户端接收数据缓冲区

u8 tcp_client_sendbuf[TCP_CLIENT_TX_BUFSIZE];
/*********************************************/
u8 tcp_client_sendbuf1[85];
u8 tcp_client_sendbuf2[25];

u8 tcp_client_flag;		//TCP客户端数据发送标志位
u8 tcp_client_flag2;		//TCP客户端数据发送标志位

extern u8 client_link_ok;//自定义客户端连接成功标志
extern u8 client_one_count;
//test
extern u8 relay_off_data_do_flag1;

extern OS_EVENT * sem_io;
extern OS_EVENT * sem_io3;

extern LOCAL_PARA localPara;


extern u8 send_err_count;

extern u8 TCP_SEND_OK;
/******************************************************/
/******************************************************/
extern u8	send_data_plc_flag1;
extern u8	send_data_plc_flag3;
extern u8	send_data_plc_flag5;
extern u8	send_data_plc_flag7;
extern u8	send_data_plc_flag9;
extern u8	send_data_plc_flag11;

extern u8	send_pwd_plc_flag2;
extern u8	send_pwd_plc_flag4;
extern u8	send_pwd_plc_flag6;
extern u8	send_pwd_plc_flag8;
extern u8	send_pwd_plc_flag10;
extern u8	send_pwd_plc_flag12;
/******************************************************/
/******************************************************/
/***************************/

/***************************/
extern u8 pwd_send_sure2;
extern u8 pwd_send_sure4;
extern u8 pwd_send_sure6;
extern u8 pwd_send_sure8;
extern u8 pwd_send_sure10;
extern u8 pwd_send_sure12;
/***************************/

/***************************/
extern u8 io_send_sure1;
extern u8 io_send_sure3;
extern u8 io_send_sure5;
extern u8 io_send_sure7;
extern u8 io_send_sure9;
extern u8 io_send_sure11;

//#define DATASUM_TASK_PRIO 		12

/********************************************************/
/********************************************************/
//TCP客户端任务
#define TCPCLIENT_PRIO			6
//任务堆栈大小
#define TCPCLIENT_STK_SIZE	512
//任务堆栈
OS_STK TCPCLIENT_TASK_STK[TCPCLIENT_STK_SIZE];
/********************************************************/
/********************************************************/
//tcp客户端任务函数
static void tcp_client_thread(void *arg)
{
	OS_CPU_SR cpu_sr;
	u32 data_len = 0;
	struct pbuf *q;
	err_t err,recv_err;
	static ip_addr_t server_ipaddr,loca_ipaddr;
	static u16_t 		 server_port,loca_port;
	u8 err1;
	u8 err2;
	u8 a;
	LWIP_UNUSED_ARG(arg);
//	server_port = REMOTE_PORT;
	/********************20170516  change server port**************************/
	server_port = localPara.serverPort;
	IP4_ADDR(&server_ipaddr, lwipdev.remoteip[0],lwipdev.remoteip[1], 
			lwipdev.remoteip[2],lwipdev.remoteip[3]);	
	/********************************************************************/
	//tcp_clientconn = netconn_new(NETCONN_TCP);  //创建一个TCP链接
	/********************************************************************/
	while (1) 
	{
		
		tcp_clientconn = netconn_new(NETCONN_TCP);  //创建一个TCP链接
		err = netconn_connect(tcp_clientconn,&server_ipaddr,server_port);//连接服务器
		if(err != ERR_OK)  netconn_delete(tcp_clientconn); //返回值不等于ERR_OK,删除tcp_clientconn连接
		else if (err == ERR_OK)    //处理新连接的数据
		{ 
			struct netbuf *recvbuf;
			tcp_clientconn->recv_timeout = 2;
			netconn_getaddr(tcp_clientconn,&loca_ipaddr,&loca_port,1); //获取本地IP主机IP地址和端口号
			printf("连接上服务器%d.%d.%d.%d,本机端口号为:%d\r\n",lwipdev.remoteip[0],lwipdev.remoteip[1], 
							lwipdev.remoteip[2],lwipdev.remoteip[3],loca_port);
			if(client_one_count==1)
			{
				client_link_ok=1;
				client_one_count=0;
			}
			else
			{
				client_link_ok=0;
				client_one_count=0;
			}
			/***********************************************************************************/
			while(1)
			{
			  //osDelay(15);
				
				if((tcp_client_flag & LWIP_SEND_DATA) == LWIP_SEND_DATA) //有数据要发送
				{
					//delay_us(20);
					IWDG_Feed();//喂狗
					OSSemPend(sem_io,0,&err1);
					//发送tcp_client_sendbuf中的数据
					//printf("buffLen大小1:%d\r\n",buffLen);
					//OS_ENTER_CRITICAL(); //关中断
					TCP_SEND_OK=0;
					
					/**************************************************************/
					//如果有问题,给我毙掉//003F
					/**************************************************************/
					//00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 
					//50 00 00 FF FF 03 00 10 00 10 00 01 14 00 00 C0 10 00 B4 02 00 06 C0 30 00//
					//50 00 00 FF FF 03 00 10 00 10 00 01 14 00 00 C2 10 00 B4 02 00 80 01 00 0C//
					//50 00 00 FF FF 03 00 10 00 10 00 01 14 00 00 C4 10 00 B4 02 00 01 60 1C 00//
					
					//50 00 00 FF FF 03 00 10 00 10 00 01 14 00 00 C6 10 00 B4 02 00 00 00 00 00//
					//50 00 00 FF FF 03 00 10 00 10 00 01 14 00 00 C8 10 00 B4 02 00 00 00 00 00//
					//50 00 00 FF FF 03 00 10 00 10 00 01 14 00 00 CA 10 00 B4 02 00 00 00 00 00//
					/**************************************************************/
					//如果有问题,给我毙掉
					/**************************************************************/
					//50 00 00 FF FF 03 00 4C 00 10 00 01 14 00 00 00 10 00 B4 20 00 FF FF FF FF//
					//50 00 00 FF FF 03 00 4C 00 10 00 01 14 00 00 20 10 00 B4 20 00 3F 00 3F 00//
					//50 00 00 FF FF 03 00 4C 00 10 00 01 14 00 00 40 10 00 B4 20 00 3F 00 FF FF//
					
					//50 00 00 FF FF 03 00 4C 00 10 00 01 14 00 00 60 10 00 B4 20 00 3F 00 3F 00//
					//50 00 00 FF FF 03 00 4C 00 10 00 01 14 00 00 80 10 00 B4 20 00 3F 00 3F 00//
					//50 00 00 FF FF 03 00 4C 00 10 00 01 14 00 00 A0 10 00 B4 20 00 3F 00 3F 00//
					/*####################################################################*/
					if(pwd_send_sure2==1)
					{
						pwd_send_sure2=0;
						if((tcp_client_sendbuf2[15]==0xC0)&&(tcp_client_sendbuf2[16]==0x10))
						{
							/
							if((tcp_client_sendbuf2[21]==0x80)||(tcp_client_sendbuf2[21]==0x01)||(tcp_client_sendbuf2[21]==0xFF)||
								(tcp_client_sendbuf2[21]==0x3F)||(tcp_client_sendbuf2[21]==0x11))
							{
								TCP_SEND_OK=1;
								tcp_client_flag &= ~LWIP_SEND_DATA;
								printf("2号异形态\r\n");
							}
							/
							else
							{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf2,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA; 
								//printf("2号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("2 different\r\n");
						}
					}
					/*####################################################################*/
					else if(pwd_send_sure4==1)
					{
						pwd_send_sure4=0;
						if((tcp_client_sendbuf2[15]==0xC2)&&(tcp_client_sendbuf2[16]==0x10))
						{
								/
								if((tcp_client_sendbuf2[21]==0x06)||(tcp_client_sendbuf2[21]==0x01)||(tcp_client_sendbuf2[21]==0xFF)||
									(tcp_client_sendbuf2[21]==0x3F)||(tcp_client_sendbuf2[21]==0x11))
								{
									TCP_SEND_OK=1;
									tcp_client_flag &= ~LWIP_SEND_DATA;
									printf("4号异形态\r\n");
								}
								/
								else
								{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf2,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA; 
								//printf("4号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("4 different\r\n");
						}
					}
					/*####################################################################*/
					else if(pwd_send_sure6==1)
					{
						pwd_send_sure6=0;
						if((tcp_client_sendbuf2[15]==0xC4)&&(tcp_client_sendbuf2[16]==0x10))
						{
							/
							if((tcp_client_sendbuf2[21]==0x06)||(tcp_client_sendbuf2[21]==0x80)||(tcp_client_sendbuf2[21]==0xFF)||
								(tcp_client_sendbuf2[21]==0x3F)||(tcp_client_sendbuf2[21]==0x11))
							{
								TCP_SEND_OK=1;
								tcp_client_flag &= ~LWIP_SEND_DATA;
								printf("6号异形态\r\n");
							}
							/
							else
							{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf2,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA; 
								//printf("6号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("6 different\r\n");
						}
					}
					/*####################################################################*/
					else if(pwd_send_sure8==1)
					{
						pwd_send_sure8=0;
						if((tcp_client_sendbuf2[15]==0xC6)&&(tcp_client_sendbuf2[16]==0x10))
						{
							/
							if((tcp_client_sendbuf2[21]==0xFF)||(tcp_client_sendbuf2[21]==0x3F))
							{
								TCP_SEND_OK=1;
								tcp_client_flag &= ~LWIP_SEND_DATA;
								printf("8号异形态\r\n");
							}
							/
							else
							{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf2,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA; 
								//printf("8号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("8 different\r\n");
						}
					}
					/*####################################################################*/
					else if(pwd_send_sure10==1)
					{
						pwd_send_sure10=0;
						if((tcp_client_sendbuf2[15]==0xC8)&&(tcp_client_sendbuf2[16]==0x10))
						{
							/
							if((tcp_client_sendbuf2[21]==0xFF)||(tcp_client_sendbuf2[21]==0x3F))
							{
								TCP_SEND_OK=1;
								tcp_client_flag &= ~LWIP_SEND_DATA;
								printf("10号异形态\r\n");
							}
							/
							else
							{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf2,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA; 	
								//printf("10号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("10 different\r\n");
						}
					}
					/*####################################################################*/
					else if(pwd_send_sure12==1)
					{
						pwd_send_sure12=0;
						if((tcp_client_sendbuf2[15]==0xCA)&&(tcp_client_sendbuf2[16]==0x10))
						{
							/
							if((tcp_client_sendbuf2[21]==0xFF)||(tcp_client_sendbuf2[21]==0x3F))
							{
								TCP_SEND_OK=1;
								tcp_client_flag &= ~LWIP_SEND_DATA;
								printf("12号异形态\r\n");
							}
							/
							else
							{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf2,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA; 	
								//printf("12号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("12 different\r\n");
						}
					}
					/*####################################################################*/
					/*####################################################################*/
					/*####################################################################*/
					else if(io_send_sure1==1)
					{
						io_send_sure1=0;
						if((tcp_client_sendbuf1[15]==0x00)&&(tcp_client_sendbuf1[16]==0x10))
						{
							/
							if(tcp_client_sendbuf1[21]!=0xFF)
							{
								TCP_SEND_OK=1;
								tcp_client_flag &= ~LWIP_SEND_DATA;
								printf("1号异形态\r\n");
							}
							/
							else
							{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf1,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA; 	
								//printf("1号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("1 different\r\n");
						}
					}
					/*####################################################################*/
					else if(io_send_sure3==1)
					{
						io_send_sure3=0;
						if((tcp_client_sendbuf1[15]==0x20)&&(tcp_client_sendbuf1[16]==0x10))
						{
							/
							if((tcp_client_sendbuf1[21]==0x06)||(tcp_client_sendbuf1[21]==0x80)||(tcp_client_sendbuf1[21]==0x01)||
								(tcp_client_sendbuf1[21]==0xFF)||(tcp_client_sendbuf1[21]==0x00)||(tcp_client_sendbuf1[21]==0x11))
							{
								TCP_SEND_OK=1;
								tcp_client_flag &= ~LWIP_SEND_DATA;
								printf("3号异形态\r\n");
							}
							/
							else
							{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf1,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA; 	
								//printf("3号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("3 different\r\n");
						}
					}
					/*####################################################################*/
					else if(io_send_sure5==1)
					{
						io_send_sure5=0;
						if((tcp_client_sendbuf1[15]==0x40)&&(tcp_client_sendbuf1[16]==0x10))
						{
							/
							if((tcp_client_sendbuf1[21]==0x06)||(tcp_client_sendbuf1[21]==0x80)||(tcp_client_sendbuf1[21]==0x01)||
								(tcp_client_sendbuf1[21]==0xFF)||(tcp_client_sendbuf1[21]==0x00)||(tcp_client_sendbuf1[21]==0x11))
							{
								TCP_SEND_OK=1;
								tcp_client_flag &= ~LWIP_SEND_DATA;
								printf("5号异形态\r\n");
							}
							/
							else
							{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf1,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA; 
								//printf("5号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("5 different\r\n");
						}
					}
					/*####################################################################*/
					else if(io_send_sure7==1)
					{
						io_send_sure7=0;
						if((tcp_client_sendbuf1[15]==0x60)&&(tcp_client_sendbuf1[16]==0x10))
						{
							/
							if((tcp_client_sendbuf1[21]==0x06)||(tcp_client_sendbuf1[21]==0x80)||(tcp_client_sendbuf1[21]==0x01)||
								(tcp_client_sendbuf1[21]==0xFF)||(tcp_client_sendbuf1[21]==0x00)||(tcp_client_sendbuf1[21]==0x11))
							{
								TCP_SEND_OK=1;
								tcp_client_flag &= ~LWIP_SEND_DATA;
								printf("7号异形态\r\n");
							}
							/
							else
							{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf1,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA;
								//printf("7号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("7 different\r\n");
						}
					}
					/*####################################################################*/
					else if(io_send_sure9==1)
					{
						io_send_sure9=0;
						if((tcp_client_sendbuf1[15]==0x80)&&(tcp_client_sendbuf1[16]==0x10))
						{
							/
							if((tcp_client_sendbuf1[21]==0x06)||(tcp_client_sendbuf1[21]==0x80)||(tcp_client_sendbuf1[21]==0x01)||
								(tcp_client_sendbuf1[21]==0xFF)||(tcp_client_sendbuf1[21]==0x00)||(tcp_client_sendbuf1[21]==0x11))
							{
								TCP_SEND_OK=1;
								tcp_client_flag &= ~LWIP_SEND_DATA;
								printf("9号异形态\r\n");
							}
							/
							else
							{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf1,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA; 	
								//printf("9号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("9 different\r\n");
						}
					}
					/*####################################################################*/
					else if(io_send_sure11==1)
					{
						io_send_sure11=0;
						if((tcp_client_sendbuf1[15]==0xA0)&&(tcp_client_sendbuf1[16]==0x10))
						{
							/
							if((tcp_client_sendbuf1[21]==0x06)||(tcp_client_sendbuf1[21]==0x80)||(tcp_client_sendbuf1[21]==0x01)||
								(tcp_client_sendbuf1[21]==0xFF)||(tcp_client_sendbuf1[21]==0x00)||(tcp_client_sendbuf1[21]==0x11))
							{
								TCP_SEND_OK=1;
								tcp_client_flag &= ~LWIP_SEND_DATA;
								printf("11号异形态\r\n");
							}
							/
							else
							{
								if(buffLen > 0)
								{	
									err = netconn_write(tcp_clientconn ,tcp_client_sendbuf1,buffLen,NETCONN_COPY); 						
								}
								buffLen = 0;
								if(err == ERR_OK)
								{
									TCP_SEND_OK=1;
								}
								else if(err != ERR_OK)
								{
									send_err_count++;
									TCP_SEND_OK=1;
								}
								tcp_client_flag &= ~LWIP_SEND_DATA; 
								//printf("11号发OK\r\n");							
							}
						}
						else
						{
							TCP_SEND_OK=1;
							tcp_client_flag &= ~LWIP_SEND_DATA;
							printf("11 different\r\n");
						}
					}
					/*####################################################################*/
					else
					{
						if(buffLen > 0)
						{
							//err = netconn_write(tcp_clientconn ,tcp_client_sendbuf,buffLen,NETCONN_MORE);		
							//err = netconn_write(tcp_clientconn ,tcp_client_sendbuf,buffLen,NETCONN_NOCOPY);		
							err = netconn_write(tcp_clientconn ,tcp_client_sendbuf,buffLen,NETCONN_COPY); 						
						}
						buffLen = 0;
						if(err == ERR_OK)
						{
							//netconn_delete(tcp_clientconn); 
							//printf("发送成功\r\n");
							TCP_SEND_OK=1;
						}
						else if(err != ERR_OK)
						{
							printf("发送失败\r\n");
							send_err_count++;
							TCP_SEND_OK=1;
						}
						tcp_client_flag &= ~LWIP_SEND_DATA;
						TCP_SEND_OK=1;
					}
					/*####################################################################*/
				}
//					if(buffLen > 0)
//						{
//							//err = netconn_write(tcp_clientconn ,tcp_client_sendbuf,buffLen,NETCONN_MORE);		
//							//err = netconn_write(tcp_clientconn ,tcp_client_sendbuf,buffLen,NETCONN_NOCOPY);		
//							err = netconn_write(tcp_clientconn ,tcp_client_sendbuf,buffLen,NETCONN_COPY); 						
//						}
//						buffLen = 0;
//						if(err == ERR_OK)
//						{
//							//netconn_delete(tcp_clientconn); 
//							//printf("发送成功\r\n");
//							TCP_SEND_OK=1;
//						}
//						else if(err != ERR_OK)
//						{
//							printf("发送失败\r\n");
//							send_err_count++;
//							TCP_SEND_OK=1;
//						}
//						tcp_client_flag &= ~LWIP_SEND_DATA;
//					}
//	
//	
//					
				if((recv_err = netconn_recv(tcp_clientconn,&recvbuf)) == ERR_OK)  //接收到数据
				{	
					/***************20170518  suspend the send task***************************///挂起定时发送传感器状态任务
					//OSTaskSuspend(DATASUM_TASK_PRIO);	
					/*************************************20170518  suspend the send task  end********************************************///
					OS_ENTER_CRITICAL(); //关中断
					memset(tcp_client_recvbuf,0,TCP_CLIENT_RX_BUFSIZE);  //数据接收缓冲区清零
					for(q=recvbuf->p;q!=NULL;q=q->next)  //遍历完整个pbuf链表
					{
						if(q->len > (TCP_CLIENT_RX_BUFSIZE-data_len)) memcpy(tcp_client_recvbuf+data_len,q->payload,(TCP_CLIENT_RX_BUFSIZE-data_len));//拷贝数据
						else memcpy(tcp_client_recvbuf+data_len,q->payload,q->len);
						data_len += q->len;  	
						if(data_len > TCP_CLIENT_RX_BUFSIZE) break; //超出TCP客户端接收数组,跳出	
					}
					OS_EXIT_CRITICAL();  //开中断
					data_len=0;  //复制完成后data_len要清零。
					plc_data_Anal(tcp_client_recvbuf);
					/***********************process the received Data 20170517 end**********************************************************/
					netbuf_delete(recvbuf);
					/*************************************20170518  resume the send task********************************************/
					//OSTaskResume(DATASUM_TASK_PRIO);
					/*************************************20170518  resume the send task  end********************************************/
				}
				else if(recv_err == ERR_CLSD)  //关闭连接
				{
					netconn_close(tcp_clientconn);
					netconn_delete(tcp_clientconn);
					printf("服务器%d.%d.%d.%d断开连接\r\n",lwipdev.remoteip[0],lwipdev.remoteip[1], lwipdev.remoteip[2],lwipdev.remoteip[3]);
					break;
				}				
			OSTimeDlyHMSM(0,0,0,10);  //延时200ms
      }
		}
		OSTimeDlyHMSM(0,0,0,10);  //延时200ms
	}
	
}
/********************************************************/
/********************************************************/
//创建TCP客户端线程
//返回值:0 		TCP客户端创建成功
//		其他 	TCP客户端创建失败
INT8U tcp_client_init(void)
{
	INT8U res;
	OS_CPU_SR cpu_sr;
	OS_ENTER_CRITICAL();	//关中断
	res = OSTaskCreate(tcp_client_thread,(void*)0,(OS_STK*)&TCPCLIENT_TASK_STK[TCPCLIENT_STK_SIZE-1],TCPCLIENT_PRIO); //创建TCP客户端线程
	OS_EXIT_CRITICAL();		//开中断
	return res;
}

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页