⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appdatatrans.c

📁 基于STC单片机与MICROCHIP ENC28J60的TCPIP以太网控制器
💻 C
字号:
#include "uip.h"
#include "appdatatrans.h"
 #include "reg51.h"
#define  DATANUM  200
#ifdef DEBUG
#include <stdio.h>
#define PRINT(x) printf("%s", x)
#define PRINTLN(x) printf("%s\n", x)
#else /* DEBUG */
#define PRINT(x)
#define PRINTLN(x)
#endif /* DEBUG */
   sbit P11 = P1^1;
   sbit P10 = P1^0;
#ifdef F_STATISTICS
#if F_STATISTICS == 1
#define BUG_NUM 50
u8_t xdata count_rx[DATANUM]={0};
static u8_t xdata count_tx[DATANUM]={0};
#endif /* F_STATISTICS */
#endif /* F_STATISTICS */

#define OVER 0
#define READY  1

#define TURNON  1
#define TURNOFF 0

#define ISO_G        0x47
#define ISO_E        0x45
#define ISO_T        0x54
struct datatrans_state *s;
static unsigned char UART_state;  			//串口发送状态
static unsigned int UART_len;					//串口数据长度
 unsigned char *ptr_tx;					//串口接收指针
unsigned int temp_len;					//串口接收数据长度
extern unsigned char  connect_state;
unsigned char ab_count;	 			//不同的计数值在做服务器和做客户端的时候
 //extern unsigned char tick_count;
unsigned char xdata STAT_AB;
//unsigned char xdata  UARTtimer;
void
file_init(void)
{
#ifdef F_STATISTICS
#if F_STATISTICS == 1
 // u8_t idata i;
  	s->count=0;	 temp_len=0;
	ptr_tx=&count_tx[0];
 	 STAT_AB=0;		
#endif /* F_STATISTICS */
#endif /* F_STATISTICS */
}


void
datatrans_init(void)
	{
	  file_init();
	  
	  /* Listen to port 80. HTTP WEB PORT 80*/
	  uip_listen(HTONS(1234));
	
	}
	
void
datatrans_appcall(void)

{
  u8_t idata i;
  u8_t xdata *dataptr;		 //串口发送指针

 if((uip_conn->lport==HTONS(1234))||(uip_conn->rport==HTONS(1280))) 
   /* Pick out the application state from the uip_conn structure. */
 	{   s = (struct datatrans_state xdata *)(uip_conn->appstate);
		 if(uip_conn->lport==HTONS(1234))
		 ab_count=40;
		 else ab_count=20;
    /* We use the uip_ test functions to deduce why we were
       called. If uip_connected() is non-zero, we were called
       because a remote host has connected to us. If
       uip_newdata() is non-zero, we were called because the
       remote host has sent us new data, and if uip_acked() is
       non-zero, the remote host has acknowledged the data we
       previously sent to it. */
      
	 if(uip_poll()) { 		  
	 			// if( (RI==0)&&(temp_len!=0))				//串口接收数据完毕
						  // 	{	delay();
				if( (RI==0)&&(temp_len!=0)&&count_tx[temp_len-1]==0xff)	
								{s->state=READY;  }	
								 
						   	// }
				if((s->state==READY)&&(temp_len!=0))		  //状态可删除 
					{  s->count=0;	//tick_count = 0;		 //add9
					if(temp_len>DATANUM)						 //可删除
					{temp_len=DATANUM;}
					 uip_slen=temp_len;					//从串口接收到的数据	add7
					uip_sappdata = &count_tx[0];				//add5	经过  
					s->state=OVER;
					temp_len=0;			   //串口接收到的数据已经发送出去,串口接收指针复位
					ptr_tx = &count_tx[0];

					uip_flags=UIP_ACKDATA;	  					//add8
					}
			if((s->count==0)){Timer1on();STAT_AB=1;} 
		    	else if(s->count == ab_count)					//每次完成一次传送就关闭
					 {	 										//不是此地
						uip_abort(); 
						 s->count=0; 
						 connect_state=TURNOFF;
						 }		   						//建立时间过长,连接关闭 	  
					return;		
					}
	else if(uip_newdata())		
			{
				 s->count=0;  	
				P10=~P10;					//add6
				uip_slen=uip_len;	
      			if(uip_slen<=DATANUM)
					{
					s->dataptr=uip_appdata;					 //add4
					for(i=0;i<uip_slen;i++)
							{	
								count_rx[i]=*(s->dataptr++);
							}
						uip_slen=0;			 					//important
 
						  UART_state=READY;	   				//add7
						  UART_len=uip_len;
					}
					if(UART_state==READY)  //确定为电脑发过来的数据,而不是要本机传数据给PC
					 {	 dataptr=&count_rx[0]; UART_state=OVER;	ES=0;
					 	while (UART_len--)
					 	{ TI=0;	
							SBUF=*dataptr++;
							while(!TI) ;	//串口发送数据
										   //可考虑将这段发送的移到poll()中
					 	}
						ES=1;
					 }
		   /**************************数据的复制********************		   
				if(s->state==READY){
						uip_slen=uip_len;							//add5
					for(i=0;i<uip_slen;i++)
					{  	count_tx[i]=count_rx[i];
					}
					uip_sappdata = &count_tx[0];				//add5	经过  
					s->state=OVER;
					}
				//else	{uip_flags=UIP_CLOSE;}	   		//add6	可以连续传数据		  */
				
			
			 
				return;	
				}
	else if(uip_connected()) {	
							//	s->state=READY;
						 	   
								 
					}
	else if(uip_acked())
			{ 	  
			
		
						return;
			}

	
	else if (uip_timedout())  	 {		P11=1;		  		//add6
							return;
	}
	else if(uip_close()||uip_stop()|| ( uip_rexmit()))
			{			  
							return;
				}	
	}						  
 else{			  //可删除
			  P11=~P11;
    /* Should never happen. */
  	uip_abort();	
    	return;	
	}
	  return;
}

/*******************串口程序********************************/
static void etherdev_UART_isr(void) interrupt 4 				//串口中断
	{	if(RI==1)					 					//add8
		 {
		RI=0;		ES=0;
		if(temp_len<DATANUM){
		*ptr_tx++=SBUF;
		temp_len++;	}	ES=1;
		return;		}
		TI=0;
		return;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -