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

📄 util.c

📁 利用51单片机对RTL8109进行控制
💻 C
📖 第 1 页 / 共 2 页
字号:
#ifndef _util_c
#define _util_c
#endif

#include "net.h"
#include "struct.h"
#include "fundef.h"
#include "variable.h"

unsigned char code hex[]={"0123456789ABCDEF"};

uchar get_char()
{
	uchar temp;
	temp=comrxdbuf[comrxdread];
	comrxdread++;
	if(comrxdread>=com_rxd_buffer_size)
	{
		comrxdread=0;
	}
	return(temp);

}

void send_char(unsigned char ascii)
 /*往串口发送一个字符  */
{
 	ea=0;
 	comtxdbuf[comtxdwrite]=ascii;
 	comtxdwrite++;
 	if(comtxdwrite>=com_txd_buffer_size)comtxdwrite=0;
	if(comtxdbufempty)
  	{
  		ti=1;
  	}
 	ea=1;
}

void send_hex(unsigned char senddata)
{
	unsigned char ch;
	ch=senddata>>4;
	send_char(hex[ch]);
	ch=senddata&0x0F;
	send_char(hex[ch]);

}

void send_string(unsigned char code *string)
//往串口发送一个字符串,字符串为存储在程序空间,碰到0x00结束
{
	while(*string!=0)
	{
		send_char(*string);
 		string++;
	}
}

uchar string_compare(unsigned char code *string,uchar number)
{
	//字符串比较,比较给定的字符串和命令缓冲区是否一样
	uchar i;
	uchar temp;
	for (i=0;i<number;i++)
	{
		temp=command_buffer[i];
		if(temp!=(*string))
		{
			return(0);
		}
		string++;
	}
	return(1);
}

void FunReadConfig(void)
{
	unsigned char temp;
	send_string("IP:");
	for(temp=0;temp<4;temp++)
	{
		send_hex(my_ip_address.bytes[temp]);
		if(temp==3){send_string("           ");}
		else{send_char('.');}
	}

	send_string("GATEWAY:");
	for(temp=0;temp<4;temp++)
	{
		send_hex(gateway_ip_address.bytes[temp]);
		if(temp==3){send_string("           ");}
		else{send_char('.');}
	}

	send_string("RIP:");
	for(temp=0;temp<4;temp++)
	{
		send_hex(ping_ip_address.bytes[temp]);
		if(temp==3){send_string("           ");}
		else{send_char('.');}
	}
	send_string("DVR:");
	for(temp=0;temp<4;temp++)
	{
		send_hex(dvr_ip_address.bytes[temp]);
		if(temp==3){send_string("           ");}
		else{send_char('.');}
	}
	send_string("DVR_READ:");
	//temp=c_rxcount>>8;
	send_hex(c_DvrReadBuffer);
	send_string("      ");
	send_hex(c_RstTcpCount);
	send_string("      ");

	send_string("DVR_WRITE:");
	//temp=c_txcount>>8;
	send_hex(c_DvrWriteBuffer);
	send_string("      ");
	send_hex(tcp_count);
}


