51單片機(jī)簡(jiǎn)單Ping的實(shí)現(xiàn)
PingEcho(*輸入包緩沖區(qū)首址pBUF) //收到應(yīng)答后回顯
{
//打印ping響應(yīng),因?yàn)?1定時(shí)器較慢,time參數(shù)省略(time是本機(jī)與對(duì)方主機(jī)往返一次所用時(shí)間)。
PrintStr("tReply from IP=");
PrintStr(輸入包之源IP地址);
PrintStr(": bytes=32");
PrintStr(" TTL=");
PrintByte(輸入包之TTL);
PrintStr("");
//處理PingBuf的記錄
for(i=0;iMaxLenPingBuf;i++){
if(PingBuf[i].status==1){
if(PingBuf[i].ip==pBUF.ipframe.ip){
PingBuf[i].status=2; //已發(fā)出且應(yīng)答
break;
}
}
}
}
PingCycle() //定時(shí)操作,放在1秒循環(huán)任務(wù)中
{
for(;;){
taskDelay(1秒);
for(i=0;iMaxLenPingBuf;i++){
switch(PingBuf[i].status)
case 0: //空閑
break;
{
case 1: //已發(fā)出但無(wú)應(yīng)答
//打印超時(shí)信息
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秒時(shí)鐘)
//查ARP緩存
if(ARP緩存有對(duì)應(yīng)項(xiàng)){
//直接發(fā)送ICMP包至TxQFIFO緩存
OSQSend(QID,*pBUF);
PingBuf[i].status=1; //已發(fā)出但無(wú)應(yīng)答
}
else PingBuf[i].status=3; //等ARP
}
break;
}
case 3: //等ARP
{
//查ARP緩存
if(ARP緩存有對(duì)應(yīng)項(xiàng)){
//直接發(fā)送ICMP包至TxQFIFO緩存
OSQSend(QID,*pBUF);
}
PingBuf[i].status=1; //已發(fā)出但無(wú)應(yīng)答
}
default: //其他狀態(tài),錯(cuò)誤
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) //校驗(yàn)和計(jì)算
{
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);
})();