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

📄 netcom.c

📁 移植到51单片机的UIP代码
💻 C
字号:
#include "uip.h"
#include "string.h"
#include "STC89C58RD+ FLASH-ISP-IAP.H"
#include "modbus.h"
#include "uip_arp.h"
#include "stdio.h"
#include "netcom.h"
#include <reg52.h>

COM_BUF ComBuf;
PARA_SET ParaSet;
u8 MS_TIMER;
u8 Second;

sfr P4    = 0xe8;
sbit P4_0 = P4^0;

bit SoftResetFlag=0;
u8 SoftRstSec;

static u8 TcpStep=0;
static bit ReConnect=0;

void SoftReset (void)
{
	((void (code *) (void)) 0x0000) ();
}

void ParaInit( void )
{
	u16 crc;
	P4_0 = 1;
	sequential_read_flash_in_one_sector(0x8000,sizeof(PARA_SET),(u8*)&ParaSet);
	crc = CalCRC( (u8*)&ParaSet, sizeof(PARA_SET)-2 );
	if( crc != ParaSet.CrcVerify || !P4_0 )
	{
		ParaSet.LocalIP[0] = 192;
		ParaSet.LocalIP[1] = 168;
		ParaSet.LocalIP[2] = 1;
		ParaSet.LocalIP[3] = 10;

		ParaSet.GateWay[0] = 192;
		ParaSet.GateWay[1] = 168;
		ParaSet.GateWay[2] = 1;
		ParaSet.GateWay[3] = 1;

		ParaSet.NetMask[0] = 255;
		ParaSet.NetMask[1] = 255;
		ParaSet.NetMask[2] = 255;
		ParaSet.NetMask[3] = 0;

		ParaSet.MacAddr[0] = 0xab;
		ParaSet.MacAddr[1] = 0xcd;
		ParaSet.MacAddr[2] = 0xef;
		ParaSet.MacAddr[3] = 0x12;
		ParaSet.MacAddr[4] = 0x34;
		ParaSet.MacAddr[5] = 0x56;

		ParaSet.TargetIP[0] = 192;
		ParaSet.TargetIP[1] = 168;
		ParaSet.TargetIP[2] = 1;
		ParaSet.TargetIP[3] = 168;

		ParaSet.LocalPort = 1000;

		ParaSet.TargetPort = 2000;

		ParaSet.BaudRate = 19200;
		ParaSet.VerifyBit = 0;
		ParaSet.DataBit = 8;
		ParaSet.StopBit = 1;
		ParaSet.FrameTime = 2;

		ParaSet.WorkMode = MODE_TCP_CLIENT;

		crc = CalCRC( (u8*)&ParaSet, sizeof(PARA_SET)-2 );
		ParaSet.CrcVerify = crc;
		sequential_write_flash_in_one_sector(0x8000,sizeof(PARA_SET),(u8*)&ParaSet);
	}
}

u32 SwapLong( u32 temp )
{
	u8 ch,cl;
	u16 ih,il;
	PC2MCU51 *Pc2Mcu51Ptr;
	Pc2Mcu51Ptr = (PC2MCU51*)&temp;
	cl = Pc2Mcu51Ptr->L.L2I.IL.CHAR.CL;
	ch = Pc2Mcu51Ptr->L.L2I.IL.CHAR.CH;

	Pc2Mcu51Ptr->L.L2I.IL.CHAR.CL = ch;
	Pc2Mcu51Ptr->L.L2I.IL.CHAR.CH = cl;

	cl = Pc2Mcu51Ptr->L.L2I.IH.CHAR.CL;
	ch = Pc2Mcu51Ptr->L.L2I.IH.CHAR.CH;

	Pc2Mcu51Ptr->L.L2I.IH.CHAR.CL = ch;
	Pc2Mcu51Ptr->L.L2I.IH.CHAR.CH = cl;

	il = Pc2Mcu51Ptr->L.L2I.IL.b;
	ih = Pc2Mcu51Ptr->L.L2I.IH.c;

	Pc2Mcu51Ptr->L.L2I.IL.b = ih;
	Pc2Mcu51Ptr->L.L2I.IH.c = il;
	
	return temp;
}

