1、研究 Xmodem协议必看的 11个问题原文地址:http:/ “string.h“void port_outbyte(unsigned char trychar)unsigned char buf2;buf0 = trychar;lowLevel_write(buf,1);unsigned char port_inbyte(unsigned int time_out)unsigned char ch;int i;last_error = 0;if(lowLevel_read(last_error = 1;return ch;static int check(int crc, const un
2、signed char *buf, int sz)if (crc)unsigned short crc = crc16_ccitt(buf, sz);unsigned short tcrc = (bufsz1) = 0);int xmodemReceive(unsigned char *dest, int destsz)unsigned char xbuff1030;unsigned char *p;int bufsz, crc = 0;unsigned char trychar = C;unsigned char packetno = 1;int i, c, len = 0;int retr
3、y, retrans = MAXRETRANS;for(;)for( retry = 0; retry bufsz)count = bufsz;if (count 0)memcpy (len += count;+packetno;retrans = MAXRETRANS+1;if (-retrans bufsz) c = bufsz;if (c = 0)memset (if (c = 0)xbuff3 = CTRLZ;elsememcpy (if (c 8) xbuffbufsz+4 = ccrc elseunsigned char ccks = 0;for (i = 3; i bufsz+3
4、; +i)ccks += xbuffi;xbuffbufsz+3 = ccks;for (retry = 0; retry MAXRETRANS; +retry)for (i = 0; i bufsz+4+(crc?1:0); +i)port_outbyte(xbuffi);c = port_inbyte(DLY_1S);if (last_error = 0 )switch (c)case ACK:+packetno;len += bufsz;goto start_trans;case CAN:c = port_inbyte(DLY_1S);if ( c = CAN)port_outbyte(ACK);flushinput();return -1;break;case NAK:default:break;port_outbyte(CAN);port_outbyte(CAN);port_outbyte(CAN);flushinput();return -4;elsefor (retry = 0; retry 10; +retry)port_outbyte(EOT);c = port_inbyte(DLY_1S)1);if (c = ACK) break;flushinput();return (c = ACK)?len:-5;