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

📄 input.c

📁 51单片机上实现tcp/ip的源代码
💻 C
字号:
/*********************************************************************
 *            Copright(c) 2004,张会福 湖南科技大学计算机学院 
 *                        All rights reserved.
 *
 *文件名称:    INPUT.C
 *文件标识: 
 *摘    要: 接收串口输入的信息,并进行“解析”,引发相应的操作
 *            1.输入命令:setgate xxx.xxx.xxx.xxx,并输入回车键后,将会按照输入的值设定网关地址
 *            2.输入命令:setip xxx.xxx.xxx.xxx,并输入回车键后,将会按照输入的值设定IP地址
 *            3.输入命令:ping xxx.xxx.xxx.xxx,并输入回车键后,将会引发ping操作
 *            4.输入命令:UDP xxx.xxx.xxx.xxx.1025.data,并输入回车键后,将会引发udp操作,其中,
 *            端口号为1025,端口号以前的内容为对方主机IP地址,1025后面的是待发送的数据.
 *当前版本: V1.0
 *作    者: 张会福
 *完成日期: 2004.4.10
 *
 *
 *
 *********************************************************************/
#define  INPUT_GLOBALS
#include "net_cfg.h"

uint    Command_Len;        //命令长度
/**********************************************************************
**函数原型:    uchar  Str_Compare(unsigned char code *string,uchar number)
**入口参数:    unsigned char code *string  :
**             uchar number                :
**出口参数:    uchar
**返 回 值:    0    :
**             1    :
**说    明:    用给定的字符串与从串口输入的字符串进行比较,如果串口输入的字符串
**             中包含给定的字符串,则返回1,否则返回0.
************************************************************************/
uchar Str_Compare(unsigned char code *string,uchar number)
{
  uchar i;
  uchar temp;
  for (i=0;i<number;i++)
  {
      temp=Command_Buf[i];
      if(temp!=(*string))
          {return(0);}
      string++;
  }
  return(1);
}