void ComBufInit( void )
{
	ComBuf.SendPtr = 0;
	ComBuf.SendLen = 0;
	ComBuf.SendFlag = 0;

	ComBuf.RecvPtr = 0;
	ComBuf.RecvLen = 0;
	ComBuf.RecvFlag = 0;
	ComBuf.Com2NetFlag = 0;
	ComBuf.RstRecvFlag = 0;
}

void ComInit(void)
{
	u32 baud;
	baud = SwapLong( ParaSet.BaudRate );
	switch( baud )
	{
		case 110:
			RCAP2H = 0xe7;
			RCAP2L = 0x76;
			break;
		case 150:
			RCAP2H = 0xee;
			RCAP2L = 0x01;
			break;
		case 200:
			RCAP2H = 0xf2;
			RCAP2L = 0x81;
			break;
		case 300:
			RCAP2H = 0xf7;
			RCAP2L = 0x01;
			break;
		case 600:
			RCAP2H = 0xfb;
			RCAP2L = 0x81;
			break;
		case 1200:
			RCAP2H = 0xfd;
			RCAP2L = 0xc0;
			break;
		case 2400:
			RCAP2H = 0xfe;
			RCAP2L = 0xe0;
			break;
		case 4800:
			RCAP2H = 0xff;
			RCAP2L = 0x70;
			break;
		case 7200:
			RCAP2H = 0xff;
			RCAP2L = 0xa0;
			break;
		case 9600:
			RCAP2H = 0xff;
			RCAP2L = 0xb8;
			break;
		case 14400:
			RCAP2H = 0xff;
			RCAP2L = 0xd0;
			break;
		case 19200:
			RCAP2H = 0xff;
			RCAP2L = 0xdc;
			break;
		case 28800:
			RCAP2H = 0xff;
			RCAP2L = 0xe8;
			break;
		case 38400:
			RCAP2H = 0xff;
			RCAP2L = 0xee;
			break;
		case 57600:
			RCAP2H = 0xff;
			RCAP2L = 0xf4;
			break;
		case 76800:
			RCAP2H = 0xff;
			RCAP2L = 0xf7;
			break;
		case 115200:
			RCAP2H = 0xff;
			RCAP2L = 0xfa;
			break;
		case 230400:
			RCAP2H = 0xff;
			RCAP2L = 0xfd;
			break;
		default:
			RCAP2H = 0xff;		//9600
			RCAP2L = 0xb8;
			break;
	}
	switch( ParaSet.VerifyBit )
	{
		case VERY_NONE:
			SCON = 0x50;
			break;
		case VERY_EVEN:
		case VERY_ODD:
		case VERY_LOW:
		case VERY_HIGH:
			SCON = 0xd0;
			break;
		default:
			SCON = 0x50;
			break;
	}
	RCLK = 1;
	TCLK = 1;
	TR2 = 1;
	PS = 1;
	ES = 1;

	ComBufInit();
}

void Timer1Init( void )
{
	TH1 = T1_RELOAD >> 8;
    TL1 = T1_RELOAD;
	TMOD |= 0x10;
	ET1 = 1;
	TR1 = 1;
}

void NetcomInit( void )
{
    struct uip_udp_conn *udp_conn;
	struct uip_conn *tcp_conn;
	struct uip_eth_addr MacAddr;

    u16_t ipaddr[2];//定义IP类型变量

	ParaInit();
	ComInit();
	Timer1Init();

	uip_ipaddr(ipaddr, ParaSet.NetMask[0],ParaSet.NetMask[1],ParaSet.NetMask[2],ParaSet.NetMask[3]);
	uip_ipaddr(ipaddr, 255,255,255,0);

	uip_ipaddr(ipaddr, ParaSet.LocalIP[0],ParaSet.LocalIP[1],ParaSet.LocalIP[2],ParaSet.LocalIP[3]);
	uip_sethostaddr( ipaddr );

	//参数设置UDP端口
	uip_ipaddr(ipaddr, ParaSet.TargetIP[0],ParaSet.TargetIP[1],ParaSet.TargetIP[2],ParaSet.TargetIP[3]);
    udp_conn = uip_udp_new(&ipaddr, HTONS(2010));
	uip_udp_bind(udp_conn,HTONS(2008));

	MacAddr.addr[0] = ParaSet.MacAddr[0];
	MacAddr.addr[1] = ParaSet.MacAddr[1];
	MacAddr.addr[2] = ParaSet.MacAddr[2];
	MacAddr.addr[3] = ParaSet.MacAddr[3];
	MacAddr.addr[4] = ParaSet.MacAddr[4];
	MacAddr.addr[5] = ParaSet.MacAddr[5];
	uip_setethaddr(MacAddr);

	ParaSet.WorkMode = MODE_TCP_CLIENT;
	switch( ParaSet.WorkMode )
	{
		case MODE_TCP_CLIENT:
			uip_ipaddr(ipaddr, ParaSet.TargetIP[0],ParaSet.TargetIP[1],ParaSet.TargetIP[2],ParaSet.TargetIP[3]);
			tcp_conn=uip_connect(ipaddr, HTONS(ParaSet.TargetPort));
			tcp_conn->lport = HTONS(ParaSet.LocalPort);
			break;
		default:
			while(1);
	}
}

