[转载]XModem协议源码-xmodem.c

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

2012-12-2811:32:37|·ÖÀࣺǶÈëʽ|±êÇ©£ºxmodemÔ´Âë|¾Ù±¨|×ֺŶ©ÔÄ#includestring.h#includecrc16.h#defineSOH0x01#defineSTX0x02#defineEOT0x04#defineACK0x06#defineNAK0x15#defineCAN0x18#defineCTRLZ0x1A#defineDLY_1S1000#defineMAXRETRANS25staticintlast_error=0;voidport_outbyte(unsignedchartrychar){unsignedcharbuf[2];buf[0]=trychar;lowLevel_write(buf,1);}unsignedcharport_inbyte(unsignedinttime_out){unsignedcharch;inti;last_error=0;if(lowLevel_read(&ch,1)==1)returnch;last_error=1;returnch;}staticintcheck(intcrc,constunsignedchar*buf,intsz){if(crc){unsignedshortcrc=crc16_ccitt(buf,sz);unsignedshorttcrc=(buf[sz]8)+buf[sz+1];if(crc==tcrc)return1;}else{inti;unsignedcharcks=0;for(i=0;isz;++i){cks+=buf[i];}if(cks==buf[sz])return1;}return0;}staticvoidflushinput(void){//while(port_inbyte(((DLY_1S)*3)1)=0);}intxmodemReceive(unsignedchar*dest,intdestsz){unsignedcharxbuff[1030];unsignedchar*p;intbufsz,crc=0;unsignedchartrychar='C';unsignedcharpacketno=1;inti,c,len=0;intretry,retrans=MAXRETRANS;for(;;){for(retry=0;retry16;++retry){if(trychar)port_outbyte(trychar);c=port_inbyte((DLY_1S)1);if(last_error==0){switch(c){caseSOH:bufsz=128;gotostart_recv;caseSTX:bufsz=1024;gotostart_recv;caseEOT:flushinput();port_outbyte(ACK);returnlen;caseCAN:c=port_inbyte(DLY_1S);if(c==CAN){flushinput();port_outbyte(ACK);return-1;}break;default:break;}}}if(trychar=='C'){trychar=NAK;continue;}flushinput();port_outbyte(CAN);port_outbyte(CAN);port_outbyte(CAN);return-2;start_recv:if(trychar=='C')crc=1;trychar=0;p=xbuff;*p++=c;for(i=0;i(bufsz+(crc?1:0)+3);++i){c=port_inbyte(DLY_1S);if(last_error!=0)gotoreject;*p++=c;}if(xbuff[1]==(unsignedchar)(~xbuff[2])&&(xbuff[1]==packetno||xbuff[1]==(unsignedchar)packetno-1)&&check(crc,&xbuff[3],bufsz)){if(xbuff[1]==packetno){intcount=destsz-len;if(countbufsz)count=bufsz;if(count0){memcpy(&dest[len],&xbuff[3],count);len+=count;}++packetno;retrans=MAXRETRANS+1;}if(--retrans=0){flushinput();port_outbyte(CAN);port_outbyte(CAN);port_outbyte(CAN);return-3;}port_outbyte(ACK);continue;}reject:flushinput();port_outbyte(NAK);}}intxmodemTransmit(unsignedchar*src,intsrcsz){unsignedcharxbuff[1030];intbufsz,crc=-1;unsignedcharpacketno=1;inti,c,len=0;intretry;for(;;){for(retry=0;retry16;++retry){c=port_inbyte((DLY_1S)1);if(last_error==0){switch(c){case'C':crc=1;gotostart_trans;caseNAK:crc=0;gotostart_trans;caseCAN:c=port_inbyte(DLY_1S);if(c==CAN){port_outbyte(ACK);flushinput();return-1;}break;default:break;}}}port_outbyte(CAN);port_outbyte(CAN);port_outbyte(CAN);flushinput();return-2;for(;;){start_trans:xbuff[0]=SOH;bufsz=128;xbuff[1]=packetno;xbuff[2]=~packetno;c=srcsz-len;if(cbufsz)c=bufsz;if(c=0){memset(&xbuff[3],0,bufsz);if(c==0){xbuff[3]=CTRLZ;}else{memcpy(&xbuff[3],&src[len],c);if(cbufsz)xbuff[3+c]=CTRLZ;}if(crc){unsignedshortccrc=crc16_ccitt(&xbuff[3],bufsz);xbuff[bufsz+3]=(ccrc8)&0xFF;xbuff[bufsz+4]=ccrc&0xFF;}else{unsignedcharccks=0;for(i=3;ibufsz+3;++i){ccks+=xbuff[i];}xbuff[bufsz+3]=ccks;}for(retry=0;retryMAXRETRANS;++retry){for(i=0;ibufsz+4+(crc?1:0);++i){port_outbyte(xbuff[i]);}c=port_inbyte(DLY_1S);if(last_error==0){switch(c){caseACK:++packetno;len+=bufsz;gotostart_trans;caseCAN:c=port_inbyte(DLY_1S);if(c==CAN){port_outbyte(ACK);flushinput();return-1;}break;caseNAK:default:break;}}}port_outbyte(CAN);port_outbyte(CAN);port_outbyte(CAN);flushinput();return-4;}else{for(retry=0;retry10;++retry){port_outbyte(EOT);c=port_inbyte((DLY_1S)1);if(c==ACK)break;}flushinput();return(c==ACK)?len:-5;}}}}

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功