/**********************************************************************
**函数原型:   void     process_command()
**入口参数:   无
**出口参数:   无
**返 回 值:   无              
**说    明:   处理用户从串口输入的命令
************************************************************************/
void process_command()
{
  uchar temp;
  uchar i;
  uint ip;
  uchar j;

  while(ComRxdRead!=ComRxdWrite)
  {
      temp=Get_Char();
      Command_Buf[Command_Len]=temp;
      if((temp!=' ')&&(temp!=0x0d)) Command_Len++;//表示不是空格和回车
      if(Command_Len==COMMAND_BUFF_SIZE) Command_Len=0;
      if(temp==0x0d) //回车键
      {
          if(Command_Len>9) //一个ping命令的指令最少要10个字符
//======================================================================================    
          if(( Str_Compare("ping",4)!=0 )||( Str_Compare("PING",4)!=0 ))
        //==========================以下为ping命令的解析和执行
          {
              ip=0;
              for(i=4,j=0;i<Command_Len;i++)
              {
                  if(Command_Buf[i]!='.')
                  {
                      Command_Buf[i]=Command_Buf[i]&0x0f;
                      ip=ip*10;
                      ip=ip+Command_Buf[i];
                  }
                  else
                  {
                      Ping_Ip_Address.bytes[j]=ip;
                      j++;
                      ip=0;
                  }
              }//for
              if(j==3)
              {
                  Ping_Ip_Address.bytes[j]=ip;
                  // 表示命令正确
                  Printf_String("\r\nPing IP=") ;
                  Printf_IPStr(Ping_Ip_Address);
                  Printf_String("\r\n") ;
                  Ping_Count=6;
                  if((Ping_Ip_Address.dwords & Mask_Ip_Address.dwords)==(My_Ip_Address.dwords & Mask_Ip_Address.dwords))
                  {//表示位于同一子网
                       if((Ping_Ip_Address.dwords==Gateway_Ip_Address.dwords)&(Gateway_IP_TTL>5))
                       {
                            for (i=0;i<3;i++)
                            {
                                Ping_MAC.words[i]=Gateway_MAC.words[i];//=RxdNetBuff.ArpFrame.SourceMacId[i];
                            }
                            Ping_Request();
                       }
                       else
                       {
                       	    Arp_Request(Ping_Ip_Address.dwords);
                       }
                  }//表示位于同一子网
                  else
                  {//表示属于不同的子网,需要通过网关.
                      Printf_String("\r\nThe Host IS In other Subnet");
                      Ping_IP_TTL=10;
                      for(j=0;j<6;j++)
                      {
                          Ping_MAC.bytes[j]=Gateway_MAC.bytes[j];
                      }
                      if(Gateway_IP_TTL==0)
                      {
                          Printf_String("\r\nGateWay Not Found!\r\nC:>");
                          Ping_Count=0;
                      }
                  }//表示属于不同的子网,需要通过网关.
              }//if(j==3)
              else
              {
                  Printf_String("\r\nPing Command Error\r\nC:>");
              }
          }//PING command
//====================================================================================
          //设置IP值
          if ((Str_Compare("setip",5)!=0)||(Str_Compare("SETIP",5)!=0))
          {
              ip = 0;
              for(i=5,j=0;i<Command_Len;i++)
              {  
                 if(Command_Buf[i]!='.')
                 {
                     Command_Buf[i]=Command_Buf[i]&0x0f;
                     ip=ip*10;
                     ip=ip+Command_Buf[i];
                 }
                 else
                 {
                     My_Ip_Address.bytes[j]=ip;
                     j++;
                     ip=0;
                 }
              }
                                        //命令执行
              if(j==3)
              {
                  My_Ip_Address.bytes[j]=ip;
              }
              //Printf_String("IP IS SET");
              Printf_String("\r\nLOCAL IP WAS SET TO ") ;
              Printf_IPStr(My_Ip_Address);
              Printf_String("\n");
          }//SETIP
//================================================================================
          //设置网关值
          if ((Str_Compare("setgate",7)!=0)||(Str_Compare("SETGATE",7)!=0))
          {
              ip = 0;
              for(i=7,j=0;i<Command_Len;i++)
              {  
                  if(Command_Buf[i]!='.')
                  {
                      Command_Buf[i]=Command_Buf[i]&0x0f;
                      ip=ip*10;
                      ip=ip+Command_Buf[i];
                     
                  }
                  else
                  {
                      Gateway_Ip_Address.bytes[j]=ip;
                      j++;
                      ip=0;
                  }
              }
                                        //命令执行
              if(j==3)
              {
                  Gateway_Ip_Address.bytes[j]=ip;
              }
              Printf_String("GATEWAY WAS SET TO ");
              Printf_IPStr(Gateway_Ip_Address);
              Printf_String("\n");
          }//setgate
//================================================================================
          //=================以下处理udp command
          if((Str_Compare("udp",3)!=0)||(Str_Compare("UDP",3)!=0))
          {
              ip=0;
              for(i=3,j=0;i<Command_Len;i++)
              {//取得ip地址
                  if(Command_Buf[i]!='.')
                  { 
                      Command_Buf[i]=Command_Buf[i]&0x0f;
                      ip=ip*10;
                      ip=ip+Command_Buf[i];
                  }
                  else
                  {
                      if(j<4)Ping_Ip_Address.bytes[j]=ip;
                      if(j==4)
                      {
                           RemotePort.word=ip;
                           //if(j==5)RemotePort.bytes.low=ip;
                           Printf_PortStr(RemotePort.word);
                      }
                      j++;
                      if(j==5)
                      {
                           Command_Buf[0]=i+1;
                           Command_Len=Command_Len-i-1;
                           Command_Buf[1]=Command_Len>>8;
                           Command_Buf[2]=Command_Len&0xff;
                           break;//
                      }
                      ip=0;
                  }
              }
              if(j==5)
              {// 表示命令正确
                  Printf_String("\r\nUDP:DestIP=") ;
                  Printf_IPStr(Ping_Ip_Address);
                  Printf_String(" RemotePort=");
                  Printf_PortStr(RemotePort.word);
                  Udp_Count=10;//10second
                  if((Ping_Ip_Address.dwords&Mask_Ip_Address.dwords)==
                                           (My_Ip_Address.dwords&Mask_Ip_Address.dwords))
                  {//表示位于同一子网.
                      Arp_Request(Ping_Ip_Address.dwords);
                  }
                  else
                  {//表示属于不同的子网,需要通过网关.
                      Ping_IP_TTL=10;
                      for(j=0;j<6;j++)
                      {
                          Ping_MAC.bytes[j]=Gateway_MAC.bytes[j];
                      }
                      if(Gateway_IP_TTL==0)
                      {
                           Printf_String("\r\nGateWay Not Found!\r\nC:>");
                           Udp_Count=0;
                      }
                  }
              }
              else
              {
                  Printf_String("\r\nudp Command Error\r\nC:>");
              }
          }//UDP

//================================================================================
          //张会福 增加 2004-10
          //显示帮助命令

⌨️ 快捷键说明

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