void Timer1_ISR( void ) interrupt 3 using 2
{
	static u16 time=0;
	TH1 = T1_RELOAD >> 8;
    TL1 = T1_RELOAD;
	MS_TIMER++;
	if( ComBuf.RecvTime>0 )
		ComBuf.RecvTime--;
	if( time++>1000 )
	{
		time = 0;
		if( Second>0 )
			Second--;
		if( SoftRstSec>0 )
			SoftRstSec--;
	}
}

void Com_ISR( void ) interrupt 4 using 3
{
	u8 temp;
	bit verfflag;
	if( RI )
	{
		RI = 0;
		temp = SBUF;
		if( ParaSet.DataBit==9 )
		{
			ACC = temp;
			switch( ParaSet.VerifyBit )
			{
				case VERY_EVEN:
					if(RB8 == P)
						verfflag = 1;
					else
						verfflag = 0;
					break;
				case VERY_ODD:
					if(RB8 == ~P)
						verfflag = 1;
					else
						verfflag = 0;
					break;
				case VERY_LOW:
					if(RB8 == 0)
						verfflag = 1;
					else
						verfflag = 0;
					break;
				case VERY_HIGH:
					if(RB8 == 1)
						verfflag = 1;
					else
						verfflag = 0;
					break;
				default:
					break;
			}
		}
		else
			verfflag = 1;

		if( ComBuf.RecvPtr<RECVBUF_LEN && verfflag )
		{
			ComBuf.RecvBuf[ComBuf.RecvPtr++] = temp;
			ComBuf.RecvTime = ParaSet.FrameTime;
			ComBuf.RstRecvFlag = 1;
		}
	}
	if( TI )
	{
		TI = 0;
		if( ComBuf.SendPtr<ComBuf.SendLen && ComBuf.SendFlag )
		{
			temp = ComBuf.SendBuf[ComBuf.SendPtr++];
			SBUF = temp;
			ACC = temp;
			if( ParaSet.DataBit==9 )
			{
				switch( ParaSet.VerifyBit )
				{
					case VERY_EVEN:
						TB8 = P;
						break;
					case VERY_ODD:
						TB8 = ~P;
						break;
					case VERY_LOW:
						TB8 = 0;
						break;
					case VERY_HIGH:
						TB8 = 1;
						break;
					default:
						break;
				}
				ComBuf.SendPtr++;
			}
		}
		else
			ComBuf.SendFlag = 0;
	}
}

//return 0:error return 1:ok
bit WriteSendBuf( u8 *ptr, u16 len )
{
	u16 i;
	if( ComBuf.SendFlag )
		return 0;
	if( len>SENDBUF_LEN )
		len = SENDBUF_LEN;

	for( i=0; i<len; i++ )
	{
		ComBuf.SendBuf[i] = ptr[i];
	}
	ComBuf.SendLen = len;
	ComBuf.SendPtr = 0;
	ComBuf.SendFlag = 1;
	TI = 1;
	return 1;
}

