<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 51單片機簡單Ping的實現(xiàn)

          51單片機簡單Ping的實現(xiàn)

          作者: 時間:2012-10-12 來源:網(wǎng)絡(luò) 收藏

          Echo(*輸入包緩沖區(qū)首址pBUF) //收到應(yīng)答后回顯
          {
          //打印ping響應(yīng),因為51定時器較慢,time參數(shù)省略(time是本機與對方主機往返一次所用時間)。
          PrintStr("tReply from IP=");
          PrintStr(輸入包之源IP地址);
          PrintStr(": bytes=32");
          PrintStr(" TTL=");
          PrintByte(輸入包之TTL);
          PrintStr("");

          //處理Buf的記錄
          for(i=0;iMaxLenBuf;i++){
          if(PingBuf[i].status==1){
          if(PingBuf[i].ip==pBUF.ipframe.ip){
          PingBuf[i].status=2; //已發(fā)出且應(yīng)答
          break;
          }
          }
          }
          }

          PingCycle() //定時操作,放在1秒循環(huán)任務(wù)中
          {
          for(;;){
          taskDelay(1秒);
          for(i=0;iMaxLenPingBuf;i++){
          switch(PingBuf[i].status)
          case 0: //空閑
          break;

          {
          case 1: //已發(fā)出但無應(yīng)答

          //打印超時信息
          PrintStr("tRequest timed out.(");
          PrintStr(PingBuf[i].ip);
          PrintStr(")");

          case 2: //已發(fā)出且應(yīng)答

          //狀態(tài)變遷
          PingBuf[i].times=PingBuf[i].times-1;
          if(PingBuf[i].times==0)
          PingBuf[i].status=0;
          else{

          case 4: //第一次準(zhǔn)備發(fā)(用于同步1秒時鐘)

          //查ARP緩存
          if(ARP緩存有對應(yīng)項){

          //直接發(fā)送ICMP包至TxQFIFO緩存
          OSQSend(QID,*pBUF);

          PingBuf[i].status=1; //已發(fā)出但無應(yīng)答
          }
          else PingBuf[i].status=3; //等ARP
          }
          break;
          }
          case 3: //等ARP
          {
          //查ARP緩存
          if(ARP緩存有對應(yīng)項){
          //直接發(fā)送ICMP包至TxQFIFO緩存
          OSQSend(QID,*pBUF);
          }
          PingBuf[i].status=1; //已發(fā)出但無應(yīng)答
          }
          default: //其他狀態(tài),錯誤
          PingBuf[i].status=0;
          }
          }
          }

          void PingCommand(WORDTABLE *WordTable)//PING命令處理
          {
          if(WordTable->Num==1)
          PrintStr("Please input IP address!");
          else{
          if(IPadrToHEX(WordTable->wt[1].Str,ping_ip_address)==0){
          PrintStr("IP address error!");return;
          }
          else
          PingRequest(ping_ip_address);
          }
          }

          INT16U CheckSum(INT16U *buf,INT16U length) //校驗和計算
          {
          INT16U len;
          INT32U sum;

          len=length>>1;
          for(sum=0;len>0;len--)
          sum+=*buf++;
          if(length1)
          sum+=(*buf0xFF00);
          sum=(sum>>16)+(sum0xFFFF);
          sum+=(sum>>16);

          return(~sum);
          }

          union ip_address_type{ //ip地址數(shù)據(jù)結(jié)構(gòu)
          unsigned long dwords;
          unsigned int words[2];
          unsigned char bytes[4];
          };

          bit IPadrToHEX(unsigned char *Str,union ip_address_type *ip) //IP字符串轉(zhuǎn)換到IP地址值
          {
          unsigned char i,j,ch,x;

          ch=*Str++;

          for(j=0;j3;j++){
          x=0;
          for(i=0;i4;i++){
          if(ch=='.') {ch=*Str++;break;}
          else if(i==3) return 0;
          else if(ch0ch>9) return 0;
          else
          x=10*x+(ch-'0');
          ch=*Str++;
          }
          ip->bytes[j]=x;
          }

          x=0;
          for(i=0;i4;i++){
          if(ch=='看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();