void process_command()
{
	//处理用户从串口输入的命令
	uchar i;
	uchar j;
	uchar ip;
	uchar temp;
	unsigned int s_port=0;
	
	union ip_address_type temp_ip_address;

	command_length=0;

	while(comrxdread!=comcommandend)
	{
		pwdi=!pwdi;
	     	temp=get_char();
      		command_buffer[command_length]=temp;
    		if((temp!=' ')&&(temp!=0x0d))//表示不是空格和回车
    		{
			command_length++;
     			if(command_length>(command_buffer_size-2))command_length=0;
     		}
     		
      		if(temp==0x0d) //回车键
        	{
//        		pwdi=!pwdi;
////////////////////////////////////////设置本机相关参数
			if(string_compare("IP",2)!=0)
			{
				ip=0;
                          	//取得ip地址
                         	for(i=2,j=0;i<command_length;i++)
                         	{
                          		if(command_buffer[i]!='.')
                                 	{
                                   		command_buffer[i]=command_buffer[i]&0x0f;
                                      	        ip=ip*10;
                                               	ip=ip+command_buffer[i];
                                        }
                              		else
                              		{
                                        	my_ip_address.bytes[j]=ip;
                                        	my_ethernet_address.bytes[j+2]=ip;
                                        	if(j==3)break;
                                               	j++;ip=0;
                                   	}
                                }
                                if(j==3)
                                {
                                	EA=0;
                                	netcard_init();
                                	EA=1;
                                	
                                	pwdi=!pwdi;

                                	delay_ms(10);

                                	for(i=0;i<3;i++){txdnet.etherframe.sourcenodeid[i]=my_ethernet_address.words[i];}

                       		}
			}

			if(string_compare("MASK",4)!=0)
			{
				ip=0;
                          	//取得ip地址
                         	for(i=4,j=0;i<command_length;i++)
                         	{
                          		if(command_buffer[i]!='.')
                                 	{
                                   		command_buffer[i]=command_buffer[i]&0x0f;
                                      	        ip=ip*10;
                                               	ip=ip+command_buffer[i];
                                        }
                              		else
                              		{
                                        	mask_ip_address.bytes[j]=ip;
                                        	if(j==3)break;
                                               	j++;ip=0;
                                   	}
                                }
                       	}

			if(string_compare("GATE",4)!=0)
			{
				ip=0;
                          	//取得ip地址
                         	for(i=4,j=0;i<command_length;i++)
                         	{
                          		if(command_buffer[i]!='.')
                                 	{
                                   		command_buffer[i]=command_buffer[i]&0x0f;
                                      	        ip=ip*10;
                                               	ip=ip+command_buffer[i];
                                        }
                              		else
                              		{
                                        	gateway_ip_address.bytes[j]=ip;
                                        	if(j==3)break;
                                               	j++;ip=0;
                                   	}
                                }
                                if(j==3)
                                {
                                	gateway_ip_address_ttl=0;
                                	sec=0;
                                	gateway_arp_request();
                     	  	}
                       	}

                       	if(string_compare("TYPE",4)!=0)
                       	{
                       		c_type=command_buffer[4]&0x0f;
                       	}

                       	if(string_compare("READ",4)!=0)
                       	{
                       		FunReadConfig();
                       	}

//////////////////////////////////网络命令

			if(string_compare("RIP",3)!=0)
			{
				ip=0;
                          	//取得ip地址
                         	for(i=3,j=0;i<command_length;i++)
                         	{
                                	if(command_buffer[i]!='.')
                                 	{
                                 		/*
                                 		if(j==4)
                                 		{
                                 			command_buffer[i]=command_buffer[i]&0x0f;
                                      	     		s_port=s_port*10;
                                             	  	s_port=s_port+command_buffer[i];
                                 		}
                                 		else*/
                                 		{
                                   			command_buffer[i]=command_buffer[i]&0x0f;
                                      	     		ip=ip*10;
                                             	  	ip=ip+command_buffer[i];
                                               	}
                                        }
                              		else
                              		{
                              			/*
                                        	if(j==4)
                                        	{
                                        		socketnumber=s_port;

                                        		break;
                                        	}
                                        	else*/
                                        	{
                                        		ping_ip_address.bytes[j]=ip;
                                        	}
                                        	if(j==3)break;

                                               	j++;ip=0;
                                   	}
                                }

 				if(j==3)
                                {
                                	socketnumber=ReduceTcp_Port;

                                	local_use_port=ReduceTcp_Port;
                                	
                                	c_net_delay=2;

                                     	if((ping_ip_address.dwords&mask_ip_address.dwords)==(my_ip_address.dwords&mask_ip_address.dwords))
                                        {
        	                             	//表示位于同一子网.
        	                             	for(i=0;i<3;i++){ping_ethernet_address.words[i]=0xffff;}
                                     		//arp_request(ping_ip_address.dwords);
                                        }
                                        else
                                        {
                                        	//表示属于不同的子网,需要通过网关.
                                           	ping_ip_address_ttl=10;
                                                for(j=0;j<6;j++)
                                                {
                                           		ping_ethernet_address.bytes[j]=gateway_ethernet_address.bytes[j];
                                                }
                                          	if(gateway_ip_address_ttl==0)
                                                {
                                                        ping_ip_address_ttl=0;
                                                }
                                   	}
                       		}
			}

			if(string_compare("%",1)!=0)
			{
				txdnet.tcpframe.tcpdata[0]=0x55;
				txdnet.tcpframe.tcpdata[1]=0xaa;
				txdnet.tcpframe.tcpdata[2]=0xaa;
				for(i=1,j=4;i<command_length;i++,j++){txdnet.tcpframe.tcpdata[j]=command_buffer[i];}
				txdnet.tcpframe.tcpdata[j]='a';

				for(i=0;i<3;i++){txdnet.etherframe.destnodeid[i]=ping_ethernet_address.words[i];}

				create_tcp_packet(command_length+4);

⌨️ 快捷键说明

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