void uip_udp_appcall( void )
{
	u16 crc;
	PARA_SET *ParasetPtr;
	if(uip_poll()) 
	{
    }	
	if( uip_udp_conns->lport == HTONS(2008) )
	{
		if(uip_newdata()) //如果指定IP的指定端口发来数据
		{
			ParasetPtr = (PARA_SET*)uip_appdata;
			crc = CalCRC( (u8*)uip_appdata, sizeof(PARA_SET)-2 );
			if( crc==ParasetPtr->CrcVerify )
			{
				sequential_write_flash_in_one_sector(0x8000,sizeof(PARA_SET),uip_appdata);
				memcpy(uip_appdata,"SUCCESS",7); 
	 			uip_send("SUCCESS",7);
				SoftResetFlag = 1;
				SoftRstSec = 5;
			}
			else if( *uip_appdata=='R' && *(uip_appdata+1)=='E' && *(uip_appdata+2)=='A' && *(uip_appdata+3)=='D' )
			{
				memcpy(uip_appdata,(u8*)&ParaSet,sizeof(PARA_SET)); 
	 			uip_send((u8*)uip_appdata,sizeof(PARA_SET));
			}
			else if( *uip_appdata=='R' && *(uip_appdata+1)=='E' && *(uip_appdata+2)=='S' && *(uip_appdata+3)=='E' && *(uip_appdata+4)=='T' && *(uip_appdata+5)=='?' )
			{
				memcpy(uip_appdata,"RESETED",7); 
	 			uip_send((u8*)uip_appdata,7);
			}
		}
	}
}

void uip_appcall( void )
{
	struct uip_conn *tcp_conn;
	u16_t ipaddr[2];//定义IP类型变量

	if( ParaSet.WorkMode==MODE_TCP_CLIENT )
	{	
		switch( TcpStep )
		{
			case 0:
				if( uip_timedout() )
				{
					uip_close();
					uip_ipaddr(ipaddr, ParaSet.TargetIP[0],ParaSet.TargetIP[1],ParaSet.TargetIP[2],ParaSet.TargetIP[3]);
					tcp_conn=uip_connect(ipaddr, HTONS(ParaSet.TargetPort));
					tcp_conn->lport = HTONS(ParaSet.LocalPort);
					//WriteSendBuf("timeout",7);
				}
				if( uip_connected() )
				{
					TcpStep = 1;
					ReConnect = 0;
					//WriteSendBuf("success",7);
				}
				break;
			case 1:
				if(uip_newdata() || uip_rexmit()) 
				{
					//uip_send("hello tcp client",16);
					if( uip_conn->lport == HTONS(ParaSet.LocalPort) )
					{
						if( uip_len>SENDBUF_LEN )
							uip_len = SENDBUF_LEN;
						WriteSendBuf(uip_appdata, uip_datalen() );
					}
				}
				if( uip_closed() )
				{
					uip_close();
					TcpStep = 0;
					ReConnect = 1;
					Second = 5;
					//WriteSendBuf("closed",6);
					return;
				}
				if( uip_aborted() )
				{
					uip_abort();
					TcpStep = 0;
					ReConnect = 1;
					Second = 5;
					//WriteSendBuf("aborted",7);
					return;
				}
				if( ComBuf.Com2NetFlag )
				{
					ComBuf.Com2NetFlag = 0;
					uip_send(ComBuf.RecvBuf, ComBuf.RecvLen );
				}
				break;
			default:
				break;
		}
	}
}

void ComTask( void )
{
	if( ComBuf.RecvTime==0 && ComBuf.RstRecvFlag )
	{
		ComBuf.RstRecvFlag = 0;
		if( ComBuf.RecvPtr!=0 )
		{
			ComBuf.Com2NetFlag = 1;
			ComBuf.RecvLen = ComBuf.RecvPtr;
			ComBuf.RecvPtr = 0;
		}
	}
	if( SoftResetFlag && SoftRstSec==0 )
		SoftReset();
}

void TcpTask( void )
{
	struct uip_conn *tcp_conn;
	u16_t ipaddr[2];//定义IP类型变量

	if( ReConnect && Second==0 )
	{
		Second = 5;
		uip_ipaddr(ipaddr, ParaSet.TargetIP[0],ParaSet.TargetIP[1],ParaSet.TargetIP[2],ParaSet.TargetIP[3]);
		tcp_conn=uip_connect(ipaddr, HTONS(ParaSet.TargetPort));
		tcp_conn->lport = HTONS(ParaSet.LocalPort);
		//WriteSendBuf("reconnect ",10);
	}
}

⌨️ 快捷键